Resolved: I had $_SERVER['REQUEST_METHOD'] != 'post' as lowercase. That has to be capital apparently... It worked as soon as I capitalized it, and that was the only change. I voted you up for you fine chaps who helped. Thanks!
I'm generating a submit button after a simple math question with Ajax. The Ajax works fine. I can verify with var_dump/print_r that the $_POST and the $_SESSION data is being passed correctly. The submit button is going in the output div like I would expect.
However, when I press the submit button, it reloads the page but it does not trigger a submit. It goes back to the form and the $_POST variable is empty / not set at all.
The submit button is in the form tags. Why is it submitting the form but not the data / how do I get it to work?
I don't think you need the code to answer the question, but nonetheless, here is a simplified version of the code:
// In a server request method block on test.php:
if( $_SERVER['REQUEST_METHOD'] != 'POST' ){
<form id="addUserId" name="addUser" method="post" action="test.php" >
<table id="formCreatoraddUserIdTableId" cellpadding="2" cellspacing="2" width="100%" >
<tr id="formCreatorRowId0" >
<td valign="top" ><font>3 minus 1: </font></td>
<td valign="top" ><input type="text" id="validatorId" name="validate" onkeyup="makeAjaxRequest('./ajax/captchaSubmitButtonCheck.php', 'captcha='+encodeURIComponent(document.getElementById('validatorId').value),'captchaOutputDiv', '1');" autocomplete="off" />
</td>
</tr>
<tr id="formCreatorRowId1" >
<td valign="top" ><font></font></td>
<td valign="top" >
<div id="captchaOutputDiv" ></div>
</td>
</tr>
</table>
<input type="hidden" name="refreshPostPrevention" />
</form>
}else{
print_r( $_POST ); // will be empty
}
// in an ajax file captchaSubmitButtonCheck.php:
<?PHP
if( $_SERVER['REQUEST_METHOD'] == 'POST' ){
printr( $_POST );
printr( $_SESSION, 'SESSION' );
if( $_POST['captcha'] == $_SESSION['captcha'] ){
echo '<input type="submit" value="Save" />';
}else{
echo '<font class="red">Your math was wrong. Try again.</font>';
}
}
?>
// the JS
function makeAjaxRequest(url, params, output, image) {
var getdate = new Date(); //Used to prevent caching during ajax call
if( image == 1 ){
document.getElementById(output).innerHTML='<img src="./images/template/ajax-loader.gif"></img>';
}
if(xmlhttp) {
xmlhttp.open("POST",url,true);
xmlhttp.onreadystatechange = function(){
if (xmlhttp.readyState == 4) {
if(xmlhttp.status == 200) {
document.getElementById(output).innerHTML=xmlhttp.responseText; //Update the HTML Form element
}
else {
alert("Error during AJAX call. Please try again");
}
}
}
xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xmlhttp.send(params); //Posting txtname to PHP File
}
}