Possible Duplicate:
JavaScript post request like a form submit
I have a value calculated in JS that I'd like to pass as part of an input form to a PHP script. How can I get a value the JS value to the PHP as a POST parameter?
Basically, on submit, I need the total
var to be passed via post to the next script.
The first idea that comes to mind is create an invisible input form that has the value in it and is inputted with the form, is that possible?
There is a lot of ways to achieve this. In regards to the way you are asking, with a hidden form element.
create this form element inside your form:
<input type="hidden" name="total" value="">
So your form like this:
<form id="sampleForm" name="sampleForm" method="post" action="phpscript.php"> <input type="hidden" name="total" id="total" value=""> <a href="#" onclick="setValue();">Click to submit</a> </form>
Then your javascript something like this:
<script> function setValue(){ document.sampleForm.total.value = 100; document.forms["sampleForm"].submit(); } </script>
Yes you could use an <input type="hidden" />
and set the value of that hidden field in your javascript code so it gets posted with your other form data.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With