Go Back   CodingForums.com > :: Client side development > JavaScript programming > Ajax and Design

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-22-2007, 03:01 AM   PM User | #1
tomyknoker
Regular Coder

 
Join Date: Mar 2006
Posts: 459
Thanks: 3
Thanked 0 Times in 0 Posts
tomyknoker is an unknown quantity at this point
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'

?>
tomyknoker is offline   Reply With Quote
Old 07-23-2007, 01:31 AM   PM User | #2
tomyknoker
Regular Coder

 
Join Date: Mar 2006
Posts: 459
Thanks: 3
Thanked 0 Times in 0 Posts
tomyknoker is an unknown quantity at this point
Anybody?
tomyknoker is offline   Reply With Quote
Old 07-24-2007, 12:13 AM   PM User | #3
StupidRalph
Senior Coder

 
Join Date: Mar 2003
Location: Atlanta
Posts: 1,037
Thanks: 14
Thanked 30 Times in 28 Posts
StupidRalph is on a distinguished road
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.
StupidRalph is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
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 On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:22 PM.


Advertisement
Log in to turn off these ads.