复选框添加($ _POST)

时间:2014-10-06 23:08:27

标签: php

我想知道如何制作它,当我点击两个复选框然后提交时,会出现一个值;在这种情况下,所需的值是“15%”

我试图让它以多种方式发挥作用。阅读PHP手册中的'$ _POST',但无法弄清楚如何解决这个问题

        

PHP Lab 04

        <form name="orderform" action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">

            Discount<br />
        <input type="checkbox" name="discount" value="student"/>Customer one 15%<br />
        <input type="checkbox" name="discount" value="senior"/>Customer two 10%<br />
        <input type="checkbox" name="discount" value="klant"/>Customer three 5%



                 <br />   <input type="image" src="button.jpg" value="order" />


        </form>


   <br /> Total discount

    <?php
           if(isset($_POST['discount']))
           {

            if($_POST['discount']=="student")
            {
               echo "15 percent";
            }
            else if($_POST['discount']=="senior")
            {
               echo "10 percent";
            }
            else if($_POST['discount']=="klant")
            {
               echo "5 percent";
            }
            else if($_POST['discount']=="senior" && $_POST['discount']=="klant")
            {
                echo "15 percent";
            }
           }

        ?>
</body>

客户二和二的结果客户三

http://i.stack.imgur.com/qsUqU.png

1 个答案:

答案 0 :(得分:0)

您应该使用无线电而不是复选框尝试以下操作:

    <form name="orderform" action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">

        Discount<br />
    <input type="radio" name="discount" value="student"/>Customer one 15%<br />
    <input type="radio" name="discount" value="senior"/>Customer two 10%<br />
    <input type="radio" name="discount" value="klant"/>Customer three 5%



             <br />   <input type="image" src="button.jpg" value="order" />


    </form>

我假设您只想要一次选择其中一种,而不是多次。

<强>更新

如果你想做你在评论中提到的话,你必须像@ 5parc那样提到。这是代码,“虽然我认为有更好的方法来完成你想要做的事情”:

    <form name="orderform" action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">

        Discount<br />
    <input type="checkbox" name="discount[]" value="student"/>Customer one 15%<br />
    <input type="checkbox" name="discount[]" value="senior"/>Customer two 10%<br />
    <input type="checkbox" name="discount[]" value="klant"/>Customer three 5%



             <br />   <input type="image" src="button.jpg" value="order" />


    </form>


<br /> Total discount

<?php
       if(isset($_POST['discount']))
       {

        if(in_array('senior', $_POST['discount']) && in_array('klant', $_POST['discount'])) {
          echo "15 percent";
        }
        else if(in_array('student', $_POST['discount']))
        {
           echo "15 percent";
        }
        else if(in_array('senior', $_POST['discount']))
        {
           echo "10 percent";
        }
        else if(in_array('klant', $_POST['discount']))
        {
           echo "5 percent";
        }

       }

    ?>
</body>
相关问题