Delete PHP variables

  jaysmoke 12:18 20 Jan 04
Locked

Can anyone tell me how to delete the value assignment statement for this php variables:

?php
$price = 10.00;
$quantity = 4;
?>

so that i can assign any amount to them using the form input like this

input type=text name=price size=10 maxlenght=10>
input type=text name=quantity size=10>

  Taran 14:26 20 Jan 04

PHP form handling is usually best done by using form field names and giving them a fixed value, then running calculations based on an amount placed in a neighbouring form field.

Let's say we have a form with one textbox. To the left of the textbox is a text description of the itee for sale - in this case, we'll call it a ruby.

So our web page reads "Please select how many rubies you would like to buy", then below this sentence there is a line of text that says:

Rubies:

and to the right of it is a textbox for you to input the number of rubies you want to buy.

Now comes the important bit. The name of the textbox is rubyqty [logical naming convention for goods offered "ruby" followed by quantity required "qty"].

So, now we can use the textbox value to calculate the cost if we have assigned a base value to ruby to begin with, like this:

define('RUBYPRICE'), 100;

$totalamount = $rubyqty * RUBYPRICE

If you link in the relevant echo statements you get a calculation based on a fixed item value from the define statement mutliplied by the number of them requested by the form user.

Type the number 4 into the textbox and you get 'defined price of ruby x 4 = ' as your calculation.

Or have I misunderstood where you're going with this ?

T

  jaysmoke 14:46 20 Jan 04

i got it but my case is that now you are you assign a price to the commodity thru the input box and it is s'pose to take that field.

say you have these codes

<?php
$price = 10.00;
$sales_tax = .0825;
$quantity = 4;
$sub_total = $price * $quantity;
$sales_tax_amount = $sub_total * $sales_tax;
$grand_total = $sub_total + $sales_tax_amount;
$sales_tax_pct = $sales_tax * 100;
$fmt_price = sprintf("%0.2f",$price);
$fmt_sub_total = sprintf("%0.2f",$sub_total);
$fmt_grand_total = sprintf("%0.2f",$grand_total);
$fmt_sales_tax_amount = sprintf("%0.2f",$sales_tax_amount);
?>
<table>
<tr><td><?php echo "You ordered $quantity bags of coffee.<br><br>"; ?></td></tr>
<tr><td><?php echo "Each bag of coffee costs \$$fmt_price each.<br><br>"; ?></td></tr>
<tr><td><?php echo "Your Sub Total is \$$sub_total.<br><br>"; ?></td></tr>
<tr><td><?php echo "Sales tax is \$$sales_tax_pct% in this area.<br><br>"; ?></td></tr>
<tr><td><?php echo "\$$fmt_sales_tax_amount has been added to your order.<br><br>"; ?></td></tr>
<tr><td><?php echo "You owe \$$fmt_grand_total for your coffee.<br><br>"; ?></td></tr>
</table>

and you want to delete the value assigned to $price and $quantity so that you can input you diesired quantity and price in the input field and get the calculations based on that....

  jaysmoke 14:51 20 Jan 04

i got it but my case is that now you are you assign a price to the commodity thru the input box and it is s'pose to take that field.

say you have these codes

<?php

$price = 10.00;

$sales_tax = .0825;

$quantity = 4;

$sub_total = $price * $quantity;

$sales_tax_amount = $sub_total * $sales_tax;

$grand_total = $sub_total + $sales_tax_amount;

$sales_tax_pct = $sales_tax * 100;

$fmt_price = sprintf("%0.2f",$price);

$fmt_sub_total = sprintf("%0.2f",$sub_total);

$fmt_grand_total = sprintf("%0.2f",$grand_total);

$fmt_sales_tax_amount = sprintf("%0.2f",$sales_tax_amount);

?>

<table>

<tr><td><?php echo "You ordered $quantity bags of coffee.<br><br>"; ?></td></tr>

<tr><td><?php echo "Each bag of coffee costs \$$fmt_price each.<br><br>"; ?></td></tr>

<tr><td><?php echo "Your Sub Total is \$$sub_total.<br><br>"; ?></td></tr>

