-2

I tried to receive the data from a form to another using the following code it worked.

var fname = document.getElementsByName("fname")[0];
fname.value = getUrlVars()["fname"];

Now i want to receive the data from form to a table which is already created. I used the code as $("table").html("<tr><td>"+fname +"</td><td>"); its not working.

Patrick Kostjens
  • 5,065
  • 6
  • 29
  • 46
  • What is `setinnerHTML`? – Explosion Pills Jul 04 '13 at 14:09
  • Inorder to set the value i used it.. plz say what is the correct syntax for setting a value which is obtained from another form.. – user2549147 Jul 04 '13 at 14:12
  • If you want to set values of form you need to assign values to Input boxes. Check out this http://stackoverflow.com/questions/7609130/set-the-value-of-a-input-field-with-javascript – GoodSp33d Jul 04 '13 at 14:14
  • http://meta.stackexchange.com/questions/156810/stack-overflow-question-checklist See [this to learn Javascript](https://developer.mozilla.org/en-US/docs/Web/JavaScript?redirectlocale=en-US&redirectslug=JavaScript) (Tools and advanced resources section) – Jeff Noel Jul 04 '13 at 14:59
  • In what way you know it "is not working"? have you tried using Firebug or jslint or any other code checking or debugging tool? – PA. Jul 04 '13 at 15:06
  • what is `getElementsByNamee`? Please make sure to post the actual code you are trying. – PA. Jul 04 '13 at 15:07

4 Answers4

1

Two issues with the code.

This is a useless statement, you need to save the result to some variable (not to mention Nmae):

document.getElementsByNmae("lname")

Should be:

var lname = document.getElementsByName("lname");

And then (setinnerHTML -> innerHTML):

lname.innerHTML="lname";
mishik
  • 9,973
  • 9
  • 45
  • 67
1

In this statement,

var fname = document.getElementsByName("fname");
fname.innerHTML = "fname";

What is the element with name "fname"?

If its a form element like textbox then it should be like,

var fname = document.getElementsByName("fname");
fname.value = "fname";

your code will only work if the element is not a form element like p or div, etc tags.

Edited Code:

I hope your second page is student.html and you have written the receiveData() in this page. Then you need to read the url and set the parameter value to the element. Like the one am writing below, provided your wrote the same name in form 2 as in form1,

var fname = document.getElementsByName("fname")[0];
fname.value = getUrlVars()["fname"];

2ndly yo can do this for textbox, but for the radio and dropdown you need to write som if-else statement.

Refer this http://papermashup.com/read-url-get-variables-withjavascript/

Hope you are getting what am willing to say.

Re-Edited Code:

Add this function with the receiveData() function.

function getUrlVars() {
 var vars = {};
 var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi,
  function(m,key,value) {
 vars[key] = value;
 });
 return vars;
}

Code for Radio Button,

