etsted Posted September 6, 2014 Share Posted September 6, 2014 <span onclick="rate('<?php echo $log_username;?>','good','<?php echo $file_url;?>')" onmouseout="normal(this)" onmouseover="green(this)"> Good </span> | <span onclick="rate('<?php echo $log_username;?>','bad','<?php echo $file_url;?>')" onmouseover="red(this)" onmouseout="normal(this)"> Bad </span> <span id="rate_Box"></span> these are three divs. The two at the top are buttons. When you click on on of them they are supposed to trigger an AJAX function, but it doesnt work. function rate(username,rate_type,file_name){ var ajax = ajaxObj("POST", "watch.php"); ajax.onreadystatechange() = function(){ if(ajaxReturn(ajax) == true){ document.getElementById("rate_Box").innerHTML = ajax.responseText; } } ajax.send("rate_type="+rate_type+"&username="+username+"&file_name="+file_name); } i tried to use console.log on ajax.responseText, but nothing happened until i pressed one of the buttons, then it outputted "object is not a function". here is the PHP it is supposed to communicative with, and all of there are in the same file. if(isset($_POST['rate_type'])) { $username = $_POST['username']; $file_name = $_POST['file_name']; $rate_type = $_POST['rate_type']; //check to see if the user has voted on this file before $sql = "SELECT rate_type,good_rate,bad_rate FROM rate WHERE username = $username && file_name = $file_name"; $query = mysql_query($sql); $numrows = mysql_num_rows($query); if($numrows != 1)//this means the user has not voted for this file before { $sql = "INSERT INTO rate VALUES('','$file_name','$username','$rate_type')"; $query = mysql_query($sql); }// if else //this means it has voted before { //check to see what the user voted before $row = mysql_fetch_array($sql); $DB_rate_type = $row['rate_type']; $DB_good_rate = $row['good_rate']; $DB_bad_rate = $row['bad_rate']; echo "bra"; //if what the user wants to vote is not equal to what he voted before, continue... if($DB_rate_type != $rate_type) { if($rate_type === "good") { $DB_good_rate++; } else { $DB_bad_rate--; } $sql = "UPATE rate SET rate_type = $rate_type && good_rate = $DB_good_rate && bad_rate = $DB_bad_rate"; $query = mysql_query($sql); exit; } else//else dont do nothing... { exit; } } } Link to comment Share on other sites More sharing options...
JamesB Posted September 6, 2014 Share Posted September 6, 2014 You need quotes around some SQL values. $sql = "SELECT rate_type,good_rate,bad_rate FROM rate WHERE username = '$username' && file_name = '$file_name'"; $sql = "UPATE rate SET rate_type = '$rate_type' && good_rate = $DB_good_rate && bad_rate = $DB_bad_rate"; Also try to use MySQLi or PDO for the database code, as the mysql_ functions are very old. Link to comment Share on other sites More sharing options...
davej Posted September 6, 2014 Share Posted September 6, 2014 When you click on on of them they are supposed to trigger an AJAX function, but it doesnt work. Your code should include error checking and error messages. Link to comment Share on other sites More sharing options...
srinivas g Posted September 7, 2014 Share Posted September 7, 2014 can u tell me what u want to do exactly..explain properly i may help u...! Link to comment Share on other sites More sharing options...
etsted Posted September 7, 2014 Author Share Posted September 7, 2014 can u tell me what u want to do exactly..explain properly i may help u...! I am trying to make a rating system. Link to comment Share on other sites More sharing options...
etsted Posted September 7, 2014 Author Share Posted September 7, 2014 i can see from here that the $_POST variables are not set, any clue? function rate(username,rate_type,file_name){ var ajax = ajaxObj("POST", "watch.php"); ajax.onreadystatechange() = function(){ if(ajaxReturn(ajax) == true){ document.getElementById("rate_Box").innerHTML = ajax.responseText; } } ajax.send("rate_type="+rate_type+"&username="+username+"&file_name="+file_name); } this is my AJAX and this is my new PHP if(isset($_POST['username'])) { $username = $_POST['username']; $file_name = $_POST['file_name']; $rate_type = $_POST['rate_type']; //check to see if the user has voted on this file before $sql = "SELECT rate_type,good_rate,bad_rate FROM rate WHERE username = '$username' && file_name = '$file_name'"; $query = mysqli_query($con, $sql) or die(mysqli_error($con)); $numrows = mysqli_num_rows($query); if($numrows != 1)//this means the user has not voted for this file before { $sql = "INSERT INTO rate VALUES('','$file_name','$username','$rate_type','','')"; $query = mysqli_query($con,$sql) or die(mysqli_error($con)); }// if else //this means it has voted before { //check to see what the user voted before $row = mysqli_fetch_array($sql); $DB_rate_type = $row['rate_type']; $DB_good_rate = $row['good_rate']; $DB_bad_rate = $row['bad_rate']; //if what the user wants to vote is not equal to what he voted before, continue... if($DB_rate_type != $rate_type) { if($rate_type === "good") { $DB_good_rate++; } else { $DB_bad_rate--; } $sql = "UPATE rate SET rate_type = '$rate_type' && good_rate = '$DB_good_rate' && bad_rate = '$DB_bad_rate' "; $query = mysqli_query($sql) or die(mysqli_error($con)); } else //else dont do nothing... { } } } Link to comment Share on other sites More sharing options...
JamesB Posted September 7, 2014 Share Posted September 7, 2014 Try changing ajax.onreadystatechange() = function(){ to ajax.onreadystatechange = function(){ Link to comment Share on other sites More sharing options...
etsted Posted September 7, 2014 Author Share Posted September 7, 2014 Try changing ajax.onreadystatechange() = function(){ to ajax.onreadystatechange = function(){ whats the difference? show me an example Link to comment Share on other sites More sharing options...
JamesB Posted September 7, 2014 Share Posted September 7, 2014 2 characters is the difference, look closer. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now