简体   繁体   中英

JavaScript Array not being passed to PHP via AJAX

I know this thread has been posted times and times before, but for some reason I can seem to apply the posted solutions to my code. In this code, I am trying to delete a row from a table when the user selects a checkbox. (Each row in the table has a checkbox and an ID)

I am collected all of the IDs of the selected checkboxes in a javascript array and passing them to PHP via Ajax.

When I try to print the elements of the $delete array, noting is happending. , I know that the JS array is now empty for I can print the values in the console.

  <?php
      $link = mysqli_connect("localhost","xxxx-xxx", "x.xxxxx","xxx-xxx");
         if($_POST['delete']){
              $delete= json_decode($_POST['deleted']);
               //Trying to print the values passed
               foreach($x as $delete){
                    echo "<script>alert(".$x.");</script>";
                }        
          }
        }

  ?>

  <form method="post">
     <table id="secondDiv" class="table table-bordered">
        <tr>
            <th >
               <form><input class="checkbox"type="checkbox" name="selectAll"id="selecctall" value=""></form>

        </th>           
              <th>Date</th>
              <th>Event</th>
              <th>Details</th>
              <th>Time & Location</th>
    </tr>

            <?php  
               $link = mysqli_connect("localhost","xxxx-xxx", "x.xxxxx","xxx-xxx");


              $query  = "SELECT * FROM meetings";

              if($result = mysqli_query($link,$query)){

                while($row = mysqli_fetch_array($result)){
                 // echo($row[0] + " " +$row[1] + " " + $row[2] + " " + $row[3]);
                  echo "<tr>";
                    echo "<td><form ><input class=\"checkbox1\" type=\"checkbox\" name=\"".$row['meetingNo']."\" >
          </form></td>";
                    echo "<td>".$row['date']."</td>";
                    echo "<td>".$row['event']."</td>";
                    echo "<td>".$row['details']."</td>";
                    echo "<td>".$row['location']."</td>";
                  echo "</tr>";

                }
              }
              //echo "hello world";
            ?>                                  

         </table>         

           <!-- <input type="submit" class="btn btn-warning" value="Edit" />    -->  
          <input onclick ="toPHP()" type="submit" class="btn btn-danger" name ="delete" value="Delete" />

     </form>

//Event Listeners--work fine 
   var toDelete = new Array();
  $(document).ready(function() {
    $('#selecctall').click(function(event) {  //on click
        if(this.checked) { // check select status
            $('.checkbox1').each(function() { //loop through each checkbox
                this.checked = true;  //select all checkboxes with class "checkbox1"              
            });
        }else{
            $('.checkbox1').each(function() { //loop through each checkbox
                this.checked = false; //deselect all checkboxes with class "checkbox1"                      
            });        
        }
    });

});

  $('input:checkbox').change(
    function(){
        if ($(this).is(':checked')) {           
           //alert(this.name);
           toDelete.push(this.name);
        }
        return true;
    });


//This function is supposed to send the toDelete array to PHP
//It is called when the user clicks delete
function toPHP(){

  for(var index = 0; index < toDelete.length; index++)
   console.log(toDelete[index]);

   var stringed = JSON.stringify(toDelete);

  $.ajax(
    {
      type: "POST",
      url: "meetings.php",
      contentType: "application/json",
      data: {deleted: stringed }
     } 
  );


  return true;  

}

You have your foreach backwards, and json_decode without the second argument will give you an object, pass true in order to get an array.

replace:

$delete= json_decode($_POST['deleted']);
foreach($x as $delete){
    echo "<script>alert(".$x.");</script>";
}   

With:

$delete= json_decode($_POST['deleted'], true);
foreach($delete as $x){
    echo "<script>alert(".$x.");</script>";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM