www.webdeveloper.com
Results 1 to 6 of 6

Thread: Help creating a PHP Form

  1. #1
    Join Date
    Nov 2006
    Posts
    27

    Help creating a PHP Form

    Hi,
    I have next to no experience using php. I've been through some basic tutorials which I think I understand but I've been asked to build a form which I thought would be basic but I cannot figure it out. Can someone help?

    This is my form so far:
    HTML Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
     
    <?php
     
    $sizes = array('Small', 'Medium', 'Large');
    $quantities = array('1,000', '2,000', '3,000', '5,000', '10,000', '20,000');
    $prints = array('1 Colour', '2 Colour', '3 Colour', 'Photographic');
     
    ?>
     
     
     
    </head>
     
    <body>
     
    <form name="bespoke" action="#" METHOD="POST">
        <?php echo '<select name="choose_sizes">';
        foreach ($sizes as $key => $value) {
        echo "<option value=\"$key\">$value</option\n";
        }
        echo '</select>';
        ?>
        
        <?php echo '<select name="choose_quantities">';
        foreach ($quantities as $key => $value) {
        echo "<option value=\"$key\">$value</option\n";
        }
        echo '</select>';
        ?>
        
        <?php echo '<select name="choose_print">';
        foreach ($prints as $key => $value) {
        echo "<option value=\"$key\">$value</option\n";
        }
        echo '</select>';
        ?>
     
    <input type="submit" name"calculate" value="Calculate" />
     
    <input type="text" name="total" value="" />
     
     
    </form>
     
    </body>
    </html>


    What I want is to select one option from each drop down, press calculate and the cost appears in the <input type="text" name="total" value="" />

    This is the information I have been given:
    [code=text]1 Colour:

    Small Medium Large
    1,000 90 + VAT 95 100
    2,000 72 + VAT 77 82
    3,000 65 69 73
    5,000 59 63 68
    10,000 53 58 63
    20,000 48 53 59


    2 Colours:

    Small Medium Large
    1,000 95 100 105
    2,000 76 81 86
    3,000 68 72 76
    5,000 61 65 70
    10,000 55 60 65
    20,000 50 55 60


    3 Colours:

    Small Medium Large
    1,000 100 105 110
    2,000 80 85 90
    3,000 71 75 79
    5,000 64 68 72
    10,000 58 62 66
    20,000 52 56 61


    Photographic:

    Small Medium Large
    1,000 150 160 170
    2,000 121 130 139
    3,000 97 105 114
    5,000 79 86 94
    10,000 66 72 79
    20,000 55 61 67
    [/code]

    Can someone help me with this?

    Thanks
    Kirsty

  2. #2
    Join Date
    Apr 2008
    Location
    Norway
    Posts
    16
    I suggest making a new php file (f.e. calculate.php)
    So that when you press Submit button the values of the selected objects are sent to the calculate.php
    Based on those values calculate.php can descide what is the answer.

    That's a good start point and a good practice!

    Later you can just change file name so that you submit the given values to the same php file, and make IT calculate the answer using same method you did in calculate.php

    Hope that helps you out.

    Dalisra

  3. #3
    Join Date
    Nov 2006
    Posts
    27
    Ok great,

    so if I set the action of my form to calculate.php I can set all the variables etc. in the new file and then echo them in my original form?

    I've made a start trying to get my form working using switch. My thinking is if I can get one dropdown working I can build on that and add the other two:

    [html]
    <?php
    switch ($choose_sizes) {

    case 'Small':
    echo '<input type="text" name="total" value="90" />';
    break;

    case 'Medium':
    echo '<input type="text" name="total" value="95" />';
    break;
    case 'Large':
    echo '<input type="text" name="total" value="100" />';
    break;
    }
    ?>
    [\html]
    I'm thinking that if $choose_sizes takes its values from the drop down 'choose_sizes' then if I select Medium it should display the text area with 95 in it. Unfortunately I can't get this to work. It just doesn't display anything.

  4. #4
    Join Date
    Apr 2008
    Location
    Norway
    Posts
    16
    Well, let's see:

    You don't need to calculate values for each of them.
    When you make a selection in your form and press Calculate button, all values are sent, in your case it is still "#".
    So if you change action into action="calc.php" and press button Calculate those values that are selected will be sent to the calc.php. Then you can use (in calc.php):
    $size = $_POST["choose_sizes"];

    to get the size that was selected. (Egzample if $size == 0 then user selected small, if $size == 1 then user selected medium etc..)
    This way you can find values that have been submited and then using them descide what is the cost.

  5. #5
    Join Date
    Mar 2007
    Location
    Orlando
    Posts
    670
    Is there a need to post back to the server? Wouldn't JavaScript be better suited for this?

  6. #6
    Join Date
    Apr 2008
    Location
    Norway
    Posts
    16
    That's a possible way of doing it! Maybe even better?
    I just thought that she wanted to learn how to do it in php..

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles