admin管理员组

文章数量:1277374

I want to submit a form and add a javascript variable with it. I tried using AJAX but that didn't work properly because the form is cut in two parts on the same page. I'm now using a <button onclick='submit_form()'>send</button> which calls on the following function:

function submit_form()
{
document.getElementById("form2").submit();
}

however I need to pass on a javascript variable current_score I have it declared and it has a value I just don't know how to add this to my submit function I tried using a return within the function itself and writing a function for it but neither worked :) help and hints are greatly appreciated. The idea is that people receive a score fill a form and send it to a database, The ajax script part was to try and pass the values on to the next page that will submit the data

I want to submit a form and add a javascript variable with it. I tried using AJAX but that didn't work properly because the form is cut in two parts on the same page. I'm now using a <button onclick='submit_form()'>send</button> which calls on the following function:

function submit_form()
{
document.getElementById("form2").submit();
}

however I need to pass on a javascript variable current_score I have it declared and it has a value I just don't know how to add this to my submit function I tried using a return within the function itself and writing a function for it but neither worked :) help and hints are greatly appreciated. The idea is that people receive a score fill a form and send it to a database, The ajax script part was to try and pass the values on to the next page that will submit the data

Share Improve this question edited May 4, 2012 at 14:20 Jeffrey Klinkhamer asked May 4, 2012 at 14:13 Jeffrey KlinkhamerJeffrey Klinkhamer 4153 gold badges5 silver badges10 bronze badges 3
  • Is current_score a form value? If not, what is it? – wanovak Commented May 4, 2012 at 14:15
  • I can't understand what you're asking. How did you "try using AJAX"? Do you have some sample code? – Marc Commented May 4, 2012 at 14:17
  • it's a int variable that's used by another function to calculate a score. The idea is that people receive a score fill a form and send it to a database – Jeffrey Klinkhamer Commented May 4, 2012 at 14:19
Add a ment  | 

2 Answers 2

Reset to default 9

Your question is not very clear. The simple way would be to append a get parameter to the URL you are requesting. The following example will append a hidden input element to your form:

var hidden = document.createElement("input");
hidden.type = "hidden";
hidden.name = "theName";
hidden.value = current_score;
var f = document.getElementById("form2");
f.appendChild(hidden);
f.submit();

You could store the information in window.variable also

本文标签: htmlsubmitting form through javascript and passing a variable with itStack Overflow