I'm trying to create a script that will calculate the tip a customer should pay, based on the number of customers and the amount of the bill. I also have to create an HTML document that has a form with two text boxes, one for number of customers and one foramount of bill(without tax and gratuity). 6 or more customers at a table is 18% gratuity, 5 or less at a table is 15% gratuity. I need to be able to print the gratuity amount to the screen.Also how much sales tax at 8.75% for bill? I need to display 4 amounts , the original amount, the sales tax amount, the gratuity amount and bill total including tax and gratuity. This is for a class that I'm in and I've been racking my brain on 2 or 3 problems. I was up most of last night trying to figure them out. I love this class and want to learn, but at  some point I have to throw in the white flag. I appreciate your help! Just so you know, I'm doing this for a hobby as I'm almost 60 and retired from the Army in 2005. I really need a tutor. Thanks


This is what I have so far and it's not working in my PHP: 
<?php
$customers = $_POST ['customers'];
$bill = $_POST ['bill'];
$tax = $_POST ['tax'];



    while(customers >= 6) {


echo "$customers X $bill =", $bill .18, "<br>";
$customers++;
This is my HTML: I have 3 text boxes, Number of customers, Bill and NY Sales Tax and I also have a submit button that I named gratuity. Please help

Recommended Answers

All 10 Replies

If you want to learn best way is on w3schools.com. You have examples and tutorials how to do form with post or get methods and so much more. I think that you will learn faster if you go over that examples, and if you still have questions post a new question and we will help you.

Try this

<form name="calcBill" id="calcBill" action="" method="post">
<label for="txtCustomersNo">Customers Number</label>
<input type="text" name="txtCustomersNo" id="txtCustomersNo" value="<?php  echo isset($_POST['txtCustomersNo']) ? $_POST['txtCustomersNo'] : "1"; ?>" />
<label for="txtAmount">Amount</label>
<input type="text" name="txtAmount" id="txtAmount"  value="<?php  echo isset($_POST['txtAmount']) ? $_POST['txtAmount'] : "0"; ?>" />
<input type="submit" name="sbt" value="Calculate" />
</form>

<?php
if(isset($_POST['sbt'])) {
echo "<h2>Bill Report</h2>";
if(is_numeric($_POST['txtCustomersNo']) && is_numeric($_POST['txtAmount'])) {
if($_POST['txtCustomersNo']>=6) $gratuity = $_POST['txtAmount']*0.18;
else $gratuity = $_POST['txtAmount']*0.15;

$tax = $_POST['txtAmount']*0.0875;
?>
<table cellpadding="2" border="1">
<tr>
<td>No: of Custermers</td>
<td><?php echo $_POST['txtCustomersNo']; ?></td>
</tr>
<tr>
<td>Amount:</td>
<td><?php echo $_POST['txtAmount']; ?></td>
</tr>
<tr>
<td>Gratuity:</td>
<td><?php echo $gratuity; ?></td>
</tr>
<tr>
<td>Tax:</td>
<td><?php echo $tax; ?></td>
</tr>
<tr>
<td><strong>Total:</strong></td>
<td><?php echo $_POST['txtAmount']+$gratuity+$tax; ?></td>
</tr>
</table>
<?php
}
else {
?>  
    <h2>Wrong Number Of Customers Or Amount </h2>
<?php
}

}
?>

Thankyou so much Bachov! I usually can figure complex things out, but I'm having a lot of mental blocks lately. When I can't figure things out after hours of trying, it does work best for be to see the results and learn from them. I believe your post will work for me. I wish I could contact you in the future in reference to this subject. God Bless

You'll want to change this:

$customers = $_POST ['customers'];
$bill = $_POST ['bill'];
$tax = $_POST ['tax'];

...to this:

$customers = $_POST['customers'];
$bill = $_POST['bill'];
$tax = $_POST['tax'];

...it's better in terms of syntax.

are they not the same

This is in reference to Bachov reply above: is there a way to do this without all the <?php ?> in the table also without the $POST everywhere in the php document? I also don't understand line 10 and 12. Thanks

@burt.munn

We can print the result with in a php tag, like

<?php
if(isset($_POST['sbt'])) {
    echo "<h2>Bill Report</h2>";
    if(is_numeric($_POST['txtCustomersNo']) && is_numeric($_POST['txtAmount'])) {
    if($_POST['txtCustomersNo']>=6) $gratuity = $_POST['txtAmount']*0.18;
    else $gratuity = $_POST['txtAmount']*0.15;

    $tax = $_POST['txtAmount']*0.0875;

    echo "<table cellpadding='2' border='1'>
    <tr><td>No: of Custermers</td><td>".$_POST['txtCustomersNo']."</td></tr>
    <tr><td>Amount:</td><td>".$_POST['txtAmount']."</td></tr>
    <tr><td>Gratuity:</td><td>".$gratuity."</td></tr>
    <tr><td>Tax:</td><td>".$tax."</td></tr>
    <tr><td><strong>Total:</strong></td><td>".($_POST['txtAmount']+$gratuity+$tax)."</td></tr>
    </table>";
    }
    else    echo "<h2>Wrong Customer Number/Amount </h2>";

}
?>

&&

Line 10 -> Checking form is submit or not

Line 12-> Checking customer number and bill amount are numeric

As I said I am very new to this. Can I ask a few more questions?
1. I thought that I needed $CustomersNo = $_POST ['CustomerNo'];
$BillAmount = $_POST ['BillAmount']; at the beginning of the php to reference the HTML document that the php information will be in.
2. what is line 18 for, the wrong customer statement?
3. in line 2 the if(isset$_POST['sbt'])) what is isset and sbt?
4. line 4 what is the "is_numeric($POST['txtCustomerNo']) &&"? This is stuff that I haven't learned or seen yet. I thought I needed the statements in 1 above of this message and after that I was to list the variables. I'm just trying to understand. I do appreciate your patience. This will link to a HTML document. 5. Are the _$POST throughout the document the variables and if so, can I list them under my #1 question above?
Thanks again

