skip to Main Content

Here is the JavaScript code:

  <script>      
  var cars = ["Saab", "Volvo", "BMW"];
    $.ajax({ 
   type: "POST", 
   url: "yep.php", 
   data: { 'cars' : cars}, 
   success: function() { 
          alert("Success"); 
    } 
   }); 
  </script>

Here is the PHP code:

  <?php
     $myArray = $_POST['cars'];
     var_dump($myArray);
 ?>` 

It is displaying success message, so it means array was passed to php
then it shows:

Notice: Undefined index: cars in C:xampphtdocsprojectyep.php on line 18
NULL

Why is it returning null for array?

3

Answers


  1. Lets say you a bunch of data and you want to submit all once, then you need to create and empty object and append values to it. This will be the case, if for some reasons you don’t want to use var form = new FormData(this.form);
    For example:

    <script>      
      var cars = {};
      cars['value1'] = 'Value 1';
      cars['value2'] = 'Value 2';
      cars['value3'] = 'Value 3';
    
      $.ajax({ 
      type: "POST", 
      url: "yep.php", 
      data: { cars : cars}, 
      success: function(data) { 
          console.log(data); 
      } 
     }); 
    </script>
    

    But your case is simple, just do it like this in your data:

    <script>      
      var cars = ["Saab", "Volvo", "BMW"];
    
      $.ajax({ 
      type: "POST", 
      url: "yep.php", 
      data: { cars : cars}, 
      success: function(data) { 
          console.log(data); 
      } 
     }); 
    </script>
    

    The first one is the direct method, but this one is the JSON method.

    in jquery first do this…

    var array_fields = ["Saab", "Volvo", "BMW"];
    var cars = JSON.stringify( array_fields );
    

    Then In PHP do this…

    $myArray = json_decode($_POST['cars']);
    print_r($myArray);
    
    Login or Signup to reply.
  2. I think you are not firing any event like click event, try this code and let me know is it working or not for you.

    <input type="button" name="button" value="submit">
    
      <script>      
      var cars = ["Saab", "Volvo", "BMW"];
      $('input[type=button]').click({
          $.ajax({ 
       type: "POST", 
       url: "yep.php", 
       data: { 'cars' : cars}, 
       success: function(data) { 
              alert("Success");
              console.log(data);
              $("body").empty().html('Ajax Response:<br />'+data);
        } 
       }); 
      })
      </script>
    
     <?php
         $myArray = $_POST['cars'];
         var_dump($myArray);
     ?>` 
    
    Login or Signup to reply.
  3. The error message comes before the script is even executed! Suggest two small changes so you can see what’s going on.

    In the script:

    success: function(data) { 
              alert(data); 
        } 
    

    This will show what is being returned from the ajax call. From the jQuery.ajax doc:

    success

    Type: Function( Anything data, String textStatus, jqXHR jqXHR
    ) A function to be called if the request succeeds. The function gets
    passed three arguments: The data returned from the server,…..

    In the php:

    <?php 
      if (!isset($_POST['cars'])) {
          echo "Wait for it";
          } 
      else {
          $myArray = $_POST['cars']; var_dump($myArray);
          } 
      ?>
    

    Now you will notice

    1. That Wait for it is displayed in the browser, instead of the error
      message
    2. In the alert box you will see the entire output of
      yep.php is being returned. Scroll down to see the data
      as you expect.

    IMO the place to start is to split this into two scripts. The first an html that does not include the php. The second, the php that will be requested from the script.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search