我对这个东西非常陌生,到目前为止,当我只传递一个$_POST
变量时,我就可以让它正常工作了。
但是,在本例中,我试图传递2个变量,由于某种原因,我得到了一个Internal Server Error
。现在我已经做了一些跟踪,这肯定不是SQL错误。我确信错误来自Ajax部分,在Ajax部分,我要传递帖子,但我不知道自己做错了什么。有人能帮忙吗?
这里是我的Ajax代码:
$('.class_checkbox').on('click',function(){
var split = this.id.split('_');
var catID = split[0];
var myData = 'catToInsert='+ $id; //build a post data structure
var categoryId = 'category='+catID;
jQuery.ajax({
type: "POST", // HTTP method POST or GET
url: "includes/category.php", //Where to make Ajax calls
dataType:"html", // Data type, HTML, json etc.
data:{myData: myData, categoryId: categoryId}, //Form variables
success:function(response){
alert(response);
},
error:function (xhr, ajaxOptions, thrownError){
//On error, we alert user
alert(thrownError);
}
});
$(this).toggleClass('checked').prev().prop('checked',$(this).is('.checked'));
});
,这是我的PHP:
<?php
include_once('config.php');
$id = $_POST['catToInsert'];
$catID = $_POST['category'];
//Create PDO Object
$con = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
//Set Error Handling for PDO
$con->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
//Query
$sql = "SELECT category_id from product_category WHERE product_id=$id";
//Prepare Statement
$stmt = $con->prepare($sql);
$stmt->execute();
$result = $stmt->fetchAll();
$array = array_map('current', $result);
echo $id;
?>
发布于 2015-10-29 14:01:43
正如@kingkero所说,myData
和categoryId
将作为您在$_POST中的密钥。jquery对您的帖子所做的事情如下所示:
$_POST[categoryId]= "categoryId=someIDofYours"
因此,您需要删除var categoryId
中的连接并将catID
直接传递到数据中,如下所示:
data:{'categoryId':catID}
https://stackoverflow.com/questions/33416152
复制相似问题