Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them, it only takes a minute:

I am trying to send a php array to ajax but it does not work. To be honest I do not know what am I doing wrong.

I am using json_encode() which returns null.

my php code:

$info = array();
$info['NEW YORK CITY'] = array(
'Name' => 'New York City'
);

$city = $_POST['city'];

if (strtoupper($city) === 'NEW YORK CITY') {

echo "Name: " . json_encode($info['NEW YORK CITY']['Name']) . ".<br>";
} else {
echo "error.";
}

my ajax code:

$('form.ajax').on('submit', function() {
var that = $(this),
    url = that.attr('action'),
    type = that.attr('method'),
    data = {};

    that.find('[name]').each(function(index, value) {
        var that = $(this),
            name = that.attr('name'),
            value = that.val();

            data[name] = value;

    });

    //console.log(data);

    $.ajax({
        url: url,
        type: type,
        data: data,
        success: function(response) {
            //console.log(response);

            $('form.ajax').html(response);
        }
    }).fail(function(jqXHR) {
        alert(jqXHR.statusText);
    });

return false;
});

Fixed it! I had the json_encode before the array. It works now that I put the array on top.

share|improve this question
    
Can i see the print_r($info);? – aldrin27 Sep 6 at 6:30
    
$info = array(); $info['NEW YORK CITY'] = array( 'Name' => 'New York City', 'Rank' => '1st, US', 'Population' => '8,491,079' ); – Rony Alvarez Sep 6 at 6:55

3 Answers 3

json_encode accepts an array as an argument

Such that it will take the following

array('key' => 'value')

This will be sent as properly formated json key:value. But a single value will not be handled correctly and may lead to unwanted results.

share|improve this answer
2  
json_encode's first argument isn't limited to arrays. A string or a number is a perfectly acceptable input. – curtis1000 Sep 6 at 6:02
    
Though correct, it's much more straight forward to keep up with the practice, and is usually advised. My mistake though. To the asking user or any other viewers. here is a link to the documentation for json_encode() php.net/manual/en/function.json-encode.php – Andrew Mata Sep 6 at 6:05

replace your php code with the following

$city = $_POST['city'];
 if (strtoupper($city) === 'NEW YORK CITY') {
     echo json_encode($info['NEW YORK CITY']);
 } else {
     echo json_encode(array("error."));
 }
share|improve this answer
    
I get an internal server error. – Rony Alvarez Sep 6 at 6:39

Try this if this works:

  $city = $_POST['city'];

   if (strtoupper($city) === 'NEW YORK CITY') {
    echo json_encode(['Name' => $info['NEW YORK CITY']['Name'] ]);
   } else {
     echo json_encode(['error']);
   }

Ajax:

 $('form.ajax').on('submit', function() {
  var that = $(this),
  url = that.attr('action'),
  type = that.attr('method'),
  data = {};

  that.find('[name]').each(function(index, value) {
    var that = $(this),
        name = that.attr('name'),
        value = that.val();

        data[name] = value;

   $.ajax({
    url: url,
    type: type,
    data: data,
    success: function(response) {
        //console.log(response);

        $('form.ajax').html(response);
     }
     }).fail(function(jqXHR) {
     alert(jqXHR.statusText);
   });

    return false;

  });

});

Your ajax should be after the on submit then it will trigger the AJAX functions.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.