Hi I have problems with the script below. The problem I think lies on data that need to be sent to php via AJAX.
jQuery
$('.send').live("click", function(){
$.ajax({
url:'foobar.php',
type:'post',
data: 'id=' + $(this).attr('id'),
dataType:'json',
contentType: 'application/json; charset=utf-8',
success: function(data) {
switch (data.status)
{
case "a":
alert(data.text);
break;
case "b":
alert(data.text);
break;
}
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert ("error: "+textStatus);
}
})
}
and, PHP
$id = $_REQUEST['id'];
switch ($id) {
case "foo":
$data["status"] = "a";
$data["text"] = "foo-foo";
echo json_encode($data);
break;
case "bar":
$data["status"] = "b";
$data["text"] = "bar-bar";
echo json_encode($data);
break;
}
but, if I do this
//data: 'id=' + $(this).attr('id'),
and change this
$id = 'foo';
the script work just fine. What I need to do to make both script above can work? Thanks in advance.
Click This!
– ani May 02 '13 at 13:16