简体   繁体   中英

Send javascript function variable with form

I need to send a variable returned from a javascript function with a form.

<form name="RegForm"  method="post" action="/validate_accountinfo.php" onsubmit="send()">
</form>


function send()
{
   var number = 5;
   return number;
}

In the validate_accountinfo.php I want to return the value of the function. How to do this?

Place a hidden field in your form, and set its value in your javascript function.

Hidden field:

<input type="hidden" id="hdnNumber">

JavaScript:

function send(){
    var number = 5;
    document.getElementById("hdnNumber").value = number;
}

Add an <input hidden id="thevalue" name="thevalue" /> to the form, set its value using javascript and then submit the form.

<form id="RegForm" name="RegForm"  method="post" action="/validate_accountinfo.php" onsubmit="send()">
    <input hidden id="thevalue" name="thevalue" />
</form>

<script type="text/javascript">
function send()
{
   var number = 5;
   return number;
}
document.getElementById('thevalue').value = send();
document.getElementById('RegForm').submit();
</script>

创建一个<input type="hidden" id="field" />并使用jQuery更新其值。

$("#field").attr({value: YourValue });

Add a hidden input and populate it before sending. Make sure you specify a name= attribute.

<form name="RegForm"  method="post" action="/validate_accountinfo.php" onsubmit="send()">
    <input type="hidden" name="myvalue"/>
</form>


function send()
{
    var number = 5;

    // jQuery
    $('input[name=myvalue]').val( number )

    return true;
}

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