English 中文(简体)
从AJAX呼声到PHP检索POST数据
原标题:Retrieving POST Data from AJAX Call to PHP
  • 时间:2011-11-23 03:15:14
  •  标签:
  • php
  • ajax
  • 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;
          } 
})

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

焚烧炉的一些错误是:

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

i 相信json_decode(>>)正在我的实验室档案中工作,但$_POST[ ] 不能承认我从我的警示中公布的数据。

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.

最佳回答

您能否通过火焰虫获得亚克斯要求的数据?

不能获得最后名称,第一个名字来自<代码>_POST。 它在 j子里。 首先,必须利用数据获取数据。

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

然后,使用<代码>数据,并查阅你的属性。

json_decode($data);
问题回答
$post = file_get_contents( php://input );

而不是

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

使用

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

尝试这一解决办法

$lastname = isset ($_POST[lastName])?json_decode ($_POST[lastName]):null;$firstname =isset ($_POST[Firstname])?json_decode($_POST[第1名]):null;$middlename=isset($_POST[中名]?json_decode($_POST[中名]):null;





相关问题
ajax login using httpRequest?

I am trying to develop my login script to give feedback to the user if the login is valid or not. Basically if it isn t correct a div box will show saying its wrong, if its correct it will show its ...

Virtual Tour using sketch up, ajax, flash technologies

I want to know if there are existing technology that make your 3d models in sketch into virtual tours, using either Ajax or Flash for web presentation. If there s none, which will be a good approach ...

How can i update div continuously

I have asp.net application where i have a div which showing the value from other site. The value of that site is changing continuously. I want that my div will automatically update in some interval ...

热门标签