我想将username
和password
传递给php脚本并检入数据库。在客户端,我使用以下脚本生成json object
并将其发布到php文件。
var myobj = {};
myobj["usrname"]= $( "#customer option:selected" ).text();
myobj["usrpass"]= $("#psw").val();
var myjson = JSON.stringify(myobj);
$.ajax({
method: "POST",
url: "checkpass.php",
data: myjson
})
.done(function( msg ) {
alert( msg );
});
在服务器端,当我在firebug
中看到时,帖子将作为
Parametersapplication / x-www-form-urlencoded不排序 { “usrname”:“XXXXXXX ...
JSONusrname “XX”
usrpass “去做就对了” 资源 { “usrname”: “XXX”, “usrpass”: “justdoit”}
然而,当我运行php脚本来检查查询时,它会返回错误
$usrname = $_POST['usrname'];
$usrpass = $_POST['usrpass'];
$sql = "select count(*) from glusers where EmpName='$usrname' and EmpPass='$usrpass'";
$result = $conn->query($sql);
if($result >0){
$output = 'Success';
} else
{
$output = 'fail';
}
我已经尝试了所有帖子,但无法让它发挥作用。
提前致谢。
此致
答案 0 :(得分:2)
为了使ajax获得成功事件而回应并死亡该声明
Js档案
var myobj = {};
myobj["usrname"] = 'myUsername';
myobj["usrpass"] = 'myPassword';
$.ajax({
type: "post",
url: "url",
dataType: "json",
data: {post_data: myobj},
contentType: "application/x-www-form-urlencoded",
success: function (responseData) {
console.log(responseData);
},
error: function (errorThrown) {
console.log(errorThrown);
}
});
PHP操作文件
/** if we print post we will get the following array * */
//print_r($_Post);
//die()
//Array
//(
// [post_data] => Array
// (
// [usrname] => myUsername
// [usrpass] => myPassword
// )
//
//)
if (isset($_Post['post_data'])) {
$myPost = $_Post['post_data'];
$usrname = $myPost['usrname'];
$usrpass = $myPost['usrpass'];
$sql = "select count(*) from glusers where EmpName='$usrname' and EmpPass='$usrpass'";
$result = $conn->query($sql);
$num_row = $result->num_rows;
if ($num_row > 0) {
$output = 'Success';
} else {
$output = 'fail';
}
echo json_encode($output);
die();
}
答案 1 :(得分:1)
试试这个: 在js文件中:
$(document).on("ready", function(){
// Create an object using an object literal.
var ourObj = {};
// Create a string member called "data" and give it a string.
// Also create an array of simple object literals for our object.
ourObj.data = "Some Data Points";
ourObj.arPoints = [{'x':1, 'y': 2},{'x': 2.3, 'y': 3.3},{'x': -1, 'y': -4}];
var savedata = JSON.stringify(ourObj)
$.ajax({
type:"POST",
url:"Users.php",
data: {"points" : JSON.stringify(ourObj)},
success: function(data) {
// Do something with data that came back.
alert(data);
}
})
});
在PHP文件中:
if (isset($_POST["points"])) {
$points = json_decode($_POST["points"]);
echo "Data is: " . $points->data . "<br>";
echo "Point 1: " . $points->arPoints[0]->x . ", " . $points->arPoints[0]->y;
}
答案 2 :(得分:0)
试试这个:
var myobj = '{
usrname:'+$( "#customer option:selected" ).text()+',
usrpass:'+$("#psw").val()+'
}';
或
var myobj = {};
myobj.usrname= $( "#customer option:selected" ).text();
myobj.usrpass= $("#psw").val();
答案 3 :(得分:0)
使用Json2库,如下所示,
var myobj = {};
myobj["usrname"]= $( "#customer option:selected" ).text();
myobj["usrpass"]= $("#psw").val();
var myjson = JSON2.stringify(myobj);
$.ajax({
method: "POST",
url: "checkpass.php",
data: myjson
})
.done(function( msg ) {
alert( msg );
});
答案 4 :(得分:0)
$ data = json_decode(file_get_contents(&#39; php:// input&#39;),true);
现在$ data是用户名和密码的字典数组。在传递查询之前还要清理数据以避免sql注入。