<tr><td><?php echo "Sales tax is \$$sales_tax_pct% in this area.<br><br>"; ?></td></tr>

<tr><td><?php echo "\$$fmt_sales_tax_amount has been added to your order.<br><br>"; ?></td></tr>

<tr><td><?php echo "You owe \$$fmt_grand_total for your coffee.<br><br>"; ?></td></tr>

</table>

and you want to delete the value assigned to $price and $quantity so that you can input you diesired quantity and price in the input field and get the calculations based on that....

  Taran 19:19 20 Jan 04

You're talking about a plain calculator, where you input both values ?

I originally thought the point was to have a fixed value for item X [for the sake of argument] which would be multiplied by whatever amount was entered into a textbox.

That's the usual way of processing things, since in all cases I work with the last thing I want is for someone to tell me how many of something they want AND what price they want it at.

Is this for soem kind of assignment or will it be part of a live project ?

  jaysmoke 10:00 21 Jan 04

some assignment...

  Taran 11:39 21 Jan 04

I've been struggling to remember where I saw a similar script before and then it came to me.

PHP Essentials 2nd Edition, by Julie Meloni.

It's on my reference book shelf.

;o)

If this is for an assignment I won't be doing you any favours by offering a working solution. It's all in that book - everything you need to run the application and understand how it works.

However, if you aren't working from that book, your tutor needs a damn good kick in the pants for pinching the content from it, so...

This is the form content (needs an html document wrapper):

<FORM method="POST" action="do_calculate.php">

<P>Enter the price per bag of coffee beans: <INPUT type="text" name="price" size=10 maxlength=10></P>

<P>How many bags would you like? <INPUT type="text" name="quantity" size=10 maxlength=10></P>

<INPUT type="submit" value="Submit">

</FORM>


And this is the do_calculate.php document that does the work (also needs an html document wrapper):


<?php

$sales_tax = .0825;

$sub_total = $_POST[price] * $_POST[quantity];

$sales_tax_amount = $sub_total * $sales_tax;

$sales_tax_pct = $sales_tax * 100;

$grand_total = $sub_total + $sales_tax_amount;

$fmt_price = sprintf("%0.2f",$_POST[price]);

$fmt_sub_total = sprintf("%0.2f",$sub_total);

$fmt_sales_tax_amount = sprintf("%0.2f",$sales_tax_amount);

$fmt_grand_total = sprintf("%0.2f",$grand_total);

echo "<P>You ordered $_POST[quantity] bags of coffee.</p>";

echo "<P>Bags of coffee are \$$fmt_price each.</p>";

echo "<P>Your subtotal is \$$fmt_sub_total.</p>";

echo "<P>Sales tax is $sales_tax_pct% in this location.</p>";

echo "<P>\$$fmt_sales_tax_amount has been added to your order.</p>";

echo "<P>You owe \$$fmt_grand_total for your coffee.</p>";

?>


Note how it works, by using the form field values input by the form user rather than fixed values identified and created by you in the first instance.

Make sure you understand it. It isn't rocket science but play with it, break it, put it back together and see how it all works or you gain nothing from this.

T

  jaysmoke 10:12 22 Jan 04

okay but have you tried it yourself and see it work..seeing any results when i submit the form...the sheet is blank...does it have to do with the version of PHP am using...PHP 4

  Taran 19:26 22 Jan 04

T tested it on a default installation of Apache 1.3.29 running on Windows XP with PHP 4.3.4

My PHP install is a full install, not the silly CGI installer you can get that some people use.

I've also uploaded the above files to one of my domains and it ran perfectly.

If it isn't running it has to be something to do with the server setup.

I can't devote much time to this or anything else at the moment. I'm not even supposed to be online and the present Mrs Taran will have my guts if she finds out.

I can only suggest off the top of my head that your testing server or domain has certain restrictions in place that prevents the script from running.

T

This thread is now locked and can not be replied to.

What is Amazon Go and will it come to the UK? The store without checkouts or queues

1995-2015: How technology has changed the world in 20 years

Hands-on with the Star Wars fighting drones you can fly yourself

15 macOS Sierra tips | How to use macOS Sierra: Secret tricks and best new features in Apple's new…