Click here to Skip to main content
15,905,682 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
Can you please give me example of passing value from javascript to PHP through hidden field?
Thanks.

I'm trying to pass the value from body.php

HTML
<html>
<head><script type="text/javascript" src="../js/script.js"></script</head>
<body>
<form name="myform" method="post"
 önSubmit="return validateRadio()" action="">

<table class="tftable" border="1">
<tr><th><div align="center" class="tftable th">Questions</div></th><th colspan="2"><div align="center">Answer</div></th>
</tr>
<tr>
  <td>I like to dance</td>
  <td>
  <label></label>
  <label>
  <input name="bm1" type="radio" value="1" />
  Yes</label>
</td>
<td>
<label></label>
<label>
<input name="bm1" type="radio" value="2" />
No
</label>
</td></table></form>

<?php
if (isset($_GET['myval']))
{
echo "VALUE PASSED  : ".$_GET['myval']."<br>";
}
?>
</body>
</html>


Here's script.js
JavaScript
function validateRadio()
{
// get all the inputs type
var inputs = myform.elements;
var radios = [];

// find the radio type
for (var i = 0; i < inputs.length; ++i) {
if (inputs[i].type == 'radio') {
radios.push(inputs[i]);
}
}
var countChecked = 0;
for (var j = 0; j < radios.length; j++) {
if (radios[j].checked) {
countChecked++;
}
}
//count number of radio button with value=1
var answeramount = 0;
for (var k = 0; k < radios.length; k++){
if(radios[k].checked && radios[k].value==1){
answeramount++;
}
}
if (countChecked != radios.length / 2){
alert("All questions must be answered.");
return false; // abort submit
} else {
 window.location.href = "body.php?myval=" + answeramount;
return true; // proceed to submit
} 
}


The echo "VALUE PASSED : ".$_GET['myval'] doesn't work...
Please help...
Posted
Updated 13-Apr-14 8:50am
v4
Comments
What have you tried?
monsterchub 13-Apr-14 14:51pm    
I've improve the question above.. thx

You are doing it wrong. Your myVal is not a part of the body of HTTP request; it is a part of the URL.

With PHP, you can get the URL of the request as $_SERVER['QUERY_STRING'].
Then you can use the PHP function parse_str, to parse it to an array:
http://php.net/manual/en/function.parse-str.php[^].

Passing parameters through the UTR string is not a very elegant way. If you want to pass some values via the HTTP request, say, through the methods "GET" or "POST" you can:
  • Have a Wen form with the method (form attribute) "post" or "get", with the input controls representing data to be posted; the names of the parameters defined by the values of the values of the attribute name of those controls.
  • Using form is not an universal method; and it needs reloading of the whole page. It's main benefit is: it does not require Javascript.

    The alternative method is using Ajax:
    http://en.wikipedia.org/wiki/Ajax_%28programming%29[^],
    https://api.jquery.com/jQuery.ajax[^].


Good luck.

—SA
 
Share this answer
 
v2
Comments
Manas Bhardwaj 13-Apr-14 15:45pm    
YUP +5!
Sergey Alexandrovich Kryukov 13-Apr-14 15:58pm    
Thank you, Manas.
—SA
Peter Leow 13-Apr-14 21:51pm    
Best, 5ed!
Sergey Alexandrovich Kryukov 13-Apr-14 23:07pm    
Thank you, Peter.
—SA
I tried some solution, so I have changed the some code. Still got error undefined variable. What did i miss?
This is body.php, I add the hidden field and submit button.
HTML
<html>
<head><script type="text/javascript" src="../js/script.js"></script>br mode="hold" /><body>
<form name="myform" method="post">
 önSubmit="return validateRadio()" action="check.php">
 
<table class="tftable" border="1">
<tr><th><div align="center" class="tftable th">Questions</div></th><th colspan="2"><div align="center">Answer</div></th>
</tr>
<tr>
  <td>I like to dance</td>
  <td>
  <label></label>
  <label>
  <input name="bm1" type="radio" value="1" />
  Yes</label>
</td>
<td>
<label></label>
<label>
<input name="bm1" type="radio" value="2" />
No
</label>
</td></tr></table>
<input type="submit" name="submit" id="submit" value="Next">
<input type="hidden" name="passvalue" id="passvalue" value="echo $passvalue;">
</input></input></form>
</body></head></html>


And the script.js , I add document.getElementsById
JavaScript
function validateRadio()
{
// get all the inputs type
var inputs = myform.elements;
var radios = [];

// find the radio type
for (var i = 0; i < inputs.length; ++i) {
if (inputs[i].type == 'radio') {
radios.push(inputs[i]);
}
}
var countChecked = 0;
for (var j = 0; j < radios.length; j++) {
if (radios[j].checked) {
countChecked++;
}
}
//count number of radio button with value=1
var answeramount = 0;
for (var k = 0; k < radios.length; k++){
if(radios[k].checked && radios[k].value==1){
answeramount++;
}
}
if (countChecked != radios.length / 2){
alert("All questions must be answered.");
return false; // abort submit
} else {
document.getElementsById('passvalue').Value = answeramount;
return true; // proceed to submit
} 
}


I add check.php to print the passvalue
PHP
<?php $passvalue=$_POST['passvalue'];
echo $passvalue;
?>
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900