简体   繁体   中英

Sending JavaScript Variable to PHP via POST

是否可以在提交表单时将JavaScript变量的内容发送给PHP?

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM