doupu7651 2015-06-26 00:56
浏览 47
已采纳

从Javascript传递时,Ajax数据不能在PHP中打印

I am trying to print the elements of an array in PHP which was passed from Javascript. I think the js array is being passed but for some reason it is not being printed via php. Sorry if my code is not properly indented. TIA

<?php
   $link = mysqli_connect("localhost","xxxx", "xxxxxx","xxxx");

   if($_POST['delete']){  

      $delete= json_decode($_POST['str'], true);

      foreach($delete as $x){
          echo"<script> alert(".$x.");</script>";
       }

  }     

  ?>
      <html lang="en">
         <head> 

         <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>

      </head>
       <body >
          //This is a table which displays rows from a database. 
          //The user selects rows and the ids of those rows are 
          //are stored in a javascript array called 'toDelete'
          //I want to pass this array to PHP 

         <form  method="post" id="myForm" >
            <table id="secondDiv" >
              <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", "xxxxxx","xxxx");                  

              $query  = "SELECT * FROM meetings";

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

                while($row = mysqli_fetch_array($result)){                     
                  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>";

                }
              }
        ?>                                  

         </table>         

           <!-- <input type="submit" class="btn btn-warning" value="Edit" />    -->  
          <input type="hidden" id="str" name="str" value="" /> 
          <input type="submit" id="btn" name="delete" value="Delete" />


     </form>

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

           });


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


   $(document).ready(function(){
       $("#btn").click( function() {
          $.post( $("#myForm").attr("action"),
              $('#str').val(JSON.stringify(toDelete))               
           );
     });

     $("#myForm").submit( function() {
        return false; 
     });

   });
   </script>    

  </body>

 </html>
  • 写回答

1条回答 默认 最新

  • dthl8036 2015-06-26 03:26
    关注

    As you are printing a string, not a javascript variable, you don't have quotes within alert call.

    Change the line:

    echo"<script> alert(".$x.");</script>";
    

    With:

    echo "<script> alert('$x'); </script>";
    

    Or:

    echo "<script> alert('".$x."'); </script>";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本