10

Three days had passed and still having problems to get this things work. This AJAX Call on my js file seems working 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 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 that json_decode() is working fine in my php file but $_POST[''] can't recognize my posted data from my ajax call w/c variables had been declared:

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

I believe I am doing right with my codes seems i had read many questions here and done what they had said but don't know why the error occurred. Is there any problem/bug you had seen? please tell me.

JH_
  • 406
  • 1
  • 4
  • 15
  • Does this answer your question? ["Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-and-notice-undefined) – Nico Haase May 27 '20 at 14:39

4 Answers4

13

Can you see the ajax request data using the firebug console?

You cannot get the lastname, firstname from $_POST. It's inside the json string. First, you have to get the data using

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

Then, decode the $data using json_decode() and access your attributes.

json_decode($data);
Syscall
  • 19,327
  • 10
  • 37
  • 52
Rakhitha Nimesh
  • 1,393
  • 2
  • 16
  • 26
  • I can see it. It displays the json data on the firebag console.I think ajax works fine with that. I want to upload the screenshot of it, for you guys to see it but my reputation is low for me to have the privilege. json data shows that: firstName "inputtedfirstname" lastName "inputtedlastname" middleName "inputtedmiddlename" but my php file can't get those values using json_decode($_POST['lastName']). where do you think is the problem. – Noel Delos Santos Perez Nov 23 '11 at 04:57
  • I'd tried it, but it also returned 'data' as Undefined index. I really don't why '$_POST' can't get the posted data. I believe that it should even read the attributes, but it didn't. I'm really having a bad time in here. – Noel Delos Santos Perez Nov 23 '11 at 11:39
  • 1
    $.post('.././CheckPerson.php', {data: dataString}, function(res){ }); try sending data using this method – Rakhitha Nimesh Nov 23 '11 at 11:43
  • It just work. I can now access the data in my php file. But I think I have to delete my ajax function now and start coding again, since those codes doesn't work for me or maybe I just can't make it work. thanks a lot. – Noel Delos Santos Perez Nov 23 '11 at 12:21
  • Did my last option worked? $.post('.././CheckPerson.php', {data: dataString}, function(res){ }); – Rakhitha Nimesh Nov 23 '11 at 16:51
  • 1
    Yup. I've tried $.post('.././CheckPerson.php', {data: dataString}, function(res){ }); just like what you had said. But now the data is not json data. I'm still looking for ways if I can use json data returned by ajax in my php file, but I think i'm good with $.post(); for now. Thanks again for the help. – Noel Delos Santos Perez Nov 24 '11 at 05:20
10
$post = file_get_contents('php://input');
Natalie Hedström
  • 2,607
  • 3
  • 25
  • 36
Lapi
  • 119
  • 1
  • 2
0

instead of this

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

use this

data: {lastName:_lname,firstName:_fname,middleName:_mname},
Jackson Harry
  • 308
  • 1
  • 2
  • 15
0

try with this solution

$lastname = isset($_POST['lastName'])?json_decode($_POST['lastName']):null; $firstname =isset($_POST['firstname'])?json_decode($_POST['firstname']):null; $middlename=isset($_POST['middlename'])?json_decode($_POST['middlename']):null;

badr
  • 1
  • 1