Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

i want to pass a json object with ajax request in my js page but when i do it, in php i have the $_POST array empty.

javascript page

    function invia(cat,subcat)
{
    var tipo = cat.options[cat.selectedIndex].text;
    var sottotipo = subcat.options[subcat.selectedIndex].text;

    var lat = getLatitudine();
    var lon = getLongitudine();     

        $.ajax({    
            type:'POST',                                
            url: "apriSegnalazione.php",
            dataType: "json",
            data : { 
                type: {type: tipo, subtype: sottotipo};
                lat: lat,
                lng: lon
                }           
            }).success(function(data){

                alert( "Data Saved: "+ data);

            });

}

and this is my php page

<?php  
header('Content-Type: application/json',true);
header('Accept: application/json');

  $tipo = $_POST['type'];    
  $ris = json_decode($tipo,true);
    var_dump($ris);

?>

any suggestions?

share|improve this question

1 Answer 1

up vote 0 down vote accepted

Replace ; with , from this line,

type: {type: tipo, subtype: sottotipo};

Should be,

type: {type: tipo, subtype: sottotipo},

There is an error SyntaxError: missing } after property list in your script, and always use tools like mozilla's firebug addon, when you are processing with ajax requests. You can see the above error in firebug's console panel.

share|improve this answer
    
the response in php is empty, this was my problem. the object type doesn't exists in $_POST –  lorenzo.lambertini Jun 12 '13 at 16:26
    
Now your $_POST array is not empty, right? –  Nikhil Mohan Jun 12 '13 at 16:29
    
in $_POST there are lat 44.4779266 lng 11.3599224 type[subtype] incidente type[type] problemi stradali but how to manage this data in php? if i try var_dump, i have NULL as response.. –  lorenzo.lambertini Jun 12 '13 at 17:36
    
What you want to do with $_POST in php, and what is expected as response from php? –  Nikhil Mohan Jun 13 '13 at 11:43
    
i want to save this values in mysql or in a local variable and print them. –  lorenzo.lambertini Jun 13 '13 at 17:01

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.