簡體   English   中英

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM