View Full Version : PHP Question

06-29-2012, 08:28 PM

I'm currently working on a page for my restaurant and I was curious if there is any way PHP could create a box that the client type in there zip code to see if we deliver to that area, and in return it would get answers such as "No, we don't deliver to that zip code" and "Yes, we do deliver to that area" if I do deliver to that area.

I remember I was doing something similar in collage by using PHP and it was pretty simple but I can't remember any of the coding, any suggestions?


06-29-2012, 08:40 PM
Well, this isn't a forum where people will write your code for you, its a help forum.

Yes, you can do it with PHP, and like you suggested, it is pretty straight forward (depending on how fancy you want to be). Do you have any code at all?

I'd think if you have a list of zip codes you know you deliver to, you could get the input, and compare it to an array (if the list doesn't change often/is short).

06-29-2012, 11:04 PM
Well I started up trying some simple coding, unfortunately I used javascript though, so I guess im not on the right thread but not sure how to move it :P

Anyway, so I got this far...by using some help from google and changing it around.

<SCRIPT Language="JavaScript">function showAndClearField(frm){
if (frm.firstName.value == "75646")
alert ("We deliver to you")
if (frm.firstName.value == "75647")
alert ("We deliver to you")
alert("Sorry, we don't deliver to you: ")


<FORM NAME="test">
<center><P>Type in your zip code: <INPUT TYPE="TEXT" NAME="firstName"><BR></center>
<center><INPUT TYPE="Button" Value="Click Here" onClick="showAndClearField(this.form)"></center>

It works however, there is one error, after the message comes up for the "we deliver to you" and you press "ok" another message comes up telling you "Sorry, we don't deliver to you". Any thoughts?

Ps. How do I write stuff in coding display on this forum?

06-29-2012, 11:49 PM
Use an else if (frm.firstName.value == "75647") instead of the if.

To write with code blocks you can use
tags. PHP tags should markup anything that's C based for the most part. Code is more generic.

06-30-2012, 01:35 AM
Something like this ?

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<meta http-equiv="content-type" content="text/html; charset=windows-1252">
<meta name="generator" content="PSPad editor, www.pspad.com">
<style type="text/css">
#cod{display:block;width:300px;margin:0 auto;}
a,p{margin:0 auto;}
<div id="cod"><fieldset><legend>Zip code</legend>
<input id="zip" type="text" onkeyup="test(this.value)">
<p id="rsp"></p>
<script type="text/javascript">
// A new function to know if a value exist in an Array
Array.prototype.exists = function(s){for (var i=0; i<this.length; i++) if (this[i] == s) return true;
return false;}
// Your list of codes to complete...
var goodCodes="75646,75647".split(',');
function test(v){
var w=v.replace(/\s+/g,'');// we remove all the spaces
// We suppose all zip Codes are longer as 4 digits and remove the response otherwise
if (w.length<5) {document.getElementById('rsp').innerHTML='';return}
document.getElementById('rsp').innerHTML=(goodCodes.exists(w)?"Yes, we":"No sorry, we don't")+' deliver this zip Code.'
</html>See this tread (http://www.webdeveloper.com/forum/showthread.php?t=253001) for further developments...