Jump to content

[SOLVED] HELP!!


ngreenwood6

Recommended Posts

I am a noob to javascript and am in the process of learning. However, I have run into a problem. I have defined a function that gets the name from a form that is submitted. When the form is submitted I want it to print the results to the same page that the form is on underneath the form. I have the following code:

 


<form name="form">
<input type="text" name="new_name"  />
<input name="submit" value="submit" type="button" onclick="submit_form();" />
<br />
<br />

</form>

<script type="text/javascript">

function submit_form() {

//get the users name from the form
var users_name = document.form.new_name.value;

}

document.write(users_name);

</script>

 

However it is not posting it to the page. If I put the document.write(users_name); inside the function it prints it to the page but it prints it to a whole new page instead of the one with the form on it. Again I am a noob so please take it easy. Thanks in advance for the help

Link to comment
Share on other sites

Put it in the form but think about it a bit more. Probably the best way to do this is using the <span> tag. Then you can set the inner html.

 

Like so:

 

<script language="javascript">
function submit_form(){
var name = document.getElementById("name").value;

//Now set the values
document.getElementById("writing").innerHTML = name;
}
</script>

<input type="text" name="name" id="name" /><br />
<input type="submit" name="submit" id="submit" value="Submit Form" onclick="javascript: submit_form()" /><br /><br />

<span id="writing"></span>

 

Start using things like getElementById and get in the habit of assigning fields with unique id's.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.