简体   繁体   中英

PHP - Simple Echo inside If-Else statement not working

I have been searching around for the solution for so long, but every time the person who asked the question is way higher than my level. I'm only a young beginner, and I can't find anything that could ever solve my problem. Every PHP-Checker says my code is fine, but here is the problem:

I put an Echo in my If-Else Statement, but without any errors, or syntax problems, the Echo'd Text simply does not display on my site.

Now, I don't know what to do. But I'd like to ask anyone reading this to Pretend I'm Five years old and keep it simple , because I can't figure it out. I don't use SQL or any weird trickery as far as I know, and this is part of my school project, so it should be possible to do very simply.

The Code:

 <form action='' method='post'>
        <table>
            <tr> 
                <td> banana: </td>
                <td><input type='text' name='product1' /></td>
            </tr>
            <tr> 
                <td> candy: </td>
                <td><input type='text' name='product2' /></td>
            </tr>
            <tr> 
                <td> tv: </td>
                <td><input type='text' name='product3' /></td>
            </tr>
            <tr> 
                <td> mouse: </td>
                <td><input type='text' name='product4' /></td>
            </tr>
            <tr>
                <td><input type='submit' value='submit' /></td>
            </tr> 
        </table>
    </form>

    <?php
        if (isset($_POST['submit'])) {
            $iProductammount1 = $_POST['product1'];
            $iProductammount2 = $_POST['product2'];
            $iProductammount3 = $_POST['product3'];
            $iProductammount4 = $_POST['product4'];

            $fprice1 = 5.00;
            $fprice2 = 0.99;
            $fprice3 = 200.00;
            $fprice4 = 15.00;

            $fProductprice1 = $iProductammount1*$fprice1;
            $fProductprice2 = $iProductammount2*$fprice2;
            $fProductprice3 = $iProductammount3*$fprice3;
            $fProductprice4 = $iProductammount4*$fprice4;

            $fTotal = $fProductprice1+$fProductprice2+$fProductprice3+$fProductprice4;


            if ($fTotal > 250) {
                $fDiscountprice = $fTotal*0.85;
                 $sDiscount = '15%';
            } else if ($fTotal > 100 ) {
                $fDiscountprice = $fTotal*0.90;
                 $sDiscount= '10%';
            } else {
                $fDiscountprice = $fTotal;
                $sDiscount= '0%'; 
            }

            echo "The usual price is &euro; ".number_format($fTotal,2,',','.').". You received a discount of ".$sDiscount." . The new price is &euro; ".number_format($fDiscountprice,2,',','.');

        } else {

        }
    ?>

I used "isset" to only do all of this when the button "submit" is pressed, which does seem to work, since putting an Echo in between the Else brackets does actually Echo it properly. However, the Echo at the end of the If-statement does not show up at all. After pressing submit, simply nothing happens at all. The numbers filled in the disappear, and you can start all over again.

I'd also like to mention that there is a tag and all the necessary things above this, but I didn't feel like they were worth mentioning.

Again, the point of interest is the final Echo line at the end of the code. Please help me figure this out!

Explain to me like I'm 5, thanks for reading :)

You need to set the name attribute to the submit input tag:

<td><input type='submit' name="submit" value='submit' /></td>

If you don't set this name attribute you can't go into the if(isset($_POST['submit'])) block because submit doesn't exists in the $_POST var.

<td><input type='submit' name="submit" value='submit' /></td>

You simply forgot to give name to button. Use above code and check.

You didn't set the "name" attribute for the "submit" button. Form elements which don't have a "name" attribute do not get submitted to the server.

<input type='submit' name="submit" value='submit' />

will fix it.

PS you can always check exactly what is being submitted by doing one of two things:

1) Watch the "network" tab in your browser's Developer Tools (press F12 on most browsers). You'll see the submit request listed. Click into it and look at Form Data / Request body to see what was submitted.

2) In PHP, use var_dump($_POST); at the beginning of the file to see what's in the POST variables received by PHP and the structure of them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM