JavaScript数组未通过AJAX传递给PHP


JavaScript Array not being passed to PHP via AJAX

我知道这个线程以前已经发布过很多次了,但出于某种原因,我似乎可以将发布的解决方案应用到我的代码中。在这段代码中,我试图在用户选中复选框时从表中删除一行。(表中的每一行都有一个复选框和一个ID)

我收集了javascript数组中所选复选框的所有ID,并通过Ajax将它们传递给PHP。

当我尝试打印$delete数组的元素时,注意到正在发生,我知道JS数组现在是空的,因为我可以在控制台中打印值。

  <?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;  
}

您的foreach向后,不带第二个参数的json_decode将给您一个对象,传递true以获得一个数组。

替换:

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

带有:

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