Today's Posts Follow Us On Twitter! TFL Members on Twitter  
Forum search: Advanced Search  
Navigation
Marketplace
  Members Login:
Lost password?
  Forum Statistics:
Forum Members: 24,254
Total Threads: 80,792
Total Posts: 566,471
There are 1154 users currently browsing (tf).
 
  Our Partners:
 
  TalkFreelance     Design and Development     Programming     PHP and MySQL :

AJAX: Flaw In Logic?

Thread title: AJAX: Flaw In Logic?
Closed Thread    
    Thread tools Search this thread Display Modes  
10-18-2006, 08:20 PM
#1
JMooring is offline JMooring
Status: I'm new around here
Join date: Oct 2006
Location:
Expertise:
Software:
 
Posts: 5
iTrader: 0 / 0%
 

JMooring is on a distinguished road

  Old  AJAX: Flaw In Logic?

Javascript:

Code:
<script type="text/javascript">
	var http

	function rate(video_id, rating) {
		http = open_http()
		var url="{$base->path}/?module=videos&action=rate&video_id=" + video_id + "&rating=" + rating
		
		http.onreadystatechange = update_rating(video_id)
		http.open("GET", url, TRUE)
		http.send(NULL)
	}
	
	function update_rating(video_id) {
		if(http.readyState == 4 || http.readyState == "complete") {
			document.getElementById(video_id).innerHTML = http.responseText
		}
	}
	
	function open_http() { 
		var http = NULL
		
		if(window.XMLHttpRequest) {
			http = new XMLHttpRequest()
		}
		
		else if(window.ActiveXObject) {
			http = new ActiveXObject("Microsoft.XMLHTTP")
		}
		
		return http
	} 
</script>
PHP:

PHP Code:
<?php
/* Jon Mooring
   jon@elementsixstudio.com */

#define action variable
if($isset($_GET['action'])) {
    
$action $_GET['action'];
}

#different action options
switch($action) {
    
#video rating block
    
case('rate'):
        
#only rate if valid
        
if(isset($_GET['video_id']) AND isset($_GET['rating']) AND !isset($_COOKIE['rating['.$_GET['video_id'].']'])) {
            
$rating_data $db->query_fetch_array("SELECT * FROM {$db->prefix}ratings
                                                   WHERE `video_id` = '
{$_GET['video_id']}';");
            
            
$count = ($rating_data['count'] + 1);
            
            
#recalculate average
            
switch($_GET['rating']) {
                case(
0):
                    
$average round(($rating_data['average'] / $count), 2);
                break;
                case(
1):
                    
$average round((($rating_data['average'] + 1) / $count), 2);
                break;
            }
            
            
#set "already rated" cookie
            
setcookie('rating['.$_GET['video_id'].']''1', (time() * 3600 24 365));
            
            
#update rating in database
            
$db->query("UPDATE {$db->prefix}ratings
                        SET `count` = '
{$count}', `average` = '{$average}'
                        WHERE `video_id` = '
{$_GET['video_id']}';");
            
            
#return new rating
            
echo('<strong>'.$average.'</strong> / 1.00 (<strong>'.$count.'</strong> votes)');
        }
    break;
}
?>
HTML:

HTML Code:
<strong>{$videos_data['title']}</strong>
<br /><br />
<strong>Description:</strong> {$videos_data['description']}
<strong>Rating:</strong> <span id="{$videos_data['video_id']}"><strong>{$videos_data['average']}</strong> / 1.00 (<strong>{$videos_data['count']}</strong> votes)</span> <a href="" onclick="rate({$videos_data['video_id']}, 1)">+</a> <a href="" onclick="rate({$videos_data['video_id']}, 0)">-</a>
<br />
The rating refuses to even update in the database. Any ideas? I assume it is because I have a flaw in my AJAX code, because I've tested the PHP code manually through my browser and it works fine.

10-18-2006, 09:29 PM
#2
Bas is offline Bas
Status: I love this place
Join date: Jan 2005
Location: The Netherlands
Expertise: Frontend, vBulletin
Software: Coda, Photoshop
 
Posts: 607
iTrader: -1 / 0%
 

Bas is on a distinguished road

Send a message via MSN to Bas

  Old

In this code you can't have an argument in the function:
Code:
http.onreadystatechange = update_rating(video_id)
It has to be either this:
Code:
http.onreadystatechange = update_rating
Or this:
Code:
http.onreadystatechange = function()
{
	// video_id here
}

10-18-2006, 10:02 PM
#3
JMooring is offline JMooring
Status: I'm new around here
Join date: Oct 2006
Location:
Expertise:
Software:
 
Posts: 5
iTrader: 0 / 0%
 

JMooring is on a distinguished road

  Old

that's the same response I got at another forum, but that doesn't work either any other ideas?

10-19-2006, 03:21 PM
#4
Bas is offline Bas
Status: I love this place
Join date: Jan 2005
Location: The Netherlands
Expertise: Frontend, vBulletin
Software: Coda, Photoshop
 
Posts: 607
iTrader: -1 / 0%
 

Bas is on a distinguished road

Send a message via MSN to Bas

  Old

Your URL starts with '{$base->path}', I think that's PHP code that you just copied in there?

10-19-2006, 07:09 PM
#5
JMooring is offline JMooring
Status: I'm new around here
Join date: Oct 2006
Location:
Expertise:
Software:
 
Posts: 5
iTrader: 0 / 0%
 

JMooring is on a distinguished road

  Old

Database-driven template system that auto-parses variables

10-19-2006, 09:35 PM
#6
Bas is offline Bas
Status: I love this place
Join date: Jan 2005
Location: The Netherlands
Expertise: Frontend, vBulletin
Software: Coda, Photoshop
 
Posts: 607
iTrader: -1 / 0%
 

Bas is on a distinguished road

Send a message via MSN to Bas

  Old

Ok, well then maybe add some alert functions in your Javascript code and see where it goes wrong. If your javascript is ok, move on to your PHP code.
I also just noticed you use 'video_id' as your id for the HTML. If this is just a number, maybe you should add a prefix to it. I've had some problems with using numbers only for ids in the past.

10-20-2006, 02:12 PM
#7
KarlP is offline KarlP
KarlP's Avatar
Status: I'm new around here
Join date: Apr 2006
Location:
Expertise:
Software:
 
Posts: 24
iTrader: 0 / 0%
 

KarlP is on a distinguished road

  Old

Ignore this post, I'd delete but there doesn't appear to be anyway to

Closed Thread    


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 

  Posting Rules  
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump:
 
  Contains New Posts Forum Contains New Posts   Contains No New Posts Forum Contains No New Posts   A Closed Forum Forum is Closed