I created a form which works using cookies in PHP. This is how the form looks like.
When I put values for price and quantity, I want them to get multiplied and the result to be displayed in the "Total Bill Value" textbox. Each time I put values for price and quantity, I want them to get added up, which is why I have used a cookie. This is my code.
<html>
<head></head>
<body>
<?php
if($_SERVER["REQUEST_METHOD"] == "POST")
{
$result=0;
if(isset($_POST["btnReset"]))
{
setcookie("result", $result);
}
elseif(isset($_POST["btnClick"]))
{
if(isset($_COOKIE["result"]))
{
$result=$_COOKIE["result"];
}
$result= $result + $_POST["Price"]*$_POST["Quantity"];
setcookie("result", $result);
}
else
{
if(isset($_COOKIE["result"]))
{
echo "something";
}
}
}
?>
<?php
if (isset($result)) {
$bill = $result;
} else {
$bill = '';
}
?>
<form method="POST" >
<ul style="list-style-type:none;">
<li>
Product Name: <input type="text" name="ProductName"> <br>
Price: <input type="number" name="Price" value="price"> <br>
Quantity: <input type="number" name="Quantity" value="qty"><br>
<input type="submit" name="btnClick" value="Add">
<input type="submit" name="btnReset" value="Clear">
<input type="submit" name="btnPrint" value="Print"><br>
Total Bill Value: <input type="text" name="Bill" value="<?php echo $bill; ?>">
</li>
</ul>
</form>
</body>
</html>
But whenever I click the "Add" button, I'm getting this error
Line 21 is this
$result= $result + $_POST["Price"]*$_POST["Quantity"];
Does anybody know why I'm getting that error??
And the other thing is, when I enter product name, price and quantity and get the total bill value and do that for many products and hit on the "Print" button, I want all the products with their total bill values to get printed out. How do I do this?
Would really appreciate any help. Thanks in advance!
Edit: So I did a var_dump to $result
and this is what I got
First thing I would do is move your PHP code above the opening<html>
tag, this will help avoid warnings withsetcookie
(for example).
I've rewritten your code slightly, renaming the buttons and values. It's been tested in PHP version 5.4.45.
<?php
// Default result value
$result = 0;
if ( !empty( $_POST ) ) {
$cookie_key = 'result';
$action = !empty( $_POST[ 'action' ] ) ? $_POST[ 'action' ] : false;
$result = !empty( $_COOKIE[ $cookie_key ] ) ? (float)$_COOKIE[ $cookie_key ] : $result;
// Add product
if ( $action === 'add' ) {
$price = !empty( $_POST[ 'Price' ] ) ? (float)$_POST[ 'Price' ] : 0;
$quantity = !empty( $_POST[ 'Quantity' ] ) ? (int)$_POST[ 'Quantity' ] : 0;
if ( $price && $quantity ) {
$result = $result + ( $price * $quantity );
}
}
// Clear result
else if ( $action === 'clear' ) {
$result = 0;
}
// Print
else if ( $action === 'print' ) {
// Handle print action
}
// Store value
setcookie( $cookie_key, $result );
}
?>
<html>
<head></head>
<body>
<form method="POST" >
<ul style="list-style-type:none;">
<li>
Product Name: <input type="text" name="ProductName"> <br>
Price: <input type="number" name="Price" value="price"> <br>
Quantity: <input type="number" name="Quantity" value="qty"><br>
<input type="submit" name="action" value="add">
<input type="submit" name="action" value="clear">
<input type="submit" name="action" value="print"><br>
Total Bill Value: <input type="text" name="Bill" value="<?php echo $result; ?>">
</li>
</ul>
</form>
</body>
</html>
Here it is in action:
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
HTML (English "hyper text markup language" - hypertext markup language) is a special markup language that is used to create sites on the Internet.
Browsers understand html perfectly and can interpret it in an understandable way. In general, any page on the site is html-code, which the browser translates into a user-friendly form. By the way, the code of any page is available to everyone.
https://www.w3.org/html/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.