0

I am trying to make a poll system using php, mysql and ajax. I have created poll table from database.

id
answer
vote

I want users to increase their answers and i have created a simple javascript function like this DEMO

<div class="inputsl">
   <div class="inputt"><input type="text" id="createPoll" name="mypoll" class="myinput"></div>
   <div class="inputt"><input type="text" id="createPoll" name="mypoll" class="myinput"></div>
   <div class="inputt"><input type="text" id="createPoll" name="mypoll" class="myinput"></div>
   <div class="inputt"><input type="text" id="createPoll" name="mypoll" class="myinput"></div>
</div>

I have also created ajax function for inserting answers

$("body").on("click",".insertp", function(){
       var poll = $("#createPoll").val();
       var dataPoll = 'poll=' + poll;
       $.ajax({
           type:'POST',
           url:'/requests/postPoll',
           data: dataPoll,
           cache: false,
           beforeSend: function(){},
           sucess: function(){
               console.log("Success!");
           }   
        });
     });

and the postPoll.php functions

<?php 
include_once '../inc/inc.php';
if(isset($_POST['poll'])){
    $poll = mysqli_real_escape_string($db, $_POST['poll']);
    if($poll){
foreach($poll as  $setPoll){
      $insertPollfromData = $InSert->Insert_Poll($uid, $setPoll);
    }
    }
}
?>

Now, i want to insert multiple answer from poll table but my postPoll.php gives me this error:

Warning: Invalid argument supplied for foreach() in

How can i insert multiple poll from database ? Anyone can help me here ? Thank you very much in advance for your help.

3
  • you only use foreach when iterating over arrays. seems $poll is not an array but a boolean. Also where does $uid come from? Commented May 10, 2017 at 7:40
  • @Akin.. i m agree with u.. Commented May 10, 2017 at 7:41
  • To accomplish your task, you'll probably need to set the input field where the user enters answers to array e.g create_answer[] and then iterate on your php side Commented May 10, 2017 at 7:45

1 Answer 1

1

In your html i would u suggest using a <form> and append inputs to it. then you can use https://api.jquery.com/serialize/

To send the serialized data from all the inputs to your PHP file trough ajax.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.