2015-06-24 69 views
2

我知道此线程已发布过次数和次数,但由于某种原因,我似乎可以将发布的解决方案应用于我的代码。 在这段代码中,当用户选择一个复选框时,我试图从表中删除一行。 (表格中的每一行都有一个复选框和一个ID)未通过AJAX将JavaScript数组传递给PHP

我收集了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; 

} 

回答

1

你有你的向后的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>"; 
} 
+0

我所做的更改(TY),但仍然什么也没有发生。我不认为该数组正在通过。 – Avi