我正在處理此作業,但我被卡住了。 所以我有圖像的形式...我想要的程序,以便每當我點擊一個圖片...它寫在文本文件中的價格...單擊圖片並執行一些操作php
例如, 當我點擊焦圖像.. 。它在文本文件中這樣寫道
2
然後當我點擊rootbeer圖像...它應該追加
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>
我想你應該看看[客戶端和服務器端編程有什麼區別?](http://stackoverflow.com/q/13840429/1816580) –
我沒有明白它。它是否與我的編碼有關?對不起,很新的php – javaMan1995