我正在做这项任务,我被卡住了。所以我有一个带有图片的表格...我想要这个程序,这样每当我点击一张图片...它把价格写在文本文件上...
例如,当我单击coke时,image...it在文本文件上写为
2然后,当我单击rootbeer时,image...it应该会追加
2
2问题是,我的代码可以工作,但是输入两次,比如当我单击焦炭图像时……txt文件如下所示
2
2此外,当我点击提交按钮(显示价格),它应该添加价格和显示它…(好吧,我可以在这个哈哈)
非常感谢!
我的代码是:
<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>发布于 2016-03-04 01:03:36
简而言之,你的“可口可乐”和“rootbeer”post变量将始终按照编码的方式进行设置。可乐加2,根啤酒加2。尝试点击任何其他图片,你会得到两个2,你应该检查是否设置了pop1_x或pop1_y post变量,因为它们只是根据被点击的图片设置的。
还可以考虑使用switch()逻辑,而不是一堆if-then- Also if语句。编辑-我的意思是在这个例子中不使用switch(),而是在以后的编码中使用。
尝试如下所示:
<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";
$beverage = '';
if(isset($_POST['pop1_x'])) //test for when coke image clicked pop1_x and pop1_y will be set
{
$beverage = "2\r\n"; //Change this so you can move the file write to one line.
}
else if(isset($_POST['pop2_x'])) //test for when rootbeer image clcked
{
$beverage = "1.50\r\n";
}
else if(isset($_POST['pop3_x'])) //test for when lemonlime image clcked
{
$beverage = "1.75\r\n";
}
if($beverage <> '')
file_put_contents($fileName, $beverage, FILE_APPEND | LOCK_EX); //Move this line down here to save lines of code
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 ($1.50)</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.75)</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>评论显示了所做的更改。
https://stackoverflow.com/questions/35777918
复制相似问题