我正在尝试将一些数据上传到我的数据库,这些数据是我在processing.js和表单中从游戏中收集的。按下表单上的提交按钮后,数据将发送到数据库。 我的问题是我无法从我的processing.js中提取String数组并将其发送到数据库。
processing.js中数组的名称是answerArray。
我的HTML:
<form action="submitted.php" method="post" onsubmit="return js_sendData()">
<input name="email" placeholder="Mail" type="text" class="text" />
</div>
<div>
<input name="name" placeholder="Full Name" type="text" class="text" />
</div>
<input type="submit" value="I solved it!" class="button">
</form>
<script language="JavaScript" type="text/javascript">
function js_sendData() {
$.post( "save.php", {'answer[]': answerArray}, function( data ) {
$( "#status" ).html( data );});
return true;
}
</script>
我的php(submitted.php):
//连接数据库
$con=mysqli_connect(connection details*);
//Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$name = $_POST['name'];
$email = $_POST['email'];
mysqli_query($con,"INSERT INTO Answer (Name, Mail)
VALUES ('$name', '$email')");
保存不起作用的php(save.php):
// Connect to the database
$con=mysqli_connect(connection details*);
//Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$dta = $_POST['answer'];
mysqli_query($con,"UPDATE Answer
SET Correct = '$dta[0]', wordOne = '$dta[1]' ORDER BY saveID DESC LIMIT 1" );
代码正确上传了html表单详细信息(邮件和电子邮件),但不会为Correct和wordOne上传。
答案 0 :(得分:0)
您必须使用 JSON.stringify() 函数在php中发送数组:
<script language="JavaScript" type="text/javascript">
function js_sendData() {
var answerToSend=JSON.stringify(answerArray);
$.post( "save.php", {'answer': answerToSend}, function( data ) {
$( "#status" ).html( data );
},'json');
return true;
}
</script>
然后在你的php中:
$dta= json_decode(stripslashes($_POST['answer']));