1

i have a form which user enters some data, could be checkboxes, radio buttons, textfields, etc

when user click submit button, i want to refresh the page with whatever data that was entered

<html> <head> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js" type="text/javascript"></script> </head> <body id="ref"> <form> Please enter your name:<input type="text" id="name" /> Please enter your age:<input type="text" id="age" /> </form> <input type="button" onclick="c()" value="submit"> <script type="text/javascript"> function c() { var o = document.getElementById('ref'); o.innerHTML = ''; var n = document.createElement('p'); var nam = document.getElementById('name'); n.innerHTML = "Your name is: " + nam; o.appendChild(n); var a = document.createElement('p'); var ag = document.getElementById('age'); a.innerHTML = "Your age is: " + ag; o.appendChild(a); //how do i get the info from the form? because both nam and ag are coming up null } </script> </body> </html> 

my guess this is not working is because the page refreshes then tries to fetch the element by id which is not there anymore.. whats the correct way of doing this??

0

3 Answers 3

5

You're confusing objects with their properties. Here, you're getting the HTMLInputElement instance for the "age" field:

var ag = document.getElementById('age'); 

But here you're using that object as though it were a simple value:

a.innerHTML = "Your age is: " + ag; 

The HTMLInputElement object has a value field you can use for that:

a.innerHTML = "Your age is: " + ag.value; 

Separately, you're completely destroying the page by doing this:

var o = document.getElementById('ref'); o.innerHTML = ''; 

...because you've given the body element the ID "ref". Completely replacing the body element completely replaces the body element, so you can't rely on objects that only exist as subordinates of that element.

The usual thing is to have an element to fill in, and (optionally) to remove the elements you no longer need. For instance (live copy):

HTML:

<form id="theForm"> Please enter your name:<input type="text" id="name" /> Please enter your age:<input type="text" id="age" /> <input type="button" onclick="c()" value="submit"> </form> <div id="result"> </div> 

(Note I moved the button into the form for convenience.)

JavaScript:

function c() { var form = document.getElementById("theForm"), nameField = document.getElementById("name"), ageField = document.getElementById("age"), result = document.getElementById("result"); form.parentNode.removeChild(form); result.innerHTML = "Your name is " + nameField.value + " and your age is " + ageField.value; } 

There, when the button is pressed, I remove the form and fill in the "result" div.

You could add the "result" div dynamically if you wanted (live copy):

HTML:

<form id="theForm"> Please enter your name:<input type="text" id="name" /> Please enter your age:<input type="text" id="age" /> <input type="button" onclick="c()" value="submit"> </form> 

JavaScript:

function c() { var form = document.getElementById("theForm"), nameField = document.getElementById("name"), ageField = document.getElementById("age"), result; result = document.createElement("div"); result.innerHTML = "Your name is " + nameField.value + " and your age is " + ageField.value; form.parentNode.insertBefore(result, form); form.parentNode.removeChild(form); } 

You can access the fields using a briefer and somewhat more natural syntax if you change your id values to name values instead (live copy):

HTML:

<form name="theForm"> Please enter your name:<input type="text" name="name" /> Please enter your age:<input type="text" name="age" /> <input type="button" onclick="c()" value="submit"> </form> 

JavaScript:

function c() { var form = document.theForm, nameField = form.name, ageField = form.age, result; result = document.createElement("div"); result.innerHTML = "Your name is " + nameField.value + " and your age is " + ageField.value; form.parentNode.insertBefore(result, form); form.parentNode.removeChild(form); } 

Further reading:

Sign up to request clarification or add additional context in comments.

4 Comments

Negative. DOM provides convenient access via document.forms and elements, so there is no need to call gebi over and over, simply document.theForm.age.value = '42'
@ T.J. Crowder, ah, i stand corrected on absence of name attibutes, didnt noticed the diff between your and original snippets. What do you mean access via window, please? Looks like i failed to remove my downvote :-( could you please touch the post
with all of excessive client-side code here, i have no idea "for sure", but my hipothesis is what i clicked twice, effectively cancelling the thumb-down and then setting it again. Given what real voting status shown here by JS in onload, thats most likely scenario. Also, previously sometimes i had to cast my votes twice. Finally, i unset my downvote, thanks.
Ah, i got it, you meant exposing element references into Global scope, a-la IE. I believe i've even seen a proposal to standardize that behaviour within HTML5 project, not sure what happened afterwards.
1

If you want to update your html using java-script only , you may use ".value" attribute of the input;

 var a = document.createElement('p').value; var ag = document.getElementById('age').value; 

Usually the Form information is processed using server-side code , this is done by specifying the action attribute of the form:

<form action="processuserinfo.aspx"> ... </form> 

Comments

-1

I'm pretty sure this isn't doable javascript alone. You'll need to use a server-side language like php. Try to google php forms, and you should get some good results. :)

3 Comments

reason im using this is cause i tried jsp but my companies serlvets etc is causing the parameters to "disappear" whenever the new page loads.. im an intern btw just tryna figure a few things =/
Yes, it can. You can completely rewrite the entire page if you want to.
never mind i got it.. gotta get the elements BEFORE i change the body of the html.. thanks for your help though!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.