SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2010
    Posts
    72
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Crippling Ajax Problem - PHP Keeps Showing NULL

    I absolutely cannot get this to work. I've tried many different solutions from different decoding in my PHP to using $.post and $.getJSON but I absolutely cannot get my ajax to work. On my main AJAX Page's network it is Requesting Payload "names%5B%5D=John+Doe&names%5B%5D=Jane+Doe&names%5B%5D=Josh+Doe" - which should be fine and dandy. The problem is my PHP Page keeps showing NULL. I've been trying many different things the last few days, any insight would be helpful.

    Here's my Javascript - I'm running JQuery 1.7.2

    Code:
    var fullName = ["John Doe", "Jane Doe", "Josh Doe"];
    
    $(window).load(function(){
    	getList();
    });
    		 
    function getList(){
        $.ajax({
           type: "post", /* the request's method: */
           url:"names.php",    /* the request's location: */
           data: {"names[]": fullName}, /* the request's fields: */
           contentType: "application/json; charset=utf-8",  /* the request's content-type : */
           dataType:"json", /* the response's content-type: */
           success: function(json){ /* the callback function */
             if(json.length != null){
                 $.each(json, function(i, v){
                    console.info(v);
                 });
             } else {
                alert('wtf?!');
             }
           }
        });
    }
    and my Simple PHP Page

    Code:
    <?php
    urldecode(var_dump($_POST['names[]']));
    $names = $_POST['names[]'];
    echo json_encode($names);

  2. #2
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,122
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Two things
    1. When I go to your ajax page, you use "names" instead of "names[]", which wouldn't match your PHP (I recommend using "names"), so update your PHP to use $_POST['names']
    2. Visiting the names.php directly will always return NULL because you are not sending any POST data. The data should be sent, but is likely causing a mismatch between the variable name in your JavaScript and the post variable name PHP is expecting.


    Moving to PHP forum.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •