www.webdeveloper.com
Recent Articles
  • Finding Slow Running Queries in ASE 15
  • A More Advanced Pie Chart for Analysis Services Data
  • Adobe AIR Programming Unleashed: Working with Windows
  • Performance Testing SQL Server 2008's Change Data Capture Functionality
  • The ABC's of PHP: Introduction to PHP
  • How to Migrate from BasicFiles to SecureFiles Storage
  • Why the Twitter Haters Are Wrong
  • User Personalization with PHP: Beginning the Application
  • Whats in an Oracle Schema?
  • Lighting Enhancement in Photoshop
  •  

    Go Back   WebDeveloper.com > Server-Side Development > PHP

    PHP Discussion and technical support for using and deploying PHP based websites.

    Reply
     
    Thread Tools Search this Thread Rate Thread Display Modes
      #1  
    Old 12-21-2005, 06:57 PM
    Sam Granger Sam Granger is offline
    Registered User
     
    Join Date: Oct 2005
    Posts: 46
    PHP & AJAX Script problem

    search.php
    PHP Code:
    <?php

    if (isset($_GET['q']) === FALSE) {
         
        exit();
    }
        
    $root = '../';
        include(
    $root . 'connect.php');



    $sQuery =  
    "SELECT *
    FROM news
    WHERE NewsContent LIKE '%"
    . mysql_real_escape_string(trim($_GET['q'])) ."%'";

    $rResult = mysql_query($sQuery);
    if (
    $rResult === FALSE) {
         
        print
    'Kon de query '. $sQuery .' niet uitvoeren.';

    }

    $aTmp  = array();


    if (
    mysql_num_rows($rResult) == 0) {
         
        print
    'No Results';
    }

    else {
         
        while (
    $aTmp = mysql_fetch_assoc($rResult)) {
             
        echo
    "<h1>{$aTmp['NewsTitle']}</h1>";
        echo
    substr($aTmp['NewsContent'],0,300) . '...';
        echo
    "<a href=\"{$aTmp['Url']}\" title=\"{$aTmp['NewsTitle']}\">Read More</a><br />";
        }
    }

    ?>
    search.html

    HTML Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
    <html xmlns="http://www.w3.org/1999/xhtml" > 
    <head> 
    <title>Live zoeken in je database.</title> 
    <script language="Javascript"  type="text/javascript"> 
    
    // Het script die daadwerkelijk het zoeken doet 
    var url = "./scripts/search.php?q="; 
    
    function handleHttpResponse() { 
        // Hier wordt de output van het script gehaald. 
        if (http.readyState == 4 && http.status == 200) { 
          document.getElementById('results').innerHTML = http.responseText; 
        } 
    } 
    
    var done = false; 
    
    function liveSearch() { 
      if (!done && http) { 
    
            var term = document.getElementById('searchterm').value; 
            document.getElementById('results').innerHTML = ""; 
            http.open('GET', url + term, true); 
            http.onreadystatechange = handleHttpResponse; 
            done = true; 
            http.send(null); 
      } 
    } 
    
    
    function getHTTPObject() { 
      var xmlhttp; 
      /*@cc_on 
      @if (@_jscript_version >= 5) 
        try { 
          xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); 
        } catch (e) { 
          try { 
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
          } catch (E) { 
            xmlhttp = false; 
          } 
        } 
      @else 
      xmlhttp = false; 
      @end @*/ 
      if (!xmlhttp && typeof XMLHttpRequest != 'undefined') { 
        try { 
          xmlhttp = new XMLHttpRequest(); 
        } catch (e) { 
          xmlhttp = false; 
        } 
      } 
      return xmlhttp; 
    } 
    var http = getHTTPObject(); 
    
    </script> 
    </head> 
    <body> 
    <form action="post"> 
      <p>Zoekterm:</p> 
      <input type="text" id="searchterm" value=""> 
      <input type="button" id="search" value="Zoek!" onclick="liveSearch();"> 
    </form> 
    <div id="results">&nbsp;</div> 
    </body> 
    </html>
    The above script searches a database and it displays results. It all works. However, once I see the results, I can't search again without refreshing the search.html - even though I still can see the search box etc...

    How can I make it so that when I would click on search again it clears all the current results and displays the newer results?

    Thanks in advance,

    Sam
    Reply With Quote
      #2  
    Old 12-21-2005, 07:09 PM
    chazzy's Avatar
    chazzy chazzy is offline
    Working class hero
     
    Join Date: Aug 2005
    Location: The Garden State
    Posts: 5,635
    it's because you're setting done to true when complete. why do you need this?
    you can try this though
    Code:
    function handleHttpResponse() { 
        // Hier wordt de output van het script gehaald. 
        if (http.readyState == 4 && http.status == 200) { 
          document.getElementById('results').innerHTML = http.responseText; 
          done = false;
        } 
    }
    __________________
    Acceptable Use | SQL Forum FAQ | celery is tasteless | twitter

    celery is tasteless - currently needing some UI time
    Reply With Quote
      #3  
    Old 12-21-2005, 07:15 PM
    Sam Granger Sam Granger is offline
    Registered User
     
    Join Date: Oct 2005
    Posts: 46
    Perfect!! Thanks mate!

    Works like a dream
    Reply With Quote
      #4  
    Old 12-22-2005, 04:25 AM
    Sam Granger Sam Granger is offline
    Registered User
     
    Join Date: Oct 2005
    Posts: 46
    Also, to search I have to press the button. I can't press enter because i get redirected to "www.domain.com/post?"

    Any tips?
    Reply With Quote
      #5  
    Old 12-22-2005, 04:33 AM
    SpectreReturns SpectreReturns is offline
    Quite Uber
     
    Join Date: Feb 2005
    Location: BC, Canada
    Posts: 1,100
    Not a clue ^^
    __________________
    Corn is no place for a mighty warrior!
    Reply With Quote
      #6  
    Old 12-22-2005, 05:24 AM
    chazzy's Avatar
    chazzy chazzy is offline
    Working class hero
     
    Join Date: Aug 2005
    Location: The Garden State
    Posts: 5,635
    pressing enter submits your form, this line is wrong
    Code:
    <form action="post">
    should be
    Code:
    <form action="#" method="post">
    see if that helps any.
    __________________
    Acceptable Use | SQL Forum FAQ | celery is tasteless | twitter

    celery is tasteless - currently needing some UI time
    Reply With Quote
      #7  
    Old 12-22-2005, 07:01 AM
    Sam Granger Sam Granger is offline
    Registered User
     
    Join Date: Oct 2005
    Posts: 46
    Thanks - that worked. but now i've make it on keyup and down press that it displays results without even pressing the search button. (I removed the search button too)but how do i make that pressing enter doesn't do anything at all (at the moment it sort of refreshes the page).
    Reply With Quote
      #8  
    Old 12-22-2005, 12:38 PM
    chazzy's Avatar
    chazzy chazzy is offline
    Working class hero
     
    Join Date: Aug 2005
    Location: The Garden State
    Posts: 5,635
    that's browser dependent behavior, IIRC, and you really shouldn't play with it (because fixing it in a browser may break your form in another, for example)
    the best place to ask that is in the javascript forum, they might be able to give you some pointers.
    __________________
    Acceptable Use | SQL Forum FAQ | celery is tasteless | twitter

    celery is tasteless - currently needing some UI time
    Reply With Quote
      #9  
    Old 12-22-2005, 03:45 PM
    bokeh's Avatar
    bokeh bokeh is offline
    Keep it simple, stupid!
     
    Join Date: Jan 2005
    Location: Alicante (Spain)
    Posts: 7,705
    Quote:
    Originally Posted by Sam Granger
    how do i make that pressing enter doesn't do anything at all.
    Something like the following works in firefox and IE (but I'm no javascript expert).
    PHP Code:
    <head>
    <
    script type="text/javascript">
    document.onkeydown = keyHit;
    var
    thisKey;

    function
    keyHit(evt)
    {
        if (
    evt){
            
    thisKey = evt.which;
        }else{
            
    thisKey = window.event.keyCode;
        }
    }

    </script>    
    </head>

    <body onkeypress="if(thisKey == 13)return false;">
    </body>
    As a side note, I've run scripts onkeypress before (see the regex checker in my signature) but you are querying the DB too on each request. Be careful not to overwhelm the server. I don't have any trouble with mine but my server load in general is pretty light.
    Reply With Quote
      #10  
    Old 12-22-2005, 06:07 PM
    Sheldon's Avatar
    Sheldon Sheldon is offline
    www.inboxdesign.co.nz
     
    Join Date: Feb 2005
    Location: Kaukapakapa
    Posts: 2,050
    What about a browser with Javascript disabled, They would not be able to submit hte search form? Maybe use a discret image as a secondry option to submit the form?
    __________________
    Auckland, New Zealand, Web Design & Hosting. - Inbox Design

    Sheldon Lendrum, Technology, PHP, Mootools & More...

    Simple Site a Completely Dynamic site using text files, PHP and no mySQL.
    Reply With Quote
      #11  
    Old 12-22-2005, 09:33 PM
    chazzy's Avatar
    chazzy chazzy is offline
    Working class hero
     
    Join Date: Aug 2005
    Location: The Garden State
    Posts: 5,635
    Quote:
    Originally Posted by bokeh
    Something like the following works in firefox and IE (but I'm no javascript expert).
    PHP Code:
    <head>
    <
    script type="text/javascript">
    document.onkeydown = keyHit;
    var
    thisKey;

    function
    keyHit(evt)
    {
        if (
    evt){
            
    thisKey = evt.which;
        }else{
            
    thisKey = window.event.keyCode;
        }
    }

    </script>    
    </head>

    <body onkeypress="if(thisKey == 13)return false;">
    </body>
    As a side note, I've run scripts onkeypress before (see the regex checker in my signature) but you are querying the DB too on each request. Be careful not to overwhelm the server. I don't have any trouble with mine but my server load in general is pretty light.
    The only change I'd recommend is for it to reference the text box, so
    Code:
    document.getElementById('searchterm').onkeyup = keyHit;
    __________________
    Acceptable Use | SQL Forum FAQ | celery is tasteless | twitter

    celery is tasteless - currently needing some UI time
    Reply With Quote
      #12  
    Old 12-23-2005, 04:11 AM
    bokeh's Avatar
    bokeh bokeh is offline
    Keep it simple, stupid!
     
    Join Date: Jan 2005
    Location: Alicante (Spain)
    Posts: 7,705
    Quote:
    Originally Posted by chazzy
    The only change I'd recommend is for it to reference the text box, so
    Code:
    document.getElementById('searchterm').onkeyup = keyHit;
    By the way I believe the the number is different for onkeydown, onkeypress, and onkeyup.
    Reply With Quote
    Reply

    Bookmarks


    Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
     
    Thread Tools Search this Thread
    Search this Thread:

    Advanced Search
    Display Modes Rate This Thread
    Rate This Thread:

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is Off
    HTML code is Off
    Forum Jump


    All times are GMT -5. The time now is 01:53 AM.



    Acceptable Use Policy

    internet.comMediabistrojusttechjobs.comGraphics.com

    WebMediaBrands Corporate Info


    Advertise | Newsletters | Feedback | Submit News

    Legal Notices | Licensing | Permissions | Privacy Policy

    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.