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数据时,AJAX调用我的js文件似乎很有效:
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:
firebug控制台的一些错误提示:
<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.
我相信我对我的代码做得很好,似乎我在这里读了很多问题,做了他们所说的,但不知道为什么会发生错误。你看到什么问题了吗?请告诉我。
2 个解决方案
#1
6
Can u see the ajax request data using the firebug console ?
可以使用firebug控制台查看ajax请求数据吗?
You cannot get the lastname, firstname from $_POST. Its inside the json string. So first you have to get the data using
您无法从$_POST获得lastname、firstname。它位于json字符串内。首先,你需要使用数据
$data = $_POST['data'] or $_REQUEST['data']
Then decode the $data using json_deocde and access your attributes.
然后使用json_deocde解码$数据并访问属性。
json_decode($data);
#2
2
$post = file_get_contents('php://input');
#1
6
Can u see the ajax request data using the firebug console ?
可以使用firebug控制台查看ajax请求数据吗?
You cannot get the lastname, firstname from $_POST. Its inside the json string. So first you have to get the data using
您无法从$_POST获得lastname、firstname。它位于json字符串内。首先,你需要使用数据
$data = $_POST['data'] or $_REQUEST['data']
Then decode the $data using json_deocde and access your attributes.
然后使用json_deocde解码$数据并访问属性。
json_decode($data);
#2
2
$post = file_get_contents('php://input');