Jump to content

using ajax search to get 2 different returns from php?


Flames

Recommended Posts

is it possible because currently i have a js script that sends the search string to searchresult.php with 2 different search types 1 for members and 1 for news, the php page decodes these search types and they are then sent back to the html page via the ajax page. the html page has 2 divs, one for news and one for members, the members one gets changed but the user one doesn't is this because of the fact im using the searchresult.php twice at the same time?

also if i manually navigate to the search result page the data is returned much quicker than through the ajax search page

Link to comment
Share on other sites

i thought you might want code but its slightly long, due to layouting code as well.

page with search:

<?php
require_once("check_login.php");
checkLogin("0");
?>
<html>
<head>
<script src="search.js"></script>
<link rel="stylesheet" type="text/css"
href="style.css" />
<title>
Help with eFlame
</title>
</head>
<body>

<div id="apDiv1">

<a href="index.php"><img src="link_imgs/home.png" alt="HOME" width="47" height="23" border="0" /></a>
<a href="help.php"><img src="link_imgs/help.png" alt="HELP" width="47" height="23" border="0" /></a>
<a href="about.php"><img src="link_imgs/about.png" alt="ABOUT" width="47" height="23" border="0" /></a>
<a href="forum/"><img src="link_imgs/forum.png" alt="FORUM" width="47" height="23" border="0" /></a>
<a href="news.php"><img src="link_imgs/news.png" alt="NEWS" width="47" height="23" border="0" /></a></div>

<div align="right" id="apDiv2">
<a href="login.php"><img src="link_imgs/login.png" alt="LOGIN" width="47" height="23" align="left" border="0" /></a>
<a href="register.php"><img src="link_imgs/register.png" alt="REGISTER" width="60" height="23" border="0" /></a></div>

<div class="style1" id="bgplacer">
<div id = "Page">
There isnt much to require help about, because the website is pretty straight forward. <br />
Below is a search bar so you can find whatever your looking for, to find members you must be logged in. <br />
This is a simple search, if you cannot find what you are looking for please use this <a href="search.php" class="link1">search</a>.
<form>
<input type="text" name="search" id="search" size="40" maxlength="100" onkeyup="searchUser(this.value)"/>
</form>
<div id="news" name="news">
News results will be displayed here.
</div>
<?php
if($online == "yes")
{
echo "<div id=\"users\" name=\"users\">
User results will be displayed here.
</div>";
}
else
{
echo "<div id=\"users2\" name=\"users2\">
You must be logged in to see user results.
</div>";
}
?>
</div>
</div>
</body>
</html>

search.js

var xmlHttp
xmlHttp=GetXmlHttpObject()

function search(str)
{
searchUser(str)
searchNews(str)
}

function searchUser(str)
{
if (xmlHttp==null)
{
return
}
var url="searchresult.php"
url=url+"?users="+str
url=url+"&sid="+Math.random()
xmlHttp.onreadystatechange=stateChanged
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
}
function searchNews(str)
{
if (xmlHttp==null)
{
return
}
var url="searchresult.php"
url=url+"?news="+str
url=url+"&sid="+Math.random()
xmlHttp.onreadystatechange=stateChanged2
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
}
function stateChanged()
{
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
{
document.getElementById("users").innerHTML=xmlHttp.responseText
}
}
function stateChanged2()
{
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
{
document.getElementById("news").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;
}

searchresult.php
<?php
// error_reporting(E_ALL);
require_once("connect.php");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
if($_GET["search"] == "user")
{
mysql_select_db($db['db'], $con) or die(mysql_error());
$user = mysql_real_escape_string($_GET["users"]);
$sql = "SELECT * FROM User WHERE Username LIKE '%$user%' ORDER BY id ASC";
$result = mysql_query($sql) or die(mysql_error());
echo "Users matching search criteria are displayed below";
echo "<table border='1'>
<tr>
<th>ID</th>
<th>Username</th>
<th>Email Address</th>
<th>Rank</th>
</tr>";

while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td><a href=\"profile.php?id=". $row['id'] ."\">" . $row['id'] . "</a></td>";
echo "<td><a href=\"profile.php?id=". $row['id'] ."\">" . $row['username'] . "</a></td>";
echo "<td>" . $row['email'] . "</td>";
echo "<td>" . $row['rank'] . "</td>";
echo "</tr>";
}
echo "</table>";
}
elseif($_GET["search"]=="news")
{
mysql_select_db($db['db'], $con) or die(mysql_error());
$news = mysql_real_escape_string($_GET["news"]);
$sql = "SELECT *,DATE_FORMAT(date, '%D %M %Y') AS date2,DATE_FORMAT(edit_date, '%D %M %Y') AS edit_date2,DATE_FORMAT(edit_date, '%D') AS edit_date3 FROM News WHERE name LIKE '%$news%' ORDER BY id ASC";
$result = mysql_query($sql) or die(mysql_error());
echo "News matching search criteria (news title)";
echo "<table border='1'>
<tr>
<th>ID</th>
<th>Title</th>
<th>Posted by</th>
<th>Posted at</th>
<th>Edited at</th>
</tr>";

while($row2 = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row2['id'] . "</td>";
echo "<td>" . $row2['name'] . "</td>";
echo "<td>" . $row2['poster'] . "</td>";
echo "<td>" . $row2['date2'] . "</td>";
if($row2['edit_date3'] < 1)
{
echo "<td> Hasn't been edited </td>";
}
else
{
echo "<td>" . $row2['edit_date2'] . "</td>";
}
echo "</tr>";
}
echo "</table>";
}

?>

As a side note a quick test revealed it wasn't working when yesterday it was working... no major edits.

Link to comment
Share on other sites

I've looked this over a bit:

 

I noticed that in search.js you are calling search news as: searchresult.php?news=text

but in searchresult.php it asks for $_GET['search']==news

 

Therefore news will never be handled.

 

Aside from alternative choices for your ajax request, that was the main thing I saw.  Take a look at that to try to narrow it down.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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