Jump to content

Ajax and MYSQL (PHP)


CUatTHEFINISH

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/102627-ajax-and-mysql-php/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.