Jump to content

CUatTHEFINISH

New Members
  • Posts

    3
  • Joined

  • Last visited

    Never

Profile Information

  • Gender
    Not Telling

CUatTHEFINISH's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. I'm trying to create a search page using ajax, and I'm having issues getting some results from it. Submit Form <form id="searchform" name="searchform"> User: <input type="text" name="user" id="user" /> Results: <select name="amount" id="amount"> <option value="5" selected="selected">5</option> <option value="10">10</option> <option value="25">25</option> <option value="50">50</option> </select> Sort Ranking: <select name="method" id="method"> <option value="ASC" selected="selected">Ascending</option> <option value="DESC">Decending</option> </select> <input type="button" onclick="showUser()" value="Search" /> </form> <br /> <hr> <div id="txtHint"></div> <hr> Javascript code (I was trying adapt it from a working one) // JavaScript Document var xmlHttp function showUser() { xmlHttp=GetXmlHttpObject() if (xmlHttp==null) { alert ("Browser does not support HTTP Request") return } var url="searchprocess.php" url=url+"?user="+user url=url+"?method="+method url=url+"?amount="+amount url=url+"&sid="+Math.random() xmlHttp.onreadystatechange=stateChanged xmlHttp.open("GET",url,true) xmlHttp.send(null) } function stateChanged() { if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { document.getElementById("txtHint").innerHTML=xmlHttp.responseText } } 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; } search processing $user = $_GET['user']; $method = $_GET['method']; $amount = $_GET['amount']; $user = mysql_real_escape_string($user); $method = mysql_real_escape_string($method); $amount = mysql_real_escape_string($amount); $sql="SELECT * FROM vote WHERE creator = '".$user."' ORDER BY avg '".$method."' LIMIT '".$amount."'"; $result = mysql_query($sql); while($ratings = mysql_fetch_array( $result )) { //This outputs the sites name print <<<END <div id='winners'> <div class='winner'> <h1>$ratings[name]</h1> <div class='poster'> <center> <img alt="$ratings[id]" src="$ratings[image]" /> <h1>$ratings[title]</h1> <h2>$ratings[caption]</h2> </center> </div> </div> </div> <br /> END; //This calculates the sites ranking and then outputs it - rounded to 1 decimal if ($ratings[avg]=="0") echo "<center><strong>Current Rating:</strong> 0 </center>"; else { Echo "<center><strong>Current Rating:</strong> " .round($ratings[avg], 2) . "</center>"; } Echo "<center><strong>Votes:</strong> " .$ratings[votes]. "</center>"; } ?> My knowledge of javascript is pretty much nil, so I'm kinda being thrown for a loop.
  2. So I've been browsing the internet looking on ways to create a webpage for rating images (somewhat like hot or not, but not in the same ballpark, it'll be far more simple, no users or comments or anything for the sort). Anyways, I've stumbled across a little tutorial at about.com on how to create a VERY basic rating script (just so I can understand how this should work). However, following the tutorial verbatim (making sure the MySQL DB information is correct and connectible), their code seems to have flaws and doesn't preform MySQL updates correctly. I've been trying all these little edits to correct the problem, but I can't seem to get it to work. I've omitted my database information. <?php // Connects to your Database mysql_connect("xxxxx", "xxxxx", "xxxxxxxx") or die(mysql_error()); mysql_select_db("xxxxxxxxxx") or die(mysql_error()); //We only run this code if the user has just clicked a voting link if ( $mode=="vote") { //If the user has already voted on the particular thing, we do not allow them to vote again $cookie = "Mysite$id"; if(isset($_COOKIE[$cookie])) { Echo "Sorry You have already ranked that site <p>"; } //Otherwise, we set a cooking telling us they have now voted else { $month = 2592000 + time(); setcookie(Mysite.$id, Voted, $month); //Then we update the voting information by adding 1 to the total votes and adding their vote (1,2,3,etc) to the total rating mysql_query ("UPDATE vote SET total = total+$voted, votes = votes+1 WHERE id = $id"); Echo "Your vote has been cast <p>"; } } //Puts SQL Data into an array $data = mysql_query("SELECT * FROM vote") or die(mysql_error()); //Now we loop through all the data while($ratings = mysql_fetch_array( $data )) { //This outputs the sites name Echo "Name: " .$ratings['name']."<br>"; //This calculates the sites ranking and then outputs it - rounded to 1 decimal $current = $ratings[total] / $ratings[votes]; Echo "Current Rating: " . round($current, 1) . "<br>"; //This creates 5 links to vote a 1, 2, 3, 4, or 5 rating for each particular item Echo "Rank Me: "; Echo "<a href=".$_SERVER['PHP_SELF']."?mode=vote&voted=1&id=".$ratings[id].">Vote 1</a> | "; Echo "<a href=".$_SERVER['PHP_SELF']."?mode=vote&voted=2&id=".$ratings[id].">Vote 2</a> | "; Echo "<a href=".$_SERVER['PHP_SELF']."?mode=vote&voted=3&id=".$ratings[id].">Vote 3</a> | "; Echo "<a href=".$_SERVER['PHP_SELF']."?mode=vote&voted=4&id=".$ratings[id].">Vote 4</a> | "; Echo "<a href=".$_SERVER['PHP_SELF']."?mode=vote&voted=5&id=".$ratings[id].">Vote 5</a><p>"; } ?> It seems to me the error lies in the if ( $mode=="vote") area. The .$ratings[id] are correct for each subject being voted it, but it seems that the script isn't catching links like http://www.mywebsite.com/vote.php?mode=vote&voted=5&id=1 (I can also tell because it doesn't even add that cookie when I vote, and it doesn't echo/print anything if I try to error test the if statement.) I have plans to add onto this script, which I have successfully when tinkering with it, but it's all worthless unless I can get this vote script up and running. I know this script is VERY basic (it'll need an if statement to avoid dividing by zero, and one to keep people from exploiting the system, etc. etc. but those I won't have a problem doing myself) I'm just very confused on why it won't update the MySQL rows for total and votes, it reads from the DB alright and lists all the items to be voted on. More information on this tutorial is here: http://php.about.com/od/finishedphp1/ss/rating_script.htm Any help would be greatly appreciated.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.