Jump to content

Multiple Input without starting over.


seephp

Recommended Posts

I'm still fairly new to PHP and am trying to build a web-based sort of cash register. I have the very basics down. Tax, Price input, name of product, purchases recorded to text file. But now I am trying to write a new part that lets user input multiple purchases without starting over. I'm trying to get the purchases to show up under the latest purchase, that would include name and tax. Then I need to get a button that makes the purchase final. The price would be showed dynamically to the side the whole time. I have absolutely no idea how to do this. Im trying to keep this all on one page.

 

<html>
<head>
<title>PHPCash</title>
</head>
<body>
<form action="PHPCash1.php" method="POST">
Name of Product <input type="text" name="name" size="40" maxsize="100" /></p><br />
Original Price: <input type="text" name="price" size="15" maxsize="20" /></p><br />
Tax: <input type="text" name="tax" size="4" maxsize="4" /></p><br />
<input type="submit" name="submit">
</form>
<?php
if (isset ($_POST['submit'])) {
$name = "{$_POST['name']}";
$price = "{$_POST['price']}";
$tax = "{$_POST['tax']}";
$lasttax = $price * $tax;
$lastprice = $lasttax + $price;
print 'The tax of '. $name .' is <b>'. $lasttax .'</b>.';
print '<br />';
print '<br />';
print 'Final price of <b>'. $name .'</b> is <b>'. $lastprice . '</b>.';

if ($fp = fopen ('recordedtransactions.txt', 'ab')) {




fwrite ($fp, "
Name: $name,
Price :$price,
Tax: $tax,
LastTax: $lasttax,
Final Price: $lastprice

\n");
fclose ($fp);
}
} 
?>
</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/45345-multiple-input-without-starting-over/
Share on other sites

<?php session_start(); ?>
<html>
<head>
<title>PHPCash</title>
</head>
<body>
<form action="testing.php" method="POST">
Name of Product <input type="text" name="name" size="40" maxsize="100" /></p><br />
Original Price: <input type="text" name="price" size="15" maxsize="20" /></p><br />
Tax: <input type="text" name="tax" size="4" maxsize="4" /></p><br />
<input type="submit" name="submit">
</form>
<?php
if (isset ($_POST['submit'])) {
$name = "{$_POST['name']}";
$price = "{$_POST['price']}";
$_SESSION['item'][] = $name.' --- $ '.$price;
$tax = "{$_POST['tax']}";
$lastprice = $lasttax + $price;
$_SESSION['subtotal'] += $price;
$tax_final = $_SESSION['subtotal'] * $tax;
foreach ($_SESSION['item'] as $value) {
	if (rtrim($value != "")){
		print  $value.'<br>';
	}
}

print '<br><br />SUBTOTAL $'.$_SESSION['subtotal'].'<br>';
$total = $_SESSION['subtotal'] + $tax_final;
        print '<br> Tax $'.$tax_final;
print '<br><br /> TOTAL PRICE  $ '.$total.'<br>';
if ($fp = fopen ('recordedtransactions.txt', 'ab')) {


fwrite ($fp, "
Name: $name,
Price :$price,
Tax: $tax,
LastTax: $lasttax,
Final Price: $lastprice

\n");
fclose ($fp);
}
} 
?>
</body>
</html>

 

try that it should work.

 

I do notice you need to enter your tax percentage as a decimal value though  ie 8% as .08

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.