0

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>
5
  • You probably want to pare down your code a little bit, and restrain it to the most relevant portions. It might save you from more downvotes. Commented Jun 26, 2015 at 1:48
  • I edited the code. Any help would be great. Commented Jun 26, 2015 at 2:42
  • have you tried the answer that I've provided? Commented Jun 26, 2015 at 13:29
  • I did. It didn't work..Could there be a problem with the ajax call? Commented Jun 26, 2015 at 15:37
  • It worked now. I forgot a ' Commented Jun 26, 2015 at 20:24

1 Answer 1

1

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>";
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.