0

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>
Artjom B.
  • 61,146
  • 24
  • 125
  • 222
javaMan1995
  • 51
  • 1
  • 1
  • 12
  • I think you should have a look at [What is the difference between client-side and server-side programming?](http://stackoverflow.com/q/13840429/1816580) – Artjom B. Mar 04 '16 at 17:55
  • i dont get it.. what does it have to do with my coding? Sorry, pretty new at php – javaMan1995 Mar 04 '16 at 18:16

1 Answers1

1

The short answer is your 'coke' and 'rootbeer' post variables will always be set the way that's coded. It's adding a 2 for cola, and a 2 for rootbeer. Try clicking any of the other images and you'll get two 2's. You should instead check if the pop1_x or pop1_y post variables are set as they are only set based on which image is clicked.

Also consider using switch() logic instead of a bunch of if-then-elseif statements. EDIT - I meant use switch() not for this example rather in future coding.

Try something like 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";
        $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>

The comments show what was changed.

sansig
  • 81
  • 7