验证输入类型“复选框”。和“数字”的PHP / HTML [英] validating input types "checkbox" and "number" php/html

查看:62
本文介绍了验证输入类型“复选框”。和“数字”的PHP / HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以验证复选框 <​​/ code>和数字 。真的很难解释我想要什么,但我会尽力而为;)。无论如何,我都有此代码:

Hi i was wondering if it is possible to validate "checkbox" and "number". It is really hard to explain what I want but I'll try my best ;). Anyways I have this code:

<input type = "checkbox" name = "coffee[]" value = "cappuccino"/>Cappuccino
  <input type = "number" value = "qty." name = "cappuccino_qty" size = "2"/><br>
<input type = "checkbox" name = "coffee[]" value = "espresso"/>Espresso
  <input type = "number" value = "qty." name = "espresso_qty" size = "2"/><br>

...依此类推

I希望程序同时验证复选框 <​​/ code>和数字 ,以便用户无法作弊。例如,如果选择了卡布奇诺咖啡,则其分配的数量( cappuccino_qty)应该是唯一能够提交的输入类型。假设卡布奇诺咖啡的价格为$ 2.00,而意式浓缩咖啡的价格为$ 3.00。使用我的代码,用户可以检查意式浓缩咖啡并将卡布奇诺咖啡的数量更改为一个或多个。因此,按我的输出,特浓咖啡的价格为2.00美元,而不是3.00美元。我想防止这种情况发生。

I want the program to validate both the "checkbox" and "number"so that a user would not be able to cheat. Such as that if the "cappuccino" is selected its assigned quantity ("cappuccino_qty") should be the only input type able to submit. Say cappuccino cost $2.00 and espresso cost $3.00 With my code a user is able to check espresso and change quantity of cappuccino to one or more. So with my output espresso cost $2.00 instead of $3.00. I want to prevent that from happening.

这是我的整个代码:

<html>
    <head>
        <title>Order</Title>
            <style>
            </style>
        <body>
            <form action = "order.php" method = "post">
                Coffee:<p>
                <input type = "checkbox" name = "coffee[]" value = "cappuccino"/>Cappuccino
                <input type = "number" value = "qty." name = "cappuccino_qty" size = "2"/><br>
                <input type = "checkbox" name = "coffee[]" value = "espresso"/>Espresso
                <input type = "number" value = "qty." name = "espresso_qty" size = "2"/><br>
                <input type = "checkbox" name = "coffee[]" value = "double_espresso"/>Double Espresso
                <input type = "number" value = "qty." name = "double_espresso_qty" size = "2"/><br>
                <input type = "checkbox" name = "coffee[]" value = "flat_white"/>Flat White
                <input type = "number" value = "qty." name = "flat_white_qty" size = "2"/><br>
                <input type = "checkbox" name = "coffee[]" value = "latte"/>Latte
                <input type = "number" value = "qty." name = "latte_qty" size = "2"/><br>
                <input type = "checkbox" name = "coffee[]" value = "ice"/>Ice Coffee
                <input type = "number" value = "qty." name = "ice_qty" size = "2"/><br>
                <p>
                <input type = "radio" value = "in" name = "dine"/>Dine in
                <input type = "radio" value = "out" name = "dine"/>Take out
                <br>
                <input type = "submit" value = "submit" name = "submit"/>
            </form>
        </body> 
    </head>
</Html>

