Php 无法使用ajax传输json数据无法插入pdo语句并返回未定义的数据
我尽量简化代码,以便您可以重新创建场景。 我不明白为什么数据插入会失败,因为Php 无法使用ajax传输json数据无法插入pdo语句并返回未定义的数据,php,ajax,jquery,pdo,Php,Ajax,Jquery,Pdo,我尽量简化代码,以便您可以重新创建场景。 我不明白为什么数据插入会失败,因为$('qcategory').val()和$('qtype').val();都是简单的value=1。 ajax将始终返回带有未定义数据的错误。 我不明白json传入php后,数据哪里出错了 这是否意味着一旦json数据被传递到php中,就像我从php解码并返回它一样? 使用print$users->addQ($user)返回的数据具体是什么 我在这个问题上已经坚持了好几天了。我一定错过了一些非常重要的事情。请帮我弄清楚
$('qcategory').val()
和$('qtype').val()
;都是简单的value=1。
ajax将始终返回带有未定义数据的错误。
我不明白json传入php后,数据哪里出错了
这是否意味着一旦json数据被传递到php中,就像我从php解码并返回它一样?
使用print$users->addQ($user)
返回的数据具体是什么
我在这个问题上已经坚持了好几天了。我一定错过了一些非常重要的事情。请帮我弄清楚!我真的很感激你
如果您不理解这个问题,请告诉我您不清楚的地方。
如果是因为你无法解决问题,那么否决并没有真正的帮助。谢谢
quick\u controller.js
$(function() {
$(document).on("click", "input#done-add-question", function(){ addQuestion(this); });
});
function addQuestion(element) {
alert('add_question ran!');
$('#indicator').show();
var User = new Object();
User.qcategory = $('#qcategory').val();
User.qtype = $('#qtype').val();
$.ajax({
type: "POST",
url: ".../controller/quiz_controller.php",
dataType: "json",
data: {
//"page":page, //page: value in the url php : currentpage: value in js
action: 'add_question',
user: userJson
},
success: function(data, textStatus) {
alert('add_question works!');
$('#indicator').hide();
},//success: function(data) END
error: function(jqXHR, XMLHttpRequest, textStatus, errorThrown,data) {
alert('add question error!');
alert(" textStatus " + textStatus+" errorThrown " + errorThrown +" XMLHttpRequest " + XMLHttpRequest);
alert("now the data is "+data)
console.log('1'+jqXHR+" |t|t| " +textStatus+" |e|e| " + errorThrown +" |x|x| " + XMLHttpRequest);
console.log('data = '+data);
} //error END
});//$.ajax() END
}
quick\u controller.php
<?php
function __autoload($className){
$classNameUrl="../model/$className.php";
include_once($classNameUrl);
}
$users=new Quiz("localhost","us","pw","equizz");
/*$_POST['action'] ='add_question';
$_POST['user'] ="fs";*/
if(!isset($_POST['action'])) {
print json_encode(0);
print " empty! not ready!";
return;
}
if(get_magic_quotes_gpc()){
$userParams = stripslashes($_POST['user']);
} else {
$userParams = $_POST['user'];
}
switch($_POST['action']) {
case 'add_question':
$user = new stdClass;
$user = json_decode($userParams );
print $users->addQ($user);
break;
}
exit();?>
<?php
class Quiz {
private $dbh;
public function __construct($host,$user,$pass,$db) {
$this->dbh = new PDO("mysql:host=".$host.";dbname=".$db,$user,$pass);
}
public function addQ($user){
$sth = $this->dbh->prepare("INSERT INTO eq_question1(`qCatID`, `qTypID`) VALUES (?, ?)");
$sth->execute(array($user->qcategory, $user->qtype));
return json_encode($this->dbh->lastInsertId());
}
}//class Quiz END
?>
在您的小测验\u controller.js中,尝试以下操作:
$(function() {
$(document).on("click", "input#done-add-question", function(){ addQuestion(this); });
});
function addQuestion(element) {
var User = new Object();
User.qcategory = $('#qcategory').val();
User.qtype = $('#qtype').val();
var userJson = JSON.stringify(User);
$('#indicator').show();
$.post('controller/quiz_controller.php',
{
action: 'add_question',
user: userJson
},
$('#indicator').hide(),
"json"
);
}
在quick_controller.php中删除:
if(!isset($_POST['action'])) {
print json_encode(0);
print " empty! not ready!";
return;
}
if(get_magic_quotes_gpc()){
$userParams = stripslashes($_POST['user']);
} else {
$userParams = $_POST['user'];
}
替换为:
if(!isset($_POST['action'])) {
print json_encode(0);
return;
}
if(isset($_POST['user'])) {
$userParams = $_POST['user'];
}
if(!isset($_POST['action'])) {
print json_encode(0);
print " empty! not ready!";
return;
}
if(get_magic_quotes_gpc()){
$userParams = stripslashes($_POST['user']);
} else {
$userParams = $_POST['user'];
}
if(!isset($_POST['action'])) {
print json_encode(0);
return;
}
if(isset($_POST['user'])) {
$userParams = $_POST['user'];
}