...

AJAX Postcode Search Box

tomyknoker
07-22-2007, 03:01 AM
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)...

<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

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
07-23-2007, 01:31 AM
Anybody?

StupidRalph
07-24-2007, 12:13 AM
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.


//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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum