jazcam jazcam - 1 year ago 132
Javascript Question

post not working in php function, even though var_dump in same function shows the data

I'm befuddled. I have a javascript function that posts to a php function. PHP says, "undefined index" on mPicker. Yet if I call var_dump within the same function mPicker is plainly visible.


I've also ran an alert client side in javascript to be sure the form data was serialized, and it too shows that mPicker indeed has a value. Yet this line in php returns the error:


This is just short hand for all your sake. The longer version of the code tests for sql injection.

And the error in xdebug:

php error in xdebug

The javascript code:

$.post("./php/adates.php", { atype: apttype, data: $("#apptForm").serialize() })
.done(function(data) {

And more of the php function code:

if (isset($_POST["atype"]) && !empty($_POST["atype"])) {
$typ = test_input($_POST['atype'], $con);
} else {
echo "error ln 6: typ is undefined.";

echo $es;

"test_input" is the function I mentioned that tests for sql injection, and just for testing, have omitted temporarily on the post on mPicker. As you can see, the line $_POST["atype"] escapes error, and is perfectly resolved in the php function. I know I am tired and must be missing something stupid. Help, anyone!

Answer Source

try to use jQuery "ajax" instead of "post"

  type: 'POST',
  url: url,
  data: data,
  dataType: "html",
  success: function(html){


and set the dataType to "html", then use "echo" or "var_dunmp" in php, you can see ur data in console

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download