Page 1 of 1

Help creating a PHP Form

Posted: Tue Apr 22, 2008 2:13 pm
by KirstyBurgoine
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:

Code: Select all

<!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: Select all

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
 
Can someone help me with this?

Thanks
Kirsty

Re: Help creating a PHP Form

Posted: Wed Apr 23, 2008 9:18 pm
by califdon
PHP is a server language, so it can never detect when a user does anything, its job is completed before the page is ever sent to the browser. You can build the form with PHP, as you did, but then you will have to either use Javascript, which is a browser language, to detect the button click, do the calculations and display the result; or send the form data back to the server for the calculations and send a new HTML page to the browser; or use Ajax, which is a combination of Javascript and a server side script. I'd suggest the first option, but of course you'll have to learn enough Javascript to be able to do that.

Re: Help creating a PHP Form

Posted: Wed Apr 23, 2008 9:18 pm
by califdon
PHP is a server language, so it can never detect when a user does anything, its job is completed before the page is ever sent to the browser. You can build the form with PHP, as you did, but then you will have to either use Javascript, which is a browser language, to detect the button click, do the calculations and display the result; or send the form data back to the server for the calculations and send a new HTML page to the browser; or use Ajax, which is a combination of Javascript and a server side script. I'd suggest the first option, but of course you'll have to learn enough Javascript to be able to do that.