检索POST数据从Ajax调用PHP

问题描述:

三天过去了,仍然有问题得到这个工作的事情。 这Ajax调用我的JS文件似乎工作,当谈到发送JSON数据:

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;
                        }

但在我的PHP文件:

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}[]

我相信, json_de code()工作正常在我的PHP文件,但 $ _ POST [''] 无法从我的Ajax调用识别我发布的数据W / C变量已宣布:

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 + "'}",

我相信我做的事情与我的codeS似乎我已经在这里找到了很多问题,做什么,他们说的,但是不知道为什么发生错误。 是否有你所看到的任何问题/错误?请告诉我。

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.

u能使用萤火虫控制台中看到Ajax请求的数据?

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

您不能从$ _ POST获得姓氏,名字。它的JSON字符串内。所以,首先你必须使用来获取数据。

You cannot get the lastname, firstname from $_POST. Its inside the json string. So first you have to get the data using

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

然后去code。使用json_deocde并访问您的属性$的数据。

Then decode the $data using json_deocde and access your attributes.

json_decode($data);