Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Mar 2006
    Posts
    459
    Thanks
    3
    Thanked 0 Times in 0 Posts

    AJAX Postcode Search Box

    Hi all... I have a little AJAX search box on a sign up page on my site, basically the user type's in a postcode and it gives them their nearest store... The only thing is as it's on a sign-up page, if they hit enter it runs the php page, it works fine if they just type in the postcode and on the last key up runs the php, how can I make the script only run on hitting the last postode... e.g '200...0' (run script)...

    PHP Code:
    <html>
    <
    head>
     
    <
    script src="http://yui.yahooapis.com/2.2.2/build/yahoo-dom-event/yahoo-dom-event.js" type="text/javascript"></script>
    <script src="http://yui.yahooapis.com/2.2.2/build/connection/connection-min.js" type="text/javascript"></script>
    <script type="text/javascript">
    var $D = YAHOO.util.Dom;     //YAHOO DOM
    var $E = YAHOO.util.Event; //YAHOO Event
    var $C = YAHOO.util.Connect; //YAHOO connection manager
     
    function init() {
        $E.on('Code', 'keyup', chkZip);
    }
     
    //See if code is fully entered (8 digits)
    function chkZip(e) {
        var zipCode = $E.getTarget(e).value;
        if(zipCode.length < 4) return; //Ignore if not complete
        
        var storeSpan = document.getElementById('store'); //Element to put store name into
        var AjaxObj = {
            success: function(o) {
                storeSpan.innerHTML = o.responseText;
            },
            failure: function(o) {
                storeSpan.innerHTML = "<em>Error - Please try again</em>";
            },
            timeout: 5000
        }
        $C.asyncRequest('GET', 'testFindStore.php?zipCode=' + encodeURIComponent(zipCode), AjaxObj);
                
    }
    $E.onDOMReady(init);
    </script>
    </head>
    <body>
     
    <form method="post" action="testFindStore.php">
    <input type="text" name="Code" id="Code"><span id="store"></span>
    </form>
     
    </body>
    </html> 
    PHP Code:
    <?php

    if(!isset($_GET['zipCode']) OR !preg_match('/^[0-9]{4}$/'$_GET['zipCode'])) exit('Invalid Code code'); 

    //connect to db
    include 'library/config.php';
    include 
    'library/opendb.php';

    $query "SELECT * FROM table WHERE `postcode` = '" $_GET['zipCode'] . "'";

    $result mysql_query($query);
    while(
    $r mysql_fetch_assoc($result)) {
      exit(
    $r['storename']." ".$r['storename']);
    }
    exit(
    'No store found');

    include 
    'library/closedb.php'

    ?>

  • #2
    Regular Coder
    Join Date
    Mar 2006
    Posts
    459
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Anybody?

  • #3
    Senior Coder
    Join Date
    Mar 2003
    Location
    Atlanta
    Posts
    1,037
    Thanks
    14
    Thanked 30 Times in 28 Posts
    So is your only problem with them hitting the enter key? You check for the enter key and have it return false from within that text box if so.

    Code:
    //modification of DynamicDrive.com's handleEnter function
    function handleEnter (field, event)
    {
    	var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
    	if (keyCode == 13) {
    		return false;
    	} 
    }
    .....
    <form method="post" action="testFindStore.php">
    <input type="text" name="Code" id="Code" onkeypress="handleEnter(this,event);"><span id="store"></span>
    </form>
    I haven't tested the code but if it doesn't work it should put you on the right course.
    Most of my questions/posts are fairly straightforward and simple. I post long verbose messages in an attempt to be thorough.


  •  

    Posting Permissions

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