Jamus
Jamus

Reputation: 865

Sending JavaScript Variable to PHP via POST

Is it possible to send the contents of a JavaScript variable to PHP when a form is submitted?

Upvotes: 1

Views: 105

Answers (1)

Armel Larcier
Armel Larcier

Reputation: 16027

Yes. Just add a javascript callback to the form submit event

<script type="text/javascript">
    var myglobalvariable = "myvalue";
    onSubmit = function(){
        document.myform.myinput.value = myglobalvariable;  return true;
    }
</script>
<form name="myform" id="myform" method="post" action="index.php" onsubmit="onSubmit();">
    <input name="myinput" id="myinput" type="hidden" />
</form>

Upvotes: 3

Related Questions