SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Aug 2007
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    A return value from an array and displaying a corresponding alert message

    Hi guys,

    This code is obviously wrong however I wanted to outline the aim of it.

    After entering a value of say "4000" an alert box would come up and say "Your postage is...". If they entered a value of "2000" it would display another message which I haven't made up yet.

    Basically after entering a value the script compares it to which array it is in and produces the corresponding message. Any ideas?

    Thanks in advance,

    A

    <script language="Javascript" type="text/javascript">
    var zoneChart = new Array;
    zoneChart[0] = new Array("4000.4416","4418.4419","4421.4427");
    zoneChart[1] = new Array("2000.2010","3000.3010");

    var zoneChart = prompt("Please enter your postcode","")
    if (zoneChart =="0")
    {
    alert("Your postage is $24.95 and delivery time is 2 days")
    }
    else
    {
    alert("Please enter a valid postcode")
    }
    </script>

  2. #2
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    could you explain further what your arrays contain? Also, if you are using STATIC data, then its best to use a switch.

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
          "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
            <title>Untitled Document</title>
            <link rel="stylesheet" type="text/css" media="screen" />
            <style type="text/css"></style>
            <script type="text/javascript">
                function getPostage() {
                    var zoneChart = new Array;
                        zoneChart[0] = new Array("4000.4416","4418.4419","4421.4427");
                        zoneChart[1] = new Array("2000.2010","3000.3010");
                    var postCode = prompt("Please enter your postcode","")
                    if(!postCode || postCode.toLowerCase() == 'null') {
                        alert('error, no postcode given!');
                        return;
                    }
                    switch(postCode) {
                        case '4000' :
                            alert('you typed in 4000');
                        break;
                        case '2000' :
                            alert('you typed in 2000');
                        break;
                        default :
                            alert('you typed in something I don\'t know yet!');
                        break;
                    }
                }
            </script>
        </head>
        <body>
            
        </body>
    </html>


  3. #3
    SitePoint Member
    Join Date
    Aug 2007
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey gRoberts,

    The arrays contains values for example in the first array numbers between 4000-4416, 4418-449, 4421-4427. The second array holds values between 2000-2346.

    The idea is is the user types in say 4001 it will return an alert box of "Your postage will be xxx amount etc. If the user types in 2000 *ie the second array* the alert box will be "Your postage will be a different amount".

    I hope I have explained this moderately.

    A

  4. #4
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Right, well this could be recoded to be alot easier.

    Unless there is a specific reason to have an array of arrays of post codes, then it should all be in one, and with that i've changed the array. It is now seperated by pipes (|) and also includes the message that will be displayed for anything between that range of post codes.

    HTH

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
          "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
            <title>Untitled Document</title>
            <link rel="stylesheet" type="text/css" media="screen" />
            <style type="text/css"></style>
            <script type="text/javascript">
                function getPostage() {
                    var zoneChart = new Array("4000|4416|4000-4416 Message","4418|4419|4418-4419 Message","4421|4427|4421-4427 Message", "2000|2010|2000-2010 Message", "3000|3010|3000-3010 Message");
                    var postCode = prompt("Please enter your postcode","")
                    if(!postCode || postCode.toLowerCase() == 'null') {
                        alert('error, no postcode given!');
                        return;
                    }
                    for(var i = 0; i < zoneChart.length; i++) {
                        var data = zoneChart[i].split('|');
                        if(postCode >= data[0] && postCode <= data[1]) {
                            alert(data[2]);
                            return;
                        }
                    }
                    alert('unknown postcode');
                }
            </script>
        </head>
        <body>
            
            <input type="button" onclick="getPostage()" value="click" />
    
        </body>
    </html>


  5. #5
    SitePoint Member
    Join Date
    Aug 2007
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi gRoberts,

    Thank you for your help. All running smoothly now

    I'll be sure to ask you in future problem cases

    Thanks again for your help

    A


Bookmarks

Posting Permissions

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