jazcam
jazcam

Reputation: 135

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.

console.log

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:

$es=$_POST["mPicker"];

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) {
    alert(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.";
}

$es=$_POST["mPicker"];
echo $es;
exit;

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!

Upvotes: 0

Views: 111

Answers (1)

joker szeto
joker szeto

Reputation: 84

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

$.ajax({
  type: 'POST',
  url: url,
  data: data,
  dataType: "html",
  success: function(html){
     console.log(html)
  }

});

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

Upvotes: 1

Related Questions