Jump to content

Calculations within a page


MikeSmith2

Recommended Posts

I am trying to get the code here Restaurant02.html upgraded so the user could click on the "Price Pizza(s)" label and a function would calculate the price of the pizza(s) and put the result in the box to the right.

I don't care about the calculations, I would just like some sample code for the function and how it would be invoked.

Mike Smith

 

Link to comment
Share on other sites

Without looking at the page and under the assumption that you are using an HTML form element to collect the consumer's preferences for the type of pizza that he wishes to purchase, perform the calculation with PHP at the top of the page and send the data used to make the calculation to the script via the form element on the same page.

To send the data to the PHP script configure your form element as follows.  Note especially the values of the method and action statements.

<form id="pizza_data" method="post" name="pizza_data" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>">

And, enclose the PHP script that performs the calculation in the following if-statement.

if ($_SERVER["REQUEST_METHOD"] == "POST") { 
	[script that performs the calculation]
}

Once the calculation has been completed copy it to a Javascript variable, and then replace the form submit button with the resulting price.  Alternatively, display it elsewhere on the same page and leave the submit button in tact.

<script>
var total_price = <?php echo $total_price;?>;
</script>

Roddy

 

Edited by iwato
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...