Jump to content

Recommended Posts

Try something like this:

 

<select name="select" id="mySelect">
<?php
while($values = mysql_fetch_array($query)){
 echo "<option value=\"{$values['column']}\" onchange=\"document.getElementById('myText').value = this.value\">{$values['column']}</option>";
}
?>
</select>
<input type="text" name="textfield" id="myText" />

what I'm trying to do is called PHP and AJAX Live Search

 

I found this site

http://www.w3schools.com/php/php_ajax_livesearch.asp

 

I edited the code to  this

 

the problem is this I get an error message when I do the search

 

Object not found!

 

The requested URL was not found on this server. The link on the referring page seems to be wrong or outdated. Please inform the author of that page about the error.

 

If you think this is a server error, please contact the webmaster.

Error 404

 

if I change this  var url="livesearch.php" to the name of the html page then I get no error message a search result is returned but if includes the page and all formating

 

any ideas

 

js

var xmlHttp

function showResult(str)
{
if (str.length==0)
{ 
document.getElementById("livesearch").
innerHTML="";
document.getElementById("livesearch").
style.border="0px";
return
}

xmlHttp=GetXmlHttpObject()

if (xmlHttp==null)
{
alert ("Browser does not support HTTP Request")
return
} 

var url="livesearch.php"
url=url+"?q="+str
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("livesearch").
innerHTML=xmlHttp.responseText;
document.getElementById("livesearch").
style.border="1px solid #A5ACB2";
} 
}

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;
}

 

php page

<?php
//specify how many results to display per page

$limit = 5;

// Get the search variable from URL
  $var = @$_GET['q'] ;
//trim whitespace from the stored variable
  $trimmed = trim($var); 
//separate key-phrases into keywords
  $trimmed_array = explode(" ",$trimmed);

// check for an empty string and display a message.
if ($trimmed == "") {

  $resultmsg =  "<p>Search Error</p><p>Please enter a search...</p>" ;
  }

// check for a search parameter
if (!isset($var)){
  $resultmsg =  "<p>Search Error</p><p>We don't seem to have a search parameter! </p>" ;
  }
// Build SQL Query for each keyword entered
foreach ($trimmed_array as $trimm){
     
// EDIT HERE and specify your table and field names for the SQL query
     $query = "SELECT * FROM users WHERE last_name LIKE \"%$trimm%\" OR first_name LIKE  \"%$trimm%\" ORDER BY last_name   DESC" ; 
     // Execute the query to  get number of rows that contain search kewords
     $numresults=mysql_query ($query);
     $row_num_links_main =mysql_num_rows ($numresults);

     // next determine if 's' has been passed to script, if not use 0.
     // 's' is a variable that gets set as we navigate the search result pages.
     if (empty($s)) {
         $s=0;
     }

      // now let's get results.
      $query .= " LIMIT $s,$limit" ;
      $numresults = mysql_query ($query) or die ( "Couldn't execute query" );
      $row= mysql_fetch_array ($numresults);

      //store record id of every item that contains the keyword in the array we need to do this to avoid display of duplicate search result.
      do{
//EDIT HERE and specify your field name that is primary key
            $adid_array[] = $row[ 'user_id' ];
      }while( $row= mysql_fetch_array($numresults));
} //end foreach

if($row_num_links_main == 0 && $row_set_num == 0){
   $resultmsg = "<p>Search results for:" . $trimmed  ."</p><p>Sorry, your search returned zero results</p>" ;
}
   //delete duplicate record id's from the array. To do this we will use array_unique function
   $tmparr = array_unique($adid_array);
   $i=0;
   foreach ($tmparr as $v) {
       $newarr[$i] = $v; 
       $i++;
   }

// now you can display the results returned. But first we will display the search form on the top of the page
foreach($newarr as $value){


// EDIT HERE and specify your table and field names for the SQL query
$query_value = "SELECT * FROM users WHERE user_id = '$value'";
$query_get_members=mysql_query ($query_value);
$members= mysql_fetch_assoc($query_get_members);
$row_num_links= mysql_num_rows ($query_get_members);
}

echo $members['first_name'];
?>

 

html page

 

<style type="text/css"> 
#livesearch
  { 
  margin:0px;
  width:194px; 
  }
#txt1
  { 
  margin:0px;
  } 
</style>
<script src="../Scripts/livesearch.js"></script> 
</head>
<body>

<div id="container">
	<div id="top">
	<?php include('../design/topMenu.php'); ?>
	</div>	
		<div id="subHeader">

		  <p> </p>
	    </div>
<div id="leftnav">

 <?php include('../searchMembers.php'); ?>

</div>
	<div id="Centercontent">

<form>
<input type="text" id="txt1" size="30"
onkeyup="showResult(this.value)">

<div id="livesearch"></div>
</form>

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.