PHP - If-Else语句中的简单回显不起作用

时间:2017-05-24 12:55:36

标签: php html

我一直在寻找解决方案这么长时间,但每次提出问题的人都高于我的水平。我只是一个年轻的初学者,我找不到任何可以解决我的问题的东西。每个PHP-Checker都说我的代码很好,但问题出在这里:

我在我的If-Else语句中放了一个Echo,但没有任何错误或语法问题,Echo'd Text根本没有显示在我的网站上。

现在,我不知道该怎么做。但我想问任何读这篇文章的人 假装我已经五岁了,保持简单,因为我无法理解。据我所知,我不使用SQL或任何奇怪的技巧,这是我学校项目的一部分,所以应该可以非常简单地做。

守则:

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

        }
    ?>

当按下“提交”按钮时,我使用“isset”来完成所有这一切,这似乎有效,因为在Ese括号之间放置一个Echo确实可以正确地回显它。但是,If语句末尾的Echo根本没有出现。按下提交后,根本没有任何事情发生。消失的数字消失了,你可以重新开始。

我还想提一下,在这之上有一个标签和所有必要的东西,但我觉得它们不值得一提。

同样,兴趣点是代码末尾的最终Echo行。请帮我解决这个问题!

向我解释,就像我5岁,感谢阅读:)

3 个答案:

答案 0 :(得分:2)

您需要将name属性设置为提交输入标记:

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

如果您未设置此name属性,则无法进入if(isset($_POST['submit']))块,因为submit var中不存在$_POST

答案 1 :(得分:2)

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

你只是忘了给按钮命名。 使用上面的代码并检查。

答案 2 :(得分:1)

您没有为“提交”按钮设置“名称”属性。没有“name”属性的表单元素不会提交给服务器。

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

会解决它。

P.S。您可以通过执行以下两项操作之一来确切地检查提交的内容:

1)在浏览器的开发人员工具中观看“网络”标签(在大多数浏览器上按F12)。您将看到列出的提交请求。单击它并查看表单数据/请求正文以查看提交的内容。

2)在PHP中,在文件开头使用var_dump($_POST);来查看PHP接收的POST变量及其结构中的内容。

相关问题