0

I do have a table that is generated dynamically from MySQL table values. Each listed record has a checkbox.

list.php

$query="SELECT * FROM drivers ORDER BY name";
$query=$mysqli->query($query);
$queryN=mysqli_num_rows($query);
if($queryN!=0){
   while($row=mysqli_fetch_assoc($query)){
       $id=$row['id'];
       $name=$row['name'];

       $output .='<tr id="'.$id.'">
                  <td>'.$name.'</td>
                  <td><input type="checkbox" name="assign" class="assigned" value='.$id.'"/></td>
                  </tr>';
  }
}else{
       $output .='<tr>
                  <td colspan="2">No data found</td>
                  </tr>';
}

The HTML file

<input type="button" name="send" id="send" value="Send" />

Now the JQuery script that will send only the values of the checked checkboxes in the table:

<script>

$(document).ready(function(){
    $('.send').click(function(){

        var checkValues = $('input[name=assign]:checked').map(function()
        {
            return $(this).val();
        }).get();

        $.ajax({
            url: 'assigndriver.php',
            type: 'post',
            data: { ids: checkValues },
            success:function(data){

            }
        });
    });
});

</script>

Finally, the PHP file assigndriver.php that will insert the array with the values of only checked checkboxes:

   include "../connect_to_mysql.php";

   $id = array();

   if(isset($_POST['ids'])){

    $id  = $_POST['ids'];
        for ($i = 0; $i < count($id); $i++) {
             $id_new = $id[$i];
             $sql="INSERT INTO teste (campo) VALUES ('$id_new')";    
        }     
    }

Well, I would like to know why it isn't working and if in this way the correct value of the checkbox, that must be the variable $id is being posted correctly to the PHP file.

Thank you

4 Answers 4

2

Try to change:

$('.send').click(

to:

$('#send').click(

since you assign id="send" not class="send" to your input button

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

1 Comment

Thanks! I did the change but still not posting values!
0

You should change the name of the checkbox as name="something[]". So it will know it is an array and values will posted correctly. Hope this helps you.

1 Comment

Sorry guys! Still not working. No value has been inserted to the database.
0

The right way to make the values of each checked checkbox be posted to the PHP file is doing the following script:

$(document).ready(function(){
    $('#btnadd').click(function(){

        var checkValues = $('input[name="assign[]"]:checked').map(function()
        {
            return $(this).val();
        }).get();

        $.ajax({
            url: 'assigndriver.php',
            type: 'post',
            data: { ids: checkValues },
            success:function(data){

            }
        });
    });
});

Also, we need to change the dynamic output in the PHP file:

$output .='<td align="center"><input type="checkbox" name="assign[]" id="add_driver'.$id.'" value="'.$id.'"/></td>';

If the checkbox is checked, the values will be posted correctly.

Comments

0

In the jquery you call the send button using id send but while calling the function you use . operator insted of # operator.

Difference

  the difference between "." operator and "#" operator is when you calling a 
  class you should use "."  operator.

  Example:
          HTML:
               <input type="button" name="send" class="send" value="Send" />  

         JQUERY:
                $('.send').click(function(){

                     //insert your code here..
                });


  but if you are going to call an id of a html element than use "#" operetor  

  Example: 
          HTML:
               <input type="button" name="send" id="send" value="Send" 
                onclick="send_data();" />



         JQUERY:
                 function send_data(){
                 $('#send').click(function(){

                     //insert your code here..
                });}

Hope it will help you...

4 Comments

Even replacing the class with id="send" and $("#send").click(... the values still cannot be posted and inserted to the database.
ok change your LIST.PHP code while($row=mysqli_fetch_assoc($query)) to while($row=mysqli_fetch_array($query))...hope it will work.If its work vote the post ...hav fun
Did you get the result?
in assigndriver.php code call the mysql_query($sql); it will work for sure

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.