单击图像并执行一些php操作


Click images and do something php

我正在做这个作业,但我被卡住了。所以我有一个带有图像的表格...我想要这个程序,以便每当我单击一张图像时......它将价格写入文本文件...

例如当我点击可乐图像时...它在文本文件上写为

2

然后当我点击根啤酒图像时...它应该附加

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>

简短的回答是,您的"可乐"和"根啤酒"帖子变量将始终按照编码方式进行设置。 它为可乐增加了一个2,为根啤酒增加了一个2。 尝试单击任何其他图像,您将获得两个2。 相反,您应该检查是否设置了pop1_x或pop1_y发布变量,因为它们仅根据单击的图像进行设置。

还可以考虑使用 switch() 逻辑而不是一堆 if-then-elseif 语句。 编辑 - 我的意思是不要在这个例子中使用 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>

注释显示了更改的内容。