I am trying to make a poll system for my project like facebook. But I need help in a matter. I use the following JavaScript code to increase the answers for inputs.
$("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. 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. 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.
$("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. I have used the following php codes for sending all created answers.
<?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. and tryed but i can not get any result.
Also i am getting this warning:
Warning: Invalid argument supplied for foreach()
Anyone can help me here please ?
idfor more than one element, they are supposed to be used to identify a single HTML element, hence their name. I believe$("#createmyanswer").val();will only return the value of the first or last#createmyanswerelement.Warning: Invalid argument supplied for foreach()