tags:

views:

86

answers:

4

Hello,

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?

A: 

If I understand your question correctly, all you are asking is using different javascript to execute for different response from server. If so, what you are doing will work with if-elseif in success callback as you are using now.

Teja Kantamneni
+2  A: 

As Teja wrote, you can just add more if-else blocks in your JavaScript code to handle the different return values.

But more to the point - if you need to return more than 1 piece of information, you really should encode it as (for example) JSON on the server-side. There are built-in PHP libraries for that, and a JSON javascript library on JSON.org.

Justin Ethier
+1  A: 

You want $.getJSON to allow multiple return values

See the docs

Then have PHP format it's output as JSON something like

{ "subscribed": true, "ignored": false }

Then your javascript can look like this

$.get("getdata.php",
function(returned_data)
{ 

   if(returned_data.subscribed){
      ... 
   }

   if(returned_data.ignored){
      ...
   }

});
Jake
+1  A: 

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:

$query = mysql_query("SELECT * FROM subscription WHERE id=$ipr");
$result = mysql_fetch_assoc($query);

if (time() > $result['date']) {
    echo '{ "resultCode":1, "description":"message 1 to be displayed to user" }';
} else {
    echo '{ "resultCode":5, "description":"message 5 to be displayed to user" }';
}

$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 '{ "resultCode":2, "description":"message 2 to be displayed to user" }';
}

The javascript can then be updated to the following:

$(document).ready(function(){
    $.getJSON("getdata.php", function(returned_data) { 

        // returned_data is now a javascript object with 2 properties:
        //   returned_data.resultCode
        //   returned_date.message

        // if the user has a valid subscription
        if (returned_data.statusCode === 5) {
            $("#message_wall").removeAttr("disabled");
            return true;
        }
        // if the user has an invalid subscription, show the message returned from the PHP
        else {
            $("div#wall").html(returned_data.message);
            $("#message_wall").attr("disabled", "disabled");
            return false;                
        }
    });
});

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:

[ { "resultCode":1, "description":"msg" }, { "resultCode":2, "description":"msg" }, ... ]

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

Rouan van Dalen
Thanks Rouan!Is it possible to hide name of *.php file in $.getJSON("getdata.php", ... ?Because that jquery code will be visible in source code of the page and I do not want to some malicious visitors try to do something with it.
Sergio
Hmmm, this is tricky as almost all of the solutions are reversible by smart people looking at the source. You could try base64 encoding the javascript string or using unicode literals inside the string, but this will throw off only the average user because those are easily recognizable and reversable. You could create a PHP page that takes a garbled string or hash of some kind and based on the supplied hash redirect to getdata.php. You would then call something like 'securedPage?pageHash=aC7bz122;' in $.getJSON
Rouan van Dalen