I'm working on this assignment and I'm stuck. So I have a form with images... I want the program so that whenever I click one image... it writes the price on text file...
For example, When I click coke image...it writes on text file as
2
then when I click rootbeer image...it should append
2
2
the thing is, my code works, however input doubles, say when i just click the coke image... the txt file becomes like this
2
2
Also, when i click the submit button(Show Prices) it should add prices and display it...(well i can work on this haha)
Thanks so much!
My code is this:
<html><head><title>Online Vending Machine</title>
<body><div align="center">
<h2>Online Vending Machine<hr/></h2>
<?php
$fileName = "c:/wamp/www/Assignment 3/prices.txt";
if(isset($_POST['coke'])) //test for when coke image clicked
{
$coke = "2\r\n";
file_put_contents($fileName, $coke, FILE_APPEND | LOCK_EX);
}
else if(isset($_POST['rootbeer'])) //test for when rootbeer image clcked
{
$rbeer = "2\r\n";
file_put_contents($fileName, $rbeer, FILE_APPEND | LOCK_EX);
}
if(isset($_POST['submit']))
{
echo file_get_contents($fileName);
}
display_form();
function display_form() //displays actual form
{
?>
<b>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"><table>
<tr><td><input type="image" src="images/cola.jpg" width="200" height="300" name="pop1" />
<input type="hidden" name="cola" /><br/><center>Cola ($2.00)</center></td>
<td><input type="image" src="images/rootbeer.jpg" width="200" height="200" name="pop2" />
<input type="hidden" name="rootbeer" /><br/><center>Rootbeer ($2.00)</center></td>
<td><input type="image" src="images/lemonlime.jpg" width="200" height="300" name="pop3" />
<input type="hidden" name="lemonlime" /><br/><center>LemonLime ($1.00)</center></td></tr>
<tr><td><input type="image" src="images/grape.jpg" width="200" height="200" name="pop4" />
<input type="hidden" name="grape" value="1.50" /><br/><center>Grape Soda ($1.50)</center></td>
<td><input type="image" src="images/cream.jpg" width="200" height="200" name="pop5" />
<input type="hidden" name="cream" value="1.50" /><br/><center>Cream Soda ($1.50)</center></td>
<td><center><input type="submit" name="submit" value="Show Prices" /></center></td></tr>
</table></form><br/></b>
<?php
}
?>
</div>
</body>
</html>