2016-03-03 34 views
0

我正在處理此作業,但我被卡住了。 所以我有圖像的形式...我想要的程序,以便每當我點擊一個圖片...它寫在文本文件中的價格...單擊圖片並執行一些操作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> 
+0

我想你應該看看[客戶端和服務器端編程有什麼區別?](http://stackoverflow.com/q/13840429/1816580) –

+0

我沒有明白它。它是否與我的編碼有關?對不起,很新的php – javaMan1995

回答

1

簡短的回答是你的「可樂」和「rootbeer」後的變量將始終設置多數民衆贊成編碼方式。它增加了一個可樂,一個2爲rootbeer。嘗試點擊任何其他圖像,你會得到兩個2。您應該檢查是否設置了pop1_x或pop1_y後置變量,因爲它們僅根據單擊的圖像進行設置。

另外考慮使用switch()邏輯,而不是一堆if-then-elseif語句。編輯 - 我的意思是使用開關()不是爲了這個例子,而是在將來的編碼。

嘗試這樣:

<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> 

中的註釋說明了什麼變化。

+0

非常感謝您的意見。但是,我如何實現呢?我可以使用if(isset($ POST [pop1_x]))...? – javaMan1995

+0

我該如何去? tnx – javaMan1995

+0

我會寫一個例子並很快發佈。 – user2016279

相關問題