@burt.munn,
first you need a HTML form to this here you can input Amount and the Number of Customer
yourhtmlfile.html

`<htm><form method= "yourphpfile.php"> Amount <input type="text" name =" amount"> Number of Customer<input type="text" name ="numberofcust"> </form></html>`

then you create a PHP file that will process the input data

`<?php
    //first check if the variables is not null
    if (isset($_POST['amount'], $_POST['numberofcust'])){
   $amount = $_POST['amount']; // This one is referring to the textbox with a name = "amount"
    $numberofCustomers = $_POST['numberofcust']; // This one is referring to the textbox with a name ="numberofcust"

    //The check if it is numeric

    if (is_numeric($numberofCustomers)){
        // Let's ask the number of customers
        $tax = ($amount*0.0875);
        if ($numberofCustomers>=6){
            $gratuity = ($amount*0.18);
          }
        else{
            $gratuity = ($amount*0.15); //if it's less than 6 
        }

       }

     }
     $total = ($total+$gratuity+$tax); //Compute for the total
     echo "<table>
             <tr>
                 <td>No of Customers</td>
                 <td>".$numberofCustomers."</td>
             </tr>

              <tr>
                 <td>Raw Amount</td>
                 <td>".$amount."</td>
             </tr>
              <tr>
                 <td>Gratuity</td>
                 <td>".$gratuity."</td>
             </tr>
              <tr>
                 <td>Tax</td>
                 <td>".$tax."</td>
             </tr>
              <tr>
                 <td>Total</td>
                 <td>".$total."</td>
             </tr>
     </table>"

    }
`?>

if you want to put a customer name in every tip i strongly suggest that you should use array.
Check www.w3schools.com for tutorial of how to pass array data to PHP.

I hope i can help :)

I do have an HTML document that has 2 text boxes, for number of customers and bill amount as well as a calculate button. I know we were taught to put $customerno=_POST['customerno']; and $billamount=_POST['billamount']; and the first 2 lines after <?php. How can I do that and still have the above php info? Respectfully

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.