Click to See Complete Forum and Search --> : Newbie question - php for calculations?


steve_c
10-09-2007, 05:52 PM
Hi, i'm just starting on the road to php and was wondering if someone could give me a pointer...

I've created a form in html that allow users to select from 4 different price groups. The groups are comprised of radio buttons with different prices for each button...

There is also 2 dropdown menus with text options..

What i want to do is: the user selects one option from each price group.. and one text otion from each dropmenu.. then presses submit...

On pressing the submit button i would like to use php to add the selected prices together and record the text options selected in the dropmenus..

This data would then be presented on another page in the form of an invoice.

Can anyone give me any pointers as to how to go about this?

I tried search for php calculations etc.. to no avail.. i was hoping there may be free scripts available to do this but couldnt find any :(

Would the data need to go into a database?

Greatly appreciate any advice..

Thanx

Crucial
10-09-2007, 07:11 PM
Hi Steve,
This should be easy to do, although there is a lot more which should be considered for security sake.

Go through some of the tutorials here.
http://w3schools.com/php/default.asp

Especially
http://w3schools.com/php/php_get.asp

Your html input fields should each have a name
<input type='text' name='form01'>
<input type='text' name='form02'>
<input type='text' name='form03'>
<input type='text' name='form04'>

the php page you submit to will have access to what was submitted (assuming a form method of 'get') in the following variables.

$_GET['form01']
$_GET['form02']
$_GET['form03']
$_GET['form04']

To calculate a sum, simply add them together.

$my_sum = $_GET['form01'] + $_GET['form02'] + $_GET['form03'] + $_GET['form04'];

Your drop down values will be available in the same way.

steve_c
10-10-2007, 07:44 AM
Hey Crucial... thanks for the reply...

I may seem dim at the moment..cos i'm still learning this :)

I'm not sure how it works.. i've only managed to do an include in php before...

I have given the input fields a name..and given them a "get" method and an "invoice.php" action... e.g:

<form id="membership" method="get" action="invoice.php">

do i now build a new page called invoice.php ...and have the submit button call that page? e.g:

<form id="calculate" method="post" action="invoice.php">

then in the new invoice.php page...where i want the data displayed would i put:

$_GET['form01']
$_GET['form02']
$_GET['form03']
$_GET['form04']
$my_sum = $_GET['form01'] + $_GET['form02'] + $_GET['form03'] + $_GET['form04'];

I'm sure this is wrong... any pointers would be greatly received..

Thanks very much

MrCoder
10-10-2007, 08:23 AM
Copy paste code..


<?php
if(isset($_POST["action"]))
{
switch($_POST["action"])
{
case "calc" :
echo "<pre>".var_export($_POST, true)."</pre>";

// THIS IS WHERE YOU WOULD DO YOUR MATH
// AND DISPLAY THE RESULTS...

die(); // Stop the script
break;
}
}
?>
<html>
<head>
</head>
<body>

<form method="POST" action="<?php echo $_SERVER["PHP_SELF"]; ?>">
<input type="hidden" name="action" value="calc">

INSERT YOUR FORM FIELDS IN HERE

<input type="text" name="test_box" value="This is a test">

<br>

<input type="submit" value="Submit the form..">

</form>

</body>
</html>