ThomasDesigns
First off, I want to list my JavaScript, and my PHP so that you can have that as a reference to the explanation.
JS:
PHP:
Ok, now here are the problems I am having.
The PHP script's output looks something like this: "5.7|bar75|holder75". If I point my browser to the PHP script and execute it manually, it works. It returns the correct variables, and updates the database as it is supposed to. However, when I call the ajax function from the page that uses it, a couple unexpected things happen.
One, the value returned ($Response[0]) first is not what it is supposed to be. Instead the value returned is the unaltered $Urating value. This would make sense if the value in the database were NULL, however I know for a fact that it is not, and the returned value should be an average of the $Urating and $Current.
Two, the database is not updated. The AJAX function clearly is able to access the output of this file, but the functions in the PHP are not being executed as I would like them to be.
It seems as if the two SQL queries are not being run when accessed by AJAX.
I have been scouring the internet for explanations more descriptive than w3schools's, and I haven't found much of anything. Is it even possible to run SQL UPDATE queries through AJAX and PHP?
Many questions, need some answers. Halp!
JS:
| Code: |
| function GetXmlHttpObject()
{ var xmlHttp=null; try { // Firefox, Opera 8.0+, Safari xmlHttp=new XMLHttpRequest(); } catch (e) { // Internet Explorer try { xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); } } return xmlHttp; } function ajaxFunction(holderid,barid,postid) { document.getElementById(holderid).removeAttribute("onmouseout"); document.getElementById(holderid).removeAttribute("onmousemove"); document.getElementById(holderid).removeAttribute("onmousedown"); var xmlHttp; xmlHttp=GetXmlHttpObject() if (xmlHttp==null) { alert ("Your browser does not support AJAX, you will not be able to rate news articles."); return; } var rating = document.getElementById(barid).innerHTML; xmlHttp.onreadystatechange = function() { if (xmlHttp.readyState==4) { var _RT = xmlHttp.responseText.split("|"); var _R = new Number(_RT[0]); var barid = _RT[1]; var holderid = _RT[2]; var _W = _R*10; document.getElementById(barid).style.width = _W + "%"; document.getElementById(barid).innerHTML = "Rated -- " + _R; } } var url = "scripts/ratings.php?id="; url += postid ; url += "&rating="; url += rating; url += "&barid="; url += barid; url += "&holderid="; url += holderid; url += "&sid=" ; url += Math.random(); xmlHttp.open("GET",url,true); xmlHttp.send(null); } |
PHP:
| Code: |
| <?php
$id = $_GET['postid']; $Urating = $_GET['rating']; $barid = $_GET['barid']; $holderid = $_GET['holderid']; $con = mysql_connect("localhost","FOO","BAR"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("FooBar", $con); $result = mysql_query("SELECT * FROM news WHERE id = '$id'"); $Crating = mysql_fetch_row($result); $Current = $Crating[6]; if($Current == null){ $newrating = $Urating; } else{ $newrating = (($Urating + $Current)/2); } $query = "UPDATE news SET rating = '$newrating' WHERE id = '$id'"; mysql_query($query); $Response[0]=$newrating; $Response[1]=$barid; $Response[2]=$holderid; echo $Response[0] . "|"; echo $Response[1] . "|"; echo $Response[2] . "|"; mysql_close($con); ?> |
Ok, now here are the problems I am having.
The PHP script's output looks something like this: "5.7|bar75|holder75". If I point my browser to the PHP script and execute it manually, it works. It returns the correct variables, and updates the database as it is supposed to. However, when I call the ajax function from the page that uses it, a couple unexpected things happen.
One, the value returned ($Response[0]) first is not what it is supposed to be. Instead the value returned is the unaltered $Urating value. This would make sense if the value in the database were NULL, however I know for a fact that it is not, and the returned value should be an average of the $Urating and $Current.
Two, the database is not updated. The AJAX function clearly is able to access the output of this file, but the functions in the PHP are not being executed as I would like them to be.
It seems as if the two SQL queries are not being run when accessed by AJAX.
I have been scouring the internet for explanations more descriptive than w3schools's, and I haven't found much of anything. Is it even possible to run SQL UPDATE queries through AJAX and PHP?
Many questions, need some answers. Halp!
