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 7 of 7
  1. #1
    Regular Coder
    Join Date
    Jul 2007
    Posts
    571
    Thanks
    25
    Thanked 28 Times in 28 Posts

    My First Script - Any Tips?

    Hey, i Just made my first script ever! (YA!!!)

    It is a very basic script that will give people a quote depending on what options they select from a form.

    Anyway,It works fine but i would just like someone to look over it and tell me if there is a way i can write cleaner, point out any mistakes i make, and any improvements i should consider applying to my future scripts. THANKS!

    PHP Code:
    <?php
    //Only process if Send button was pressed
    if (array_key_exists('send'$_POST)) {

    //Assign Value according to numer of pages requires
      
    if ($_POST['pages'] == 'five')  {
      
    $multi_pages ='10.00';
      }
      elseif (
    $_POST['pages'] == 'ten'){
      
    $multi_pages '20.00';
      }
      elseif (
    $_POST['pages'] == 'fifteen'){
      
    $multi_pages '30.00';
      }
      elseif (
    $_POST['pages'] == 'over_fifteen'){
      
    $multi_pages '40.00';
      }
      else {
      
    $multi_pages '0';
      }

    //Assign Value according to numer of images to be supplied  
        
    if ($_POST['images'] == 'no')  {
      
    $multi_images '10.00';
      }
      elseif (
    $_POST['images'] == 'few'){
      
    $multi_images '$20.00';
      }
      elseif (
    $_POST['images'] == 'moderate'){
      
    $multi_images '30.00';
      }
      elseif (
    $_POST['images'] == 'lots'){
      
    $multi_images '40.00';
      }
      elseif (
    $_POST['images'] == 'all'){
      
    $multi_images '50.00';
      }
      else {
      
    $multi_images '0';
      }
      
    }
    ?>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Quotes</title>
    </head>

    <body>

            <form id="quotes" method="post" action="">
            
            <p>How many pages will your site require?</p>
            <input type="radio" name="pages" id="pages-five" value="five"> 5
            <input type="radio" name="pages" id="pages-ten" value="ten">  6-10
            <input type="radio" name="pages" id="pages-fifteen" value="fifteen">  11-15
            <input type="radio" name="pages" id="pages-over16" value="over_fifteen">  16+
             <p>
             
            <p>Will you provide us with any images to be used in this project?</p>
            <input type="radio" name="images" id="images-no" value="no"> No
            <input type="radio" name="images" id="images-few" value="few">  A Few
            <input type="radio" name="images" id="images-moderate" value="moderate">  Moderate
            <input type="radio" name="images" id="images-lots" value="lots">  Lots
            <input type="radio" name="images" id="images-all" value="all">  All
             <p>
             
            <input name="send" id="send" type="submit" value="Get Quote" />
            </p>
            
            </form>
    <p> Your Quote:
    <?php
    if (isset($_POST['pages']) && isset($_POST['images'])) {
    echo 
    '$';
    echo 
    $multi_pages $multi_images;
    }
    else {
    echo 
    'Please fill in all fields';
    }
    ?></p>
    Last edited by srule_; 07-28-2007 at 04:54 PM.

  • #2
    Senior Coder
    Join Date
    Aug 2005
    Posts
    1,119
    Thanks
    2
    Thanked 1 Time in 1 Post
    I personally would use a switch for this, it would clean it up quite alot.

    http://us3.php.net/switch

  • #3
    Regular Coder
    Join Date
    Jul 2007
    Location
    United Kingdom
    Posts
    159
    Thanks
    2
    Thanked 15 Times in 15 Posts
    A switch would be great for this... and it looks very clean... the only problem there is that I don't think you can use variables within a switch...which means that you won't be able to add up the cost of the pages and the cost of the images.

    Just for your own personal reference... this is how you would use the switch for your case... but still make reference to the URL thesavior posted =]

    PHP Code:
    if (array_key_exists('send'$_POST)) {
     
    switch (
    $_POST['pages']) {
    case 
    five:
        echo 
    '$10.00';
        break;
    case 
    ten:
        echo 
    '$20.00';
        break;
    case 
    fifteen:
        echo 
    '$30.00';
        break;
    case 
    over_fifteen:
        echo 
    '$40.00';
        break;
    default:
        echo 
    '$0.00';
        break;
    }


    ~Jordan

  • #4
    Senior Coder NancyJ's Avatar
    Join Date
    Feb 2005
    Location
    Bradford, UK
    Posts
    3,174
    Thanks
    19
    Thanked 66 Times in 65 Posts
    You could eliminate the need for switches altogether if you used more useful form values.

    PHP Code:
    <?php
    //Only process if Send button was pressed
    if (array_key_exists('send'$_POST)) {

    //Assign Value according to number of pages requires
      
    $multi_pages $_POST['pages'] ? number_format($_POST['pages']*2,2) : 0;

    //Assign Value according to numer of images to be supplied  
    $multi_images $_POST['images'] ? number_format($_POST['images']*2,10) : 0;
      
    }
    ?>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Quotes</title>
    </head>

    <body>

            <form id="quotes" method="post" action="">
            
            <p>How many pages will your site require?</p>
            <input type="radio" name="pages" id="pages-five" value="5"> 5
            <input type="radio" name="pages" id="pages-ten" value="10">  6-10
            <input type="radio" name="pages" id="pages-fifteen" value="15">  11-15
            <input type="radio" name="pages" id="pages-over16" value="20">  16+
             <p>
             
            <p>Will you provide us with any images to be used in this project?</p>
            <input type="radio" name="images" id="images-no" value="1"> No
            <input type="radio" name="images" id="images-few" value="2">  A Few
            <input type="radio" name="images" id="images-moderate" value="3">  Moderate
            <input type="radio" name="images" id="images-lots" value="4">  Lots
            <input type="radio" name="images" id="images-all" value="5">  All
             <p>
             
            <input name="send" id="send" type="submit" value="Get Quote" />
            </p>
            
            </form>
    <p> Your Quote:
    <?php
    if (isset($_POST['pages']) && isset($_POST['images'])) {
    echo 
    '$'.($multi_pages $multi_images);
    }
    else {
    echo 
    'Please fill in all fields';
    }
    ?></p>
    If you want to validate that form values are only the values you want to allow, replace
    PHP Code:
    //Assign Value according to number of pages requires
      
    $multi_pages $_POST['pages'] ? number_format($_POST['pages']*2,2) : 0;

    //Assign Value according to numer of images to be supplied  
    $multi_images $_POST['images'] ? number_format($_POST['images']*2,10) : 0
    with
    PHP Code:
    //Assign Value according to number of pages requires
      
    $allowed = array(5,10,15,20); 
      
    $multi_pages in_array($_POST['pages'],$allowed) ? number_format($_POST['pages']*2,2) : 0;

    //Assign Value according to numer of images to be supplied 
      
    $allowed = array(1,2,3,4,5);  
      
    $multi_images in_array($_POST['images'],$allowed) ? number_format($_POST['images']*2,10) : 0
    Last edited by NancyJ; 07-28-2007 at 07:35 PM.

  • #5
    Regular Coder
    Join Date
    Jul 2007
    Posts
    571
    Thanks
    25
    Thanked 28 Times in 28 Posts
    You could eliminate the need for switches altogether if you used more useful form values.
    thank you nancy this solution u provided highlights of my main frustration with php in which i never know if there is a good function i could use to simplify my code.

  • #6
    Regular Coder
    Join Date
    Apr 2007
    Posts
    141
    Thanks
    3
    Thanked 1 Time in 1 Post
    your first script definitely more complex than mine.
    while mine was
    <?php
    echo "hello world";
    ?>
    lol
    actually I never created this script...but.....???!!!!

  • #7
    Regular Coder
    Join Date
    Jul 2007
    Posts
    571
    Thanks
    25
    Thanked 28 Times in 28 Posts
    that was prob my 1st too lol~~!

    this was the 1st i didn't copy from a book tho, kinda sad we had to copy something like that at one point! haha


  •  

    Posting Permissions

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