Jump to content

AJAX and PHP


etsted

Recommended Posts

<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

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

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...