![]() |
Ajax Response Text Looping (Conditional)
Okay so I am trying to add a functionality to my website that more or less mimics the way that IMDb has a star-rating for movies. I'm using Ajax to submit a rating to the database when a star is clicked and currently the updating of the database is working BUT I am having a hard problem getting the stars to update to show the new user rating without reloading the page.
Essentially I have the response text to the Ajax call just give me the number rating (1-10) that was submitted once it was done so. I need to find out how to acquire that response text correctly and create an event that automatically updates the stars once that response text is received. Here is my relevant javascript code: Code:
/* PHP Code:
PHP Code:
Any help would be appreciated. I just need to know how to loop through the response text to check the rating number (1-10) I am having problems finding the right way to debug this problem: http://www.danconia.us/images/star_rating_issue.png |
Just had a quick glance but:
Code:
if (ajax.responseText === 'r') {You might do something like the following, rather than looping through 1-10: Code:
var respond = ajax.responseText; |
Will try and check back. Thank you.
|
So now I'm getting an error telling me the following:
Code:
<div class="error"><p>An error occurred in script '/home/kylhur/danconia.us/resources/star_submit.php' on line 9: Undefined index: currentUsernameWhen I check it in Chrome though, it says that I have an "unexpected token <". This is always a hard error to correct, especially when NetBeans doesn't find any parsing error. |
Once I fixed the unexpected token problem... which by the way I am still not sure how I fixed... I was able to keep debugging to find out how far my JSON (I switched it to JSON from originally being just text because I was running out of ideas).
First problem I think it was being returned as text and failing the !NaN test. Secondly, and perhaps more importantly, I forgot to remove the event that was added earlier that resets the blue stars to whatever rating the entity had from the user when the page was loaded. So now I remove that event and replace it with the next rating (as responded from the server upon success). Final relevant code looked like this: Code:
/* PHP Code:
|
you should really look into using json_encode(phpArr) to format your data instead of building vars manually. it's faster, and the native feature are tested and patched, making them way more reliable when silly data works its way in, which is inevitable.
|
Okay so for the record I found out why I get an "unexpected token <", it's from the PHP response when my PHP script has en error. Notice how the response from the server starts with an open braces (or whatever that character is called):
Code:
<div class="error"><p>An error occurred in script '/home/kylhur/danconia.us/resources/find_comments.php' on line 6: mysqli_escape_string() expects exactly 2 parameters, 1 given |
| All times are GMT +1. The time now is 11:19 AM. |
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.