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';
?>