The Web Design Group

... Making the Web accessible to all.

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> calculate price from form
jackmaessen
post Apr 4 2013, 03:46 PM
Post #1


Novice
**

Group: Members
Posts: 25
Joined: 17-January 13
Member No.: 18,471



Hello,

I have a form which customers should fill in and after they do, it should calculate the price of what they have choosen:

CODE

<form id="pricecalculation" method="post" action="">
<table>
<tr>
<td>do you want a table?</td>
<td><input  type="radio" name="table_yes" value="0" />Yes</td>
<td><input  type="radio" name="table_no" value="1" />No</td>
</tr>
<tr>
<td>Do you want a chair?</td>
<td><input  type="radio" name="chair_yes" value="0" />Yes</td>
<td><input  type="radio" name="chair_no" value="1" />No</td>
</tr>
<tr>
<td>How many drinks do you want?</td>
<td><input name="drinks" type="text" id="drinks" size="3"></td>
</tr>
<tr>
<td>How many snacks do you want?</td>
<td><input name="snacks" type="text" id="snacks" size="3"></td>
</tr>
</table>
<input name="submit" type="submit" value="Calculate price"/>
</form>


About the prices:
- table: $20
- chair: $5
- drinks: $2 each
- snacks: $1 each

Can someone help me with the PHP code to realise this?
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Apr 4 2013, 06:44 PM
Post #2


.
********

Group: WDG Moderators
Posts: 8,176
Joined: 10-August 06
Member No.: 7



You could use a single checkbox instead of each pair of yes/no radio buttons. If you still want radio buttons, one should be checked by default (otherwise you won't get a straight answer from users that haven't checked any of them). You also need to use the same NAME for each group of radio buttons. It's also a bit peculiar to give the Yes choice the value "0", so I switched those.

Here's a simple script:

CODE
<?php

$table_price=20;
$chair_price=5;
$drinks_price=2;
$snacks_price=1;

$total=0;

if(isset($_POST['table']) && $_POST['table']=='1')
{
    $total=$total+$table_price;
}

if(isset($_POST['chair']) && $_POST['chair']=='1')
{
    $total=$total+$chair_price;
}

if(isset($_POST['drinks']))
{
    $num=(int)$_POST['drinks']; // make sure only integers are accepted
    if($num>=0) // ignore negative values
    {
        $total=$total+($drinks_price*$num);
    }
}

if(isset($_POST['snacks']))
{
    $num=(int)$_POST['snacks'];
    if($num>=0)
    {
        $total=$total+($snacks_price*$num);
    }
}

echo "<p>Total price is \$$total</p>\n";

?>

<form id="pricecalculation" method="post" action="">
<table>
<tr>
<td>do you want a table?</td>
<td><input  type="radio" name="table" value="1" />Yes</td>
<td><input  type="radio" name="table" value="0" checked="checked" />No</td>
</tr>
<tr>
<td>Do you want a chair?</td>
<td><input  type="radio" name="chair" value="1" />Yes</td>
<td><input  type="radio" name="chair" value="0" checked="checked" />No</td>
</tr>
<tr>
<td>How many drinks do you want?</td>
<td><input name="drinks" type="text" id="drinks" size="3"></td>
</tr>
<tr>
<td>How many snacks do you want?</td>
<td><input name="snacks" type="text" id="snacks" size="3"></td>
</tr>
</table>
<input name="submit" type="submit" value="Calculate price"/>
</form>


A more fancy version could preserve the submitted values in the form fields, or print the values as text on the page. You could also print out error messages for invalid values.
User is online!PM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
jackmaessen
post Apr 5 2013, 05:07 AM
Post #3


Novice
**

Group: Members
Posts: 25
Joined: 17-January 13
Member No.: 18,471



This works great Christian. I am learning PHP, but i am not yet on that level that i can make such things on my own at the moment. But is is very educational for me. I am trying to understand the code. Thank you very much!

This post has been edited by jackmaessen: Apr 5 2013, 05:08 AM
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Apr 5 2013, 01:22 PM
Post #4


.
********

Group: WDG Moderators
Posts: 8,176
Joined: 10-August 06
Member No.: 7



Just ask if you wonder about somehing. The PHP manual is very good too.
User is online!PM
Go to the top of the page
Toggle Multi-post QuotingQuote Post

Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



- Lo-Fi Version Time is now: 16th July 2019 - 04:24 PM