Rewritten URL stops jquery auto suggest working

Hi guys,

Really need some help on this tricky problem. Basically, I have a search box which has an auto suggest feature (jquery) which runs through a list of stores (mysql database query). Once the user types a key in the search box, a list of stores matching that letter appears. Clicking the resulting store name from the dropdown box enters the store name into the search box, ready to be searched. The auto suggest feature of the search box works fine except if the url is a rewritten one with a variable passed through it. I can still type manually into the search box but really want the auto suggest to do the hard work.

This url won’t work - http://www.mywebsite.co.uk/store/store_name

however, if I manually change the url to the native querystring it works

http://mywebsite.co.uk/store.php?name=store_name

Anyone have any suggestions? Btw, I get no errors.

Here’s the code for the search box


<div id="search_container">
  <form id="search" action="" method="post" >
    <input name="find" type="text" class="search" /><input type="submit" value="submit">
  </form>
</div>

<div id="suggestbox">
  <div class="dropdown">
    <ul class="result"></ul>
  </div>
</div>

<script src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
<script src="search.js"></script>

and here’s the code for the search.js script


$(document).ready(function() {
	$('.search').keyup(function() {
		var search_term = $(this).attr('value');
		$.post('result.php', {search_term:search_term}, function(data) {
			$('.result').html(data);
			$('.result li').click(function() {
				var result_value = $(this).text();
				$('.search').attr('value', result_value);
				$('.result').html('');
			});
		});*/

	});
});

finally here’s the code for the result.php page


<?php
include ('inc/functions.php');
connectdata ();

if (isset($_POST['search_term']) == true && empty($_POST['search_term']) == false) {
	$findit = mysql_real_escape_string($_POST['search_term']);
	$query = mysql_query("SELECT store FROM stores WHERE store_name LIKE '$findit%' ORDER BY RAND() LIMIT 10");
	while (($row = mysql_fetch_array($query)) !== false) {
		echo '<li>', $row['store_name'], '</li>';
	}
}
?>

Any help would be great, really puzzling me as to why it won’t work on some pages but is fine on others

My guess is it is trying to run result.php inside the store/store_name folder when using http://www.mywebsite.co.uk/store/store_name
You can verify this by looking at your logs to see if it performed a POST against http://www.mywebsite.co.uk/store/store_name/result.php

If that is the case, you just need to either 1) give the full path to result.php (ie: http://www.mywebsite.co.uk/result.php – safest method), or you could use …/…/result.php (may not work for other rewrite rules).

Matt

Thanks for the tip but it’s not working. I’ve stripped the search.php file down so that it displays an alert box when a key is pressed in the search box and again it works on some pages but those that have dynamic urls, the alert box does not pop up and I have no errors.

My search.js now looks like this:

$(document).ready(function() {
$(‘.searchtxt’).keyup(function() {
alert(‘works’);
});
});

This is just to test it works

Give an absolute path to your search.js file too. ie: <script src=“http://www.mywebsite.co.uk/search.js”></script>

Cpradio, I owe you. Thanks it was the link. Many thanks