[英]How to send data multiple same name input using ajax and php
I am trying to make a poll system for my project like facebook. 我正在尝试为像Facebook这样的项目创建一个投票系统。 But I need help in a matter. 但我需要帮助。 I use the following JavaScript code to increase the answers for inputs. 我使用以下JavaScript代码来增加输入的答案。
$("body").on("click",".createnew", function(){
$(".inputsl").append("<div class='inputt'><input type='text' name='myanswer' id='createmyanswer' class='myinput' placeholder='Write answer!'></div>");
});
When you click on the CreateNew button users can prompted to write a new answer. 当您单击CreateNew按钮时,可以提示用户编写新答案。 Like this: 像这样:
<div class="inputsl">
<div class="inputt"><input type="text" id="createmyanswer" name="myanswer" class="myinput"></div>
<div class="inputt"><input type="text" id="createmyanswer" name="myanswer" class="myinput"></div>
<div class="inputt"><input type="text" id="createmyanswer" name="myanswer" class="myinput"></div>
<div class="inputt"><input type="text" id="createmyanswer" name="myanswer" class="myinput"></div>
</div>
So you can see all input name and id is same. 这样就可以看到所有输入名称和ID相同。 It is easy to send one input value. 发送一个输入值很容易。 But I want to give the user the right to ask more than one question. 但是我想赋予用户提出多个问题的权利。
For this I used the following ajax code. 为此,我使用了以下ajax代码。
$("body").on("click",".insertp", function(){
var answers = $("#createmyanswer").val();
var dataPollAnswers = 'answers=' + answers;
$.ajax({
type:'POST',
url:'/requests/postPollAnswers',
data: dataPollAnswers,
cache: false,
beforeSend: function(){},
sucess: function(){
console.log("Success!");
}
});
});
The last think is php codes for postPollAnswers. 最后想到的是postPollAnswers的php代码。 I have used the following php codes for sending all created answers. 我已使用以下php代码发送所有创建的答案。
<?php
include_once '../inc/inc.php';
if(isset($_POST['answers'])){
$answers = mysqli_real_escape_string($db, $_POST['answers']);
if($answers){
foreach($answers as $setAnswer){
$insertAnswersfromData = $InSert->Insert_Poll($uid, $setAnswer);
}
}
}
?>
I think i have array problem i have searched a solution and tryed many thinks but i can not send multiple answers. 我认为我有数组问题,我已经搜索了一个解决方案并尝试了许多想法,但是我无法发送多个答案。 I have checked also maybe i need some jquery code like serialize() ect. 我已经检查过,也许我需要一些jquery代码,例如serialize()等。 and tryed but i can not get any result. 并尝试,但我无法获得任何结果。
Also i am getting this warning: 我也收到此警告:
Warning: Invalid argument supplied for foreach() 警告:为foreach()提供了无效的参数
Anyone can help me here please ? 有人可以帮我吗?
Just change selector to class name
and use serialize
只需将选择器更改为class name
并使用serialize
dataPollAnswers = $('.myinput').serialize();
Note : id should be unique 注意:id应该是唯一的
Update 1: 更新1:
PHP : PHP的:
foreach($_POST['myanswer'] as $row)
{ ^^^^^^^^^^
echo $row;
}
Note : Input name is myanswer
not answer 注意:输入名称是myanswer
答案,不是答案
Update 2: 更新2:
PHP PHP
<?php
include_once '../inc/inc.php';
if(isset($_POST['myanswer'])){
foreach($_POST['myanswer'] as $setAnswer)
{
if(!empty($setAnswer))
{
$new_set_ans = mysqli_real_escape_string($db, $setAnswer);
$insertAnswersfromData = $InSert->Insert_Poll($uid, $new_set_ans);
}
}
}
?>
just take name attribute as array and their id's should be unique: 只需将name属性作为数组,并且其id应该是唯一的:
var i;
$("body").on("click",".createnew", function(){
$(".inputsl").append("<div class='inputt'><input type='text' name='myanswer[]' id='createmyanswer'"+i+" class='myinput' placeholder='Write answer!'></div>");
i++;//no needed if you dont want to use id attribute
});
Then post as usual using name attribute and you will get array at server side . 然后像往常一样使用name属性发布,您将在服务器端获得数组。
Thanks 谢谢
Here is working demo for you: 这是为您工作的演示:
$("#submit").click(function(){ var paramsToSend = {}; var i = 1; $("input[name='myanswer[]']").each(function(){ paramsToSend[i] = $(this).val(); i++; }); $("#dataToSend").html(JSON.stringify(paramsToSend)); $.ajax({ type: "POST", url: 'URL_HERE', data: {params:JSON.stringify(paramsToSend)}, success: function(data) { console.log("SUCCESS!!!"); } }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="inputsl"> <div class="inputt"><input type="text" id="createmyanswer" name="myanswer[]" class="myinput"></div> <div class="inputt"><input type="text" id="createmyanswer" name="myanswer[]" class="myinput"></div> <div class="inputt"><input type="text" id="createmyanswer" name="myanswer[]" class="myinput"></div> <div class="inputt"><input type="text" id="createmyanswer" name="myanswer[]" class="myinput"></div> </div> <button id="submit"> Submit </button> <div id="dataToSend"></div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.