Page 1 of 1

Passing value from Javascript to PHP

Posted: Mon Apr 14, 2014 10:50 am
by monsterchub
I tried some solution, so I have changed the some code. I want to pass answeramount value. Still got error undefined variable. What did i miss?
This is body.php, I add the hidden field and submit button.

Code: Select all

<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

Code: Select all

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

Code: Select all

<?php $passvalue=$_POST['passvalue'];
echo $passvalue;
?>

Re: Passing value from Javascript to PHP

Posted: Mon Apr 14, 2014 10:53 am
by Christopher
It probably be:

Code: Select all

<input type="hidden" name="passvalue" id="passvalue" value="<?php echo $passvalue; ?>">

Re: Passing value from Javascript to PHP

Posted: Mon Apr 14, 2014 11:36 am
by monsterchub
I've tried value="<?php echo $passvalue; ?>">
Still not working

Re: Passing value from Javascript to PHP

Posted: Mon Apr 14, 2014 2:17 pm
by Christopher
Does the page have a .html or .php extension? If needs a .php extension. If you view source and look at the HTML generated, what is the value of the hidden input named passvalue?

Re: Passing value from Javascript to PHP

Posted: Mon Apr 14, 2014 2:22 pm
by Celauran
I don't see any included files and I don't see $passvalue having been defined anywhere.

Re: Passing value from Javascript to PHP

Posted: Mon Apr 14, 2014 11:59 pm
by monsterchub
I'm trying to pass the "answeramount value" to php... I don't know the proper way to do that... Maybe you can help me out.

Re: Passing value from Javascript to PHP

Posted: Tue Apr 15, 2014 5:25 pm
by Christopher
It should be:

Code: Select all

document.getElementById('passvalue').value = answeramount;