从 AJAX 调用检索 POST 数据到 PHP
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8236903/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Retrieving POST Data from AJAX Call to PHP
提问by Noel Delos Santos Perez
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:
三天过去了,仍然有问题无法让这件事发挥作用。在发送 JSON 数据时,我的 js 文件上的这个 AJAX 调用似乎有效:
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:
但在我的 PHP 文件中:
$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:
我相信这json_decode()
在我的 php 文件中工作正常,但$_POST['']
无法识别我的 ajax 调用中发布的数据,w/c 变量已被声明:
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.
我相信我的代码做得对,似乎我在这里阅读了很多问题并完成了他们所说的但不知道为什么会发生错误。您是否遇到过任何问题/错误?请告诉我。
回答by Rakhitha Nimesh
Can you see the ajax request data using the firebug console?
你能用firebug控制台看到ajax请求数据吗?
You cannot get the lastname, firstname from $_POST. It's inside the json string. First, you have to get the data using
您无法从 $_POST 获取姓氏和名字。它在 json 字符串中。首先,您必须使用
$data = $_POST['data'] or $_REQUEST['data']
Then, decode the $data using json_deocde and access your attributes.
然后,使用 json_deocde 解码 $data 并访问您的属性。
json_decode($data);
回答by Lapi
$post = file_get_contents('php://input');
回答by Hymanson Harry
instead of this
而不是这个
data: "{'lastName':'" + _lname + "','firstName':'" + _fname +
"','middleName':'" + _mname + "'}",
use this
用这个
data: {lastName:_lname,firstName:_fname,middleName:_mname},