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
https://forums.phpfreaks.com/topic/126704-solved-help/
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
https://forums.phpfreaks.com/topic/126704-solved-help/#findComment-655317
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.