我试图通过单击按钮将JS数组发送到.php文件,并使用发送的数组在单击时运行php文件的代码。
我有一个按钮:
<button id="submit" class="button1" >Submit<span></span></button>
我尝试使用以下命令运行此JS ajax:
$('#submit').click(function() {
$.ajax({
method: "POST",
url: "submit.php",
data: selectedImageArray
}).done(function( msg ) {
alert( "Data Saved: " + msg );
});
});
在我的Submit.php文件中,我连接到数据库,然后执行以下操作:
$array = $_POST['selectedImageArray'];
$sql = $dbh->prepare("INSERT INTO pending_trades (steam_id, trade_items, is_giving, is_receiving, has_started) VALUES (:steamid, :itemlist, '1', '0', '0')");
$sql->bindParam(':steamid', $steamprofile['steamid']);
$sql->bindParam(':itemlist', $array);
$sql->execute();
}
当我单击“提交”按钮时,出现消息“数据已保存”,但是数据库没有任何反应。是否应该单击按钮运行Submit.php代码?
最佳答案
$('#submit').click(function(){
$.ajax({
method: "POST",
url: "submit.php",
data: {selectedImageArray: selectedImageArray}, // this is supposed to be JSON
.done(function( msg ) {
alert( "Data Saved: " + msg );
});
});
});
和
$_POST['selectedImageArray'] // see the quotes