0

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?

1
  • Please remember to upvote all answers that were helpful, thanks. Commented Mar 12, 2013 at 23:14

4 Answers 4

1

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;
}
Sign up to request clarification or add additional context in comments.

Comments

0

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>

Comments

0

Make a <input type="hidden" id="field" /> and update it's value with jQuery.

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

Comments

0

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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.