I have a problem with the jquery $.get function.
I’m trying to get data from PHP file needed to check does the specific user has subscription and is he recorded at ignore list by other users.
With this jquery code I can get only one of this two data from MySql using PHP file.
The jquery code is:
$(document).ready(function(){
$.get("getdata.php", function(returned_data) {
if(returned_data === "1") {
$("div#wall").html('user has no subscription');
$("#message_wall").attr("disabled", "disabled");
return false;
}
});
});
The PHP file has MySQL query and that page looks like this:
$query = mysql_query("SELECT * FROM subscription WHERE id=$ipr");
$result = mysql_fetch_assoc($query);
if (time() > $result['date']) {
echo "1";
} else {
echo "5";
}
$res = mysql_query("SELECT * FROM ignor WHERE migid=$ipr AND igid=$id") or die(mysql_error());
$num_rows = mysql_num_rows($res);
if ($num_rows >= "1") {
echo "2";
}
The thing that I need from jquery $.get function is separeted .attr() and .html() for each PHP echo response code (for echo “1”, echo “2” and echo “5”)
How can I do that with Jquery $.get?
Like Jake said, using $.getJSON is better. jQuery has built in support for receiving JSON as a response from the web server, so we don’t need any additional JSON libraries on the javascript side for such a simple task. Also on the PHP side, we can build up the JSON response (to be sent to the javascript) using hand-rolled JSON for this simple scenario.
The PHP code would look something like this:
The javascript can then be updated to the following:
Also if you need to be able to send multiple responses back to the javascript, you can build up the JSON response objects into a javascript array so that the response from the PHP code looks like this:
your javacript will then receive an array in the returned_data parameter, which can be iterated by using the built-in $.each() function.
For more info on JSON, see json.org