var sex = document.getElementsByName("sex");
sexValue = getUrlVars()["sex"];
for(i=0;i<sex.length;i++)
{
 if(sex[i].value==sexValue)
 {
  sex[i].checked=true;
  break;
 }
}
Nitesh Mishra
  • 311
  • 1
  • 10
  • I had made the changes which were said by u but not displaying the data in the form. please suggest more... element fname is the name given to the textfield First Name. – user2549147 Jul 04 '13 at 15:15
  • as per your question you are taking data from one form to another, so where are the two forms. can you put the html in your question or share a fiddle? – Nitesh Mishra Jul 04 '13 at 15:19
  • This is the first form... – user2549147 Jul 04 '13 at 15:24
  • again instead of using **document.getElementsByName("fname")** use **document.getElementById("fname")**. And dont forget to add id attribute in the **input** tag. – Nitesh Mishra Jul 04 '13 at 15:26
  • as getElementsByName('n') always return an array of elements who have their name as n. so, to access it use **getElementsByName('n')[0]** or any index you want. in your case it would be 0. – Nitesh Mishra Jul 04 '13 at 15:33
  • can you please show me the complete html of both the form, the source form and destination form. – Nitesh Mishra Jul 04 '13 at 15:56
  • still more..
    Student Registration Form
    First Name:
    Last Name:
    Father Name:
    – user2549147 Jul 04 '13 at 16:31
  • Gender: Male Female
    Qualifiction:
    – user2549147 Jul 04 '13 at 16:33
  • Email-Id:
    Password:
    Mobile No:
    Address:
    DOB:
    – user2549147 Jul 04 '13 at 16:35
  • ok, whether the 2nd form is in the same page or other. Does 2nd form contains the same no of elements with same name? – Nitesh Mishra Jul 04 '13 at 17:14
  • I wrote what u said but the value is not getting by the second form – user2549147 Jul 05 '13 at 05:50
  • try using **var fname = document.getElementsByName("fname")[1]** – Nitesh Mishra Jul 05 '13 at 06:49
  • I used it the first name is displaying in the url but not in the form. what can i do?? – user2549147 Jul 05 '13 at 07:07
  • first please clear, are those both forms are in the same page or in different page. if both are in same page then why are you using two forms? why you want to copy the data from one form to another. – Nitesh Mishra Jul 05 '13 at 07:13
  • Actually i created a registration form in one page with some fields. when i click the submit button then the details which were entered in that form must be displayed in the other form which is same as that form. – user2549147 Jul 05 '13 at 07:18
  • ok, then i hope 2nd form is in the 2nd page. so the code i had written in the edited code section in my answer will work. provided you have written the receiveData() function in the 2nd page. – Nitesh Mishra Jul 05 '13 at 07:23
  • i think i got your problem. try writing the receiveData() function in the 2nd page after the body end tag. – Nitesh Mishra Jul 05 '13 at 07:28
  • I wrote it after the

    in second page but not got the data.

    – user2549147 Jul 05 '13 at 07:33
  • you have used method **post** in the first form. Use **get** to get data in url. – Nitesh Mishra Jul 05 '13 at 08:48
  • and remove the last statement i.e. submit statement. as you dont need to submit anything in the 2nd page. – Nitesh Mishra Jul 05 '13 at 08:59
  • I used get method and removed submit statement but still not got it. – user2549147 Jul 05 '13 at 09:10
  • are you using **var fname = document.getElementsByName("fname")[0]** – Nitesh Mishra Jul 05 '13 at 09:46
  • I used var fname= document.getElementsByName("fname")[0]; but didnt got result. – user2549147 Jul 05 '13 at 10:00
  • check **Re-Edited Code** in my Answer. – Nitesh Mishra Jul 05 '13 at 10:33
  • I added as u said : function ReceiveData() { var fname = document.getElementsByName("fname")[0]; fname.value = getUrlVars()["fname"]; function getUrlVars() { var vars = {}; var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) { vars[key] = value; }); return vars; } } but didnt got. – user2549147 Jul 05 '13 at 10:57
  • no no, receivedata() and getUrlvars() are different functions. dont put the 2nd one inside the first one. put it different. – Nitesh Mishra Jul 05 '13 at 11:20
  • I wrote ur code in b/w and my receive function next to it in and i wrote statement as var fname = document.getElementsByName("fname")[0]; fname.value = getUrlVars()["fname"]; didnt got it.' – user2549147 Jul 05 '13 at 11:32
  • script should be after the body end tag or just before it. the javascripts should be like this [http://jsfiddle.net/nitesh354/YhZVx/1/](http://jsfiddle.net/nitesh354/YhZVx/1/) – Nitesh Mishra Jul 05 '13 at 11:54
  • again the problem is who is calling the receivedata() function. so, don't write this function, rather simply write its contents. – Nitesh Mishra Jul 05 '13 at 11:55
  • where should i write the var fname = document.getElementsByName("fname")[0]; fname.value = getUrlVars()["fname"];.. i wrote it before script. should i include it in { }.. – user2549147 Jul 05 '13 at 12:38
  • yes you should include it in the script tag like: **''** – Nitesh Mishra Jul 05 '13 at 12:42
  • Thanks a lot i got every fields data except radio button's data could you please tell about it. – user2549147 Jul 05 '13 at 13:14
  • Thanks a lot i got exact output...atlast by u i got the correct guidance thank u soo much.. – user2549147 Jul 05 '13 at 14:12
  • you welcome, hope you will not mind to upvote this answer. :) – Nitesh Mishra Jul 05 '13 at 14:23
0

Use docs not your own imagination:

var newEl = document.getElementById("newElDay");
newEl.innerHTML = document.getElementById("oldElDay").innerHTML;
webrama.pl
  • 1,870
  • 1
  • 23
  • 36
0

Apart from other problems which people have mentioned, perhaps you are missing this as the last line of your Javascript code inside Receivedata() :

document.getElementById('myform').submit();
Abhitalks
  • 27,721
  • 5
  • 58
  • 81
  • document.getElementById('StudentRegistration').submit(); i wrote this but still not getting.. on the same form i wrote
    ..
    – user2549147 Jul 04 '13 at 15:22
  • Why is the action attribute "#" ? Are you are posting to the same page? In that case use "pagename.ext". – Abhitalks Jul 04 '13 at 15:33