tags:

views:

43

answers:

4

I want to be able to display a message if a user is not logged in if they try rating a user by clicking a rating. Is there a way to add it to my JQuery code below or can I pass it to my PHP script?

I'm using PHP

Here is the JQuery code.

$('#rate li a').click(function(){
    $.ajax({
        type: "GET",
        url: "http://localhost/update.php",
        data: "rating="+$(this).text()+"&do=rate",
        cache: false,
        async: false,
        success: function(result) {
            // remove #ratelinks element to prevent another rate
            $("#rate").remove();
            // get rating after click
            getRating();
            getRatingAvg();
            getRatingText2();
            getRatingText();
        },
        error: function(result) {
            alert("some error occured, please try again later");
        }
    });
A: 

Send something back to jQuery as the result and check it, before you perform your other steps.

ZeissS
send something back from where? excuse my ignorance.
jphp
See Reigels answer. He provides a more complete answer.
ZeissS
A: 

jQuery alone cannot test reliably if the user is logged in, but your request to PHP could send an answer if theuser was logged in or not. This answer can be checked and then display an error (or not).

alopix
How can i do this? an example would lead me in the right direction.
jphp
PHP outputs something and in the success-function you can test if the result is e.g. 'error'.
alopix
+1  A: 

just check it if one is login in your update.php script. If not login, echo something like "error".

then in your success handler,

    success: function(result) {
        if (!$.trim(result)==='error') {
           // remove #ratelinks element to prevent another rate
           $("#rate").remove();
           // get rating after click
           getRating();
           getRatingAvg();
           getRatingText2();
           getRatingText();
        } else {
           // not login, do something...
           alert('login first please...');
        }
    },
Reigel
A: 

Use your update.php script to send back a codified response that includes an acknowledgement of whether the user is logged in; I am assuming the script echoes some value which is then received as the result variable in your jquery snippet.

if(result.loggedin == "1"){ //Assuming your output is a JSON object

}else{

}
Kapil Kaisare