3 Replies - 982 Views - Last Post: 24 August 2013 - 02:29 PM

#1 JackoLeylo09  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 24-August 13

Basic Task - Error using forms

Posted 24 August 2013 - 02:06 PM

Firstly, i am an amateur programmer so if I act stupid I do apologise.

Using java, I am trying to create a form to get a user's name, and then use this inserted name in a sentence, further down the page. So basically, "Welcome (name) to my page".

I have experimented with the basic code I know to use a form and extract data from the form - but I can't figure it out.

Any help with how I can use this form to complete a sentence using the user's name would be appreciated. These are my efforts so far.... (incomplete, obviously)

<form name="jacks">
Your name is:  <input type="text"> <input type="submit" value="CONFIRM!">

  var x = document.jacks.usersname.name;
  document.write ("Welcome" + x + "to my website")

Is This A Good Question/Topic? 0
  • +

Replies To: Basic Task - Error using forms

#2 andrewsw  Icon User is offline

  • bin deployable
  • member icon

Reputation: 6261
  • View blog
  • Posts: 25,051
  • Joined: 12-December 12

Re: Basic Task - Error using forms

Posted 24 August 2013 - 02:24 PM

FYI java and Javascript are completely different languages, you can use JS if you want to abbreviate Javascript.

There is a lot wrong with your code so I suggest that you take a tutorial and study before attempting to write your own code. There is a Tutorials link at the top of this page. (I should not have to suggest this, it is common sense :dontgetit: )

This post has been edited by andrewsw: 24 August 2013 - 02:29 PM

Was This Post Helpful? 0
  • +
  • -

#3 CasiOo  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1553
  • View blog
  • Posts: 3,489
  • Joined: 05-April 11

Re: Basic Task - Error using forms

Posted 24 August 2013 - 02:27 PM

Give a name attribute to your input element like so
<input name="name" type="text">

You can then get a reference to it by doing
var usernameElement = document.jacks.name;

However! When you submit your form, you will either do a POST or GET to the webserver with the form data
The request will make a roundtrip to the server and receive a new page. In your case the same page is displayed
The value of the input element will now be blanked, so you cannot use document.jacks.name.value since it is empty

Your form is currently set up for a GET request. This means that the form data will be visible in the query string
For example if I enter "Foo" and submit, the new URL will be: page.html?name=foo
You therefore simply need to parse the query string and get the value of the "name" key
Was This Post Helpful? 0
  • +
  • -

#4 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1158
  • View blog
  • Posts: 2,538
  • Joined: 05-May 05

Re: Basic Task - Error using forms

Posted 24 August 2013 - 02:29 PM

**Moved to Javascript forum**

You really don't need a form for this. You need to (1) create a text element input and define an 'id' attribute (or 'name' attribute), (2) create a 'div' element to place the sentence, (3) create a Javascript function that gets the text element value using document.getElementById, and (4) create a button and define its 'onclick' attribute to call the Javascript function.

See this example.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1