<?php
    //coffee cost//
    $cappuccino_cost = 3.75;
    $espresso_cost = 3.00;
    $double_espresso_cost = 4.25;
    $flat_white_cost = 3.75;
    $latte_cost = 3.5;
    $ice_cost = 2.5;
    //default qty of each coffee//
    $cappuccino_qty = 0;
    $espresso_qty = 0;
    $double_espresso_qty = 0;
    $flatwhite_qty = 0;
    $latte_qty = 0;
    $ice_qty = 0;
    //discounts & charges//
    $charge = 1.05;
    $discount = 0.1;
    //submitting inputs//
    if(isset($_POST["submit"]))
    {
        //number of checkboxe(s) that are checked
        if(isset($_POST['coffee']))
        {
            $checked_array = $_POST['coffee'];
            $count = count($checked_array);
            if($count != 0)
            {
                //coffee cost is being readied// 
                if(!isset($_POST['coffee']) && $_POST['coffee'] == 'cappuccino')
                {
                    $cappuccino_cost = 0;
                }
                if(!isset($_POST['coffee']) && $_POST['coffee'] == 'espresso')
                {
                    $espresso_cost = 0;
                }
                if(!isset($_POST['coffee']) && $_POST['coffee'] == 'double_espresso')
                {
                    $double_espresso_cost = 0;
                }
                if(!isset($_POST['coffee']) && $_POST['coffee'] == 'flat_white')
                {
                    $flat_white_cost = 0;
                }
                if(!isset($_POST['coffee']) && $_POST['coffee'] == 'latte')
                {
                    $latte_cost = 0;
                }
                if(!isset($_POST['coffee']) && $_POST['coffee'] == 'ice')
                {
                    $ice_cost = 0;
                }
                //the quantity calculated//
                if(isset($_POST['cappuccino_qty']) && $_POST['cappuccino_qty'] != 'qty.')
                {
                    $cappuccino_qty = $_POST['cappuccino_qty'];
                }
                if(isset($_POST['espresso_qty']) && $_POST['espresso_qty'] != 'qty.')
                {
                    $espresso_qty = $_POST['espresso_qty'];
                }
                if(isset($_POST['double_espresso_qty']) && $_POST['double_espresso_qty'] != 'qty.')
                {
                    $double_espresso_qty = $_POST['double_espresso_qty'];
                }
                if(isset($_POST['flat_white_qty']) && $_POST['flat_white_qty'] != 'qty.')
                {
                    $flat_white_qty = $_POST['flat_white_qty'];
                }
                if(isset($_POST['latte_qty']) && $_POST['latte_qty'] != 'qty.')
                {
                    $latte_qty = $_POST['latte_qty'];
                }
                if(isset($_POST['ice_qty']) && $_POST['ice_qty'] != 'qty.')
                {
                    $ice_qty = $_POST['ice_qty'];
                }
                //cost calculated//
                $cappuccino = $cappuccino_cost * $cappuccino_qty;
                $espresso = $espresso_cost * $espresso_qty;
                $double = $double_espresso_cost * $double_espresso_qty;
                $flat = $flat_white_cost * $flat_white_qty;
                $latte = $latte_cost * $latte_qty;
                $ice = $ice_cost * $ice_qty;
                //total amount of cost and no. cofee//
                $total = $cappuccino + $espresso + $double + $flat + $latte + $ice;
                $total_coffee = $cappuccino_qty + $espresso_qty + $double_espresso_qty + $flat_white_qty + $latte_qty + $ice_qty;
                //take away charge calculated//
                if(isset($_POST['dine']) && $_POST['dine'] == 'out')
                {
                    $total = $charge * $total;
                    $total = round($total,2);
                }
                //discount calculated//
                if($count >= 3 or $total_coffee >= 3)
                {
                    $total = $total - ($total * $discount);
                    $total = round($total,2);
                }
                //output total 
                if($total != 0)
                {
                    echo "$".(round($total,2));
                }
            }   
        }   
    }
?>  


推荐答案

三个词:将其排列起来!

Three words: array it up!

观察以下代码:

<form action="order.php" method="post">
    <p>
        Coffee: <br>

        <!-- This ensures false is submitted if the cappuccino box is unticked -->
        <input type="hidden" name="coffee[cappuccino][selected]" value="0">
        <label><input type="checkbox" name="coffee[cappuccino][selected]" value="1"> Cappuccino</label>
        <input type="number" name="coffee[cappuccino][qty]" size="2"><br>

        <!-- This ensures false is submitted if the espresso box is unticked -->
        <input type="hidden" name="coffee[espresso][selected]" value="0">
        <label><input type="checkbox" name="coffee[espresso][selected]" value="1"> Espresso</label>
        <input type="number" name="coffee[espresso][qty]" size="2"><br>
    </p>

    <p>[...]</p>

    <p>
        <label><input type="radio" name="dine" value="in"> Dine in</label>
        <label><input type="radio" name="dine" value="out"> Take out</label>
    </p>

    <p><input type="submit" value="submit" name="submit"></p>
</form>

在提交时,输入将作为关联数组提交,如下所示:

When this submits, the inputs are submitted as an associative array as this:

array (size=3)
  'coffee' => 
    array (size=2)
      'cappuccino' => 
        array (size=2)
          'selected' => string '1' (length=1)
          'qty' => string '4' (length=1)
      'espresso' => 
        array (size=2)
          'selected' => string '1' (length=1)
          'qty' => string '3' (length=1)
  'dine' => string 'in' (length=2)
  'submit' => string 'submit' (length=6)

如您所见,每杯咖啡现在都以以咖啡类型为键的数组,也有自己的数组,还有另外两个键: selected qty

As you can see, each coffee is now being submitted as an array with the type of coffee being a key that also has an array of its own with two further keys: selected and qty.

所选的 确定是否选中了该咖啡的复选框( 1 )( 0 )或 qty 保存用户输入。

The selected determines if the checkbox for that coffee was ticked (1) or not (0) and the qty holds the user input.

这几乎可以完成您的验证,因为每个输入的数量现在都属于单个咖啡。

This pretty much does your validation since each quantity input now belongs to the individual coffee.

我希望我已经理解正确地解决了您的难题,这可以回答您的问题,也可以使您了解如何继续执行此操作。

I hope I've understood your dilemma correctly and this answers your question or give a you an idea of how to go on about doing this.

享受:)

这篇关于验证输入类型“复选框”。和“数字”的PHP / HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