SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Hybrid View

  1. #1
    SitePoint Addict
    Join Date
    Mar 2009
    Posts
    268
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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

    Code:
    <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

    Code:
    $(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

    Code:
    <?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

  2. #2
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,169
    Mentioned
    153 Post(s)
    Tagged
    0 Thread(s)
    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

  3. #3
    SitePoint Addict
    Join Date
    Mar 2009
    Posts
    268
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

  4. #4
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,169
    Mentioned
    153 Post(s)
    Tagged
    0 Thread(s)
    Give an absolute path to your search.js file too. ie: <script src="http://www.mywebsite.co.uk/search.js"></script>

  5. #5
    SitePoint Addict
    Join Date
    Mar 2009
    Posts
    268
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cpradio, I owe you. Thanks it was the link. Many thanks


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •