Question

I'm trying to get data from a user interface to PHP.

In JS, I have:

var myPostData=JSON.stringify({'categoria':valor,'fluxo':fluxo});
$.ajax({
        url:'data.php',
        type:'post',
        data:myPostData,
        dataType: "json",
});

Where valor and fluxo are variables.

And in PHP:

if (isset($_POST['categoria'])){
    $fluxo=$_POST['fluxo'];
    $categoria=$_POST['categoria']; 

    echo("Fluxo ".$fluxo);
    echo("categoria ".$categoria);

}else{
    echo "nada";    
}

But I can't get the data to be processed by PHP. I always get 'nada' in return...

Thanks in advance for any help!

Was it helpful?

Solution

Instead of transforming the data to a JSON string, just add it as a plain Javascript object:

var myPostData={'categoria':valor,'fluxo':fluxo};
$.ajax({
    url:'data.php',
    type:'post',
    data:myPostData,
    dataType: "json",
});

Also as pointed out in comments, you are accessing the POST variable by $_POST['valor'], when you are sending it as $_POST['categoria'].

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top