Mounta1nGoat
Mounta1nGoat

Reputation: 3

JQuery $.post never successful, why?

I've made many different attempts at this so my mistake must be really obvious, my knowledge of JQuery isn't great but I've even tried examples straight from the internet, but I can't seem to get it, all I want to do is pass the variable "myLatlng" to a PHP file and echo the result of the PHP file on callback, currently the PHP file consists of:

$data=$_POST['coord'];
echo $data;

The JavaScript is:

$.post("/includes/eventgrab.php", { "coord": myLatlng },
    function(data) {
        alert("Data Loaded: " + data);
    }, "text"
);

I have used firebug and myLatlng does hold a value. Please help

Upvotes: 0

Views: 83

Answers (2)

mike b
mike b

Reputation: 56

//PHP
$data=$_POST['coord'];
echo json_encode(array('data'=>$data);)//echo as json_encode

//JS
var mylatlng = $(".mylatlang").val();
$.ajax({
type:'post',      
url:'/includes/eventgrab.php',
data:'coord='+mylatlng,
dataType:'json',
success:function(data){
alert('data loaded'+data.data)
}
});

Upvotes: 0

Shyju
Shyju

Reputation: 218872

The below should work fine assuming you have the eventgrab.php at the correct location (use firebug to see whether it is making a call nicely to that page) and you have a proper value in the myLatlng variable.

$.post("/includes/eventgrab.php", { coord: myLatlng }, function(data) {
        alert("Data Loaded: " + data);
});

Upvotes: 1

Related Questions