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 5 of 5

Thread: PHP Question

  1. #1
    New to the CF scene
    Join Date
    Jun 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    PHP Question

    Hello,

    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?

    Thanks!

  • #2
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,536
    Thanks
    45
    Thanked 259 Times in 256 Posts
    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).

  • #3
    New to the CF scene
    Join Date
    Jun 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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")
    else
    alert("Sorry, we don't deliver to you: ")

    }
    </script>

    <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>
    </p></div></FORM>


    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?

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Use an else if (frm.firstName.value == "75647") instead of the if.

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

  • #5
    Regular Coder
    Join Date
    May 2012
    Location
    France
    Posts
    224
    Thanks
    0
    Thanked 32 Times in 30 Posts
    Something like this ?
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <meta http-equiv="content-type" content="text/html; charset=windows-1252">
    <meta name="generator" content="PSPad editor, www.pspad.com">
    <title></title>
    <style type="text/css">
    body{background:#ccc;text-align:center;}
    #cod{display:block;width:300px;margin:0 auto;}
    #zip{text-align:center;}
    a{text-decoration:none;color:#909;}
    a,p{margin:0 auto;}
    </style>
    </head>
    <body>
    <div id="cod"><fieldset><legend>Zip code</legend>
    <input id="zip" type="text" onkeyup="test(this.value)">
    <p>&nbsp;</p>
    <p id="rsp"></p>
    </fieldset></div>
    <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.'
    }	
    </script>
    </body>
    </html>
    See this tread for further developments...
    Last edited by 007julien; 06-30-2012 at 12:48 AM. Reason: complements


  •  

    Posting Permissions

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