Getting POST data from an AJAX call in PHP

Three days have passed and there are still problems to make it all work. This AJAX call in my js file seems to work when it comes to sending JSON data:

var _lname = $('#ptLastName').val(); var _fname = $('#ptFirstName').val(); var _mname = $('#ptMiddleName').val(); var _gender = $('#ptGender').val(); var _bday = $('input[name="birthdate"]').val(); // $('#ptBirthDate').val(); var _ssn = $('#ptSSN').val(); $.ajax({ type: "POST", url: ".././CheckPerson.php", data: "{'lastName':'" + _lname + "','firstName':'" + _fname + "','middleName':'" + _mname + "'}", contentType: "application/json; charset=utf-8", dataType: "json", success: function (response) { var res = response.d; if (res == true) { jAlert('Person Name already exists!', 'Error'); return; } }) 

but in my php file:

 $lastname = json_decode($_POST['lastName']); $firstname = json_decode($_POST['firstName']); $middlename = json_decode($_POST['middleName']); $response = array(); mysql_connect ("*****", "****") or die ('Error: ' . mysql_error()); mysql_select_db ("********"); $query = "SELECT Lastname, Firstname, MiddleName FROM tbl_people WHERE Lastname = '$lastname' || Firstname = '$firstname' || MiddleName = '$middlename'"; $result = mysql_query($query); $row = mysql_fetch_array($result); if ($row) { $response = json_encode(array('d' => true, 'test' => $lastname)); } else { $response = json_encode(array('d' => false, 'test' => $lastname)); } echo $response; print json_encode($_POST); 

some error from the firebug console says:

 <br /> <b>Notice</b>: Undefined index: lastName in <b>C:\xampp\htdocs\..\CheckPerson.php</b> on line <b>2</b><br /> <br /> <b>Notice</b>: Undefined index: firstName in <b>C:\xampp\htdocs\..\CheckPerson.php</b> on line <b>3</b><br /> <br /> <b>Notice</b>: Undefined index: middleName in <b>C:\xampp\htdocs\..\CheckPerson.php</b> on line <b>4</b><br /> {"d":false,"test":null}[] 

I believe json_decode() works fine in my php file, but $_POST[''] cannot recognize my published data from my ajax call to w / c variables:

 data: "{'lastName':'" + _lname + "','firstName':'" + _fname + "','middleName':'" + _mname + "'}", 

I believe that I am doing everything right with my codes, it seems that I read a lot of questions here and did what they said, but I don’t know why the error occurred. Is there a problem / error you saw? please tell me.

+9
source share
3 answers

Can I view ajax request data using firebug console?

You cannot get the last name, name from $ _POST. Its inside json string. Therefore, you must first obtain the data using

  $data = $_POST['data'] or $_REQUEST['data'] 

Then decode $ data with json_deocde and get access to your attributes.

 json_decode($data); 
+8
source
 $post = file_get_contents('php://input'); 
+6
source

instead of this

 data: "{'lastName':'" + _lname + "','firstName':'" + _fname + "','middleName':'" + _mname + "'}", 

use this

 data: {lastName:_lname,firstName:_fname,middleName:_mname}, 
0
source

Source: https://habr.com/ru/post/1382627/


All Articles