[英]How can i sent an array with JS and AJAX
I have multiple input fields where a user can fill in answers to the question, with JavaScript i put the user input into a array. 我有多个输入字段,用户可以在其中填写问题的答案,并使用JavaScript将用户输入内容放入数组中。
When I view the console log inside google chrome I see the array wil be made correct, after that I want to sent the array with AJAX to a another php page so that i can save the data into my database. 当我查看google chrome中的控制台日志时,我看到该数组将被正确设置,此后,我想将带有AJAX的数组发送到另一个php页面,以便我可以将数据保存到我的数据库中。
When i check my database the post result = "Array" :( 当我检查数据库时,发布结果=“ Array” :(
someone who can help me??? 可以帮助我的人???
////// JavaScript and AJAX code : /////// JavaScript和AJAX代码:
if (isConfirm) {
checkAnswers();
$('input[id="answer"]').each(function() {
allAnswers.push($(this).val());
allAnswers.toString();
});
console.log(allAnswers);
$.ajax({
type: 'post',
url: 'actions/save_questions.php',
data: { 'answers': allAnswers }
});
}
/////// PHP code : /////// PHP代码:
<?php
$user_answer = $_POST['answers'];
$date = date('d-m-Y h:i:s');
$arr = array($user_answer);
$query = "INSERT INTO made_questions (lesson_id, user_answer, datum) VALUES ('1', '$arr', '$date')";
mysqli_query($conn,$query) or die (mysqli_error($conn));
?>
The output into my database is: 输出到我的数据库是:
id: 27 id:27
lesson_id: 1 lesson_id:1
user_answer: Array <----- this is wrong :( user_answer: 数组 <-----这是错误的:(
datum: 16-06-2016 08:12:32 基准:16-06-2016 08:12:32
Console log output: 控制台日志输出:
["horse", "nocleu", "Geel", "This is my dog", "Guten tag", "Sun", "Aap", "Honger", "knol", "Cat"] [“马”,“ nocleu”,“ Geel”,“这是我的狗”,“ Guten标签”,“ Sun”,“ Aap”,“ Honger”,“ knol”,“ Cat”
Someone who maybe can help me??? 也许可以帮助我的人???
Thanks a lot! 非常感谢!
.toString()
and .join()
do not modify the original variable . .toString()
和.join()
不会修改原始变量 。 You need to either say: 您需要说:
allAnswers = allAnswers.toString();
OR 要么
data: {'answers': allAnswers.toString()}
If you want the values ", " separated rather than just "," which is what is returned by .toString()
you can say: 如果您想要值“,”分隔而不是仅“。” .toString()
。 .toString()
返回的值.toString()
您可以说:
allAnswers = allAnswers.join(", ");
OR 要么
data: {'answers': allAnswers.join(", ");
It is important to note that your .each() loop will break if you use .toString() in that way. 重要的是要注意,如果以这种方式使用.toString(),则.each()循环将中断。
$('input[id="answer"]').each(function(){
allAnswers.push($(this).val());
allAnswers = allAnswers.toString();
});
The above code will not work because after the first iteration, allAnswers will be a string, therefore, calling .push()
will throw an exception because .push()
is not a function of string
. 上面的代码不起作用,因为在第一次迭代之后,allAnswers将是一个字符串,因此,调用.push()
将引发异常,因为.push()
不是string
的函数。
$('input[id="answer"]').each(function(){
allAnswers.push($(this).val());
});
allAnswers = allAnswers.toString();
This code fixes the issue by converting the array to a string after you are done appending values to the array. 在将值添加到数组后,此代码通过将数组转换为字符串来解决此问题。
if you want all questions saved in string format, you can use json_encode 如果您希望所有问题以字符串格式保存,则可以使用json_encode
<?php
$user_answer = $_POST['answers'];
$date = date('d-m-Y h:i:s');
$ques = json_encode($user_answer);
$query = "INSERT INTO made_questions (lesson_id, user_answer, datum) VALUES ('1', '$ques', '$date')";
mysqli_query($conn,$query) or die (mysqli_error($conn));
?>
warning : your code is not safe ! 警告:您的代码不安全!
在sql中将其串联之前,应将数组转换为字符串。
$arr = implode(",", $user_answer);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.