BobRodes BobRodes - 1 year ago 95
jQuery Question

PHP isset() not recognizing $_POST variables, when var_dump does?

I'm trying to pass post variables back to the same page, and I can't seem to get

to recognize them. Here's a test page:

<?php include ('includes/masterheaderfiles.php'); ?>

<script type="text/javascript" language="JavaScript">
$(document).ready( function() {
$('button').on('click', function(e) {
var theForm = document.forms['frmTest'];
theForm.hdnTest.value =,1);


echo 'hdnTest = '; var_dump( $_POST['hdnTest']); echo '<br>';
echo 'foo = '; var_dump( $_POST['foo']); echo '<br>';

echo ' hdnTest is ' . (isset($POST_['hdnTest']) ? '' : 'NOT ') . 'set<br>';
echo ' foo is ' . (isset($POST_['foo']) ? '' : 'NOT ') . 'set';

<form name="frmTest" action="test.php" method="post">
<input type="text" name="hdnTest">
<button id='b0'>Zero</button>
<button id='b1'>One</button>

I have two buttons, one of which enters a zero in a text box and the other of which enters a one. If I push the "one" button, I get this feedback:

hdnTest = string(1) "1"
foo = NULL
hdnTest is NOT set
foo is NOT set

If I push the zero button, I get the same, except the var_dump shows a value of "0", as expected. In other words, the var_dump is showing that the variable is being posted to the page, while
isn't recognizing it as set.

I've stepped through the javascript/jQuery code up to the point the form gets submitted, and the hidden input element is indeed being set to the value specified by the click event handler. I've also unhid the text box, and when I click a button, I can see the appropriate value get plugged in, stay for a half second, and disappear when the var_dump value gets updated. That suggests to me that the form is being properly submitted.

Can someone explain these results? Why isn't the variable set, when it's there and has a value?

Answer Source

You have typos in your code for your isset calls

     ^ should be $_POST