我的程序中有一个错误。如何解决

时间:2019-03-15 07:52:15

标签: javascript php html debugging

我已经编写了一个代码,其中,如果有人选择了该项目的复选框前缀,那么他必须输入一个数量值enter image description here

问题在于,当他选中相应项目的复选框然后单击提交时,而不是输入任何数量值,都会引发错误,但是复选框被取消选择,然后他在数量框中输入了值,即使他没有再次选择相应的复选框,页面被提交。这是我程序中的错误,但我不知道如何解决

请告知我,只要他输入数量值和相应的复选框(如果未选中),则应该引发错误 以下是我的代码

<!DOCTYPE html>
<?php
session_start();
ob_start();     
?>
<html lang="en">
<head>
<title>
Power For Light
</title>
<meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="stylesheet" href="bootstrap/css/bootstrap.min.css">
        <script type="text/javascript" src="jquery.min.js"></script>
        <script type="text/javascript" src="bootstrap/js/bootstrap.min.js"></script>




<style type="text/css">
.colht{
height:200px;
border: 5px solid red;
}
.colht1{
height:100px;
border: 5px solid yellow;
}
.navbar-custom {
    background-color:#ffffff;
    color:#ffffff;
    border-radius:0;
    padding:10px;
}
.ht{
    height:200px;
}
h2,h3{
text-align: center;
}
sup{
    color:red;
}
</style>
</head>

<body onload="document.myForm.reset();">
<?php
$bequanerr=$bechkErr="";
$bequan=$ber= $bes="";

$c=0;
//$_SESSION['postid']=rand(10, 30);
 if ($_SERVER["REQUEST_METHOD"] == "POST"):

            if (!empty($_POST["bechk"])):
                $c++;
                if(empty($_POST["bequan"])):
                    echo "Hello";
                    $bequanerr="Please enter a quantity value";


                else:
                    $bequan=test_input($_POST["bequan"]);
                    if($bequan <=0):
                        $bequanerr="Please enter a valid quantity value greater than 0";

                    else:
                        if(empty($_POST["ber"])):
                            $ber=400;
                            echo "400";
                        else:
                            $ber=test_input($_POST["ber"]);

                        endif;
                        if(empty($_POST["bes"])):
                            $bes=0;
                        else:
                            $bes=test_input($_POST["bes"]);
                        endif;
                    endif;

                echo $ber." " . $bes. " " . $bequan;
                // check if name only contains letters and whitespace

                endif;
            endif;

endif;
function test_input($data) {
            $data = trim($data);
            $data = stripslashes($data);
            $data = htmlspecialchars($data);
            return $data;
        }

?>
<nav class="navbar navbar-inverse">
            <div class="container">
                <div class="navbar-header">
                    <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>                        
                    </button> 
                    <a class="navbar-brand" href="index.php"><img src="images/powerforlight.jpg" style= "max-height:50px; margin-top:-14px; "/></a>
                </div>
                <div class="collapse navbar-collapse" id="myNavbar">
                    <ul class="nav navbar-nav">
                        <li class="active"><a href="index.php">Home</a></li>
                        <li class=""><a href="aboutus.php">About Us</a></li>
                        <li class="">
                            <a class="" href="vendorcf.php">Vendor Contact Form</a>
                        </li>
                        <li class="">
                            <a class="" href="contactus.php">Contact Us</a>
                        </li>
                    </ul>



                </div>
            </div>
        </nav>

<div class="container">
    <div class="row">
    <form  name="myForm" class="form-horizontal " role="form" method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"  autocomplete="off">
        <div class="panel panel-primary">
        <div class="panel-heading text-center">
        <h2 class="text-center">Bootstrap Design table</h2>
        </div>
        <div class="panel-body">
        <div class="table-responsive">          
            <table class="table table-bordered table-hover">
                <thead>
                    <tr>        
                        <th rowspan="2">Household Items</th>
                        <th colspan="2">Running Wattage

                        </th>
                        <th colspan="2">Starting Wattage

                        </th>
                        <th rowspan="2">Quantity</th>
                        <th rowspan="2">Should start Parallely</th>
                        <th rowspan="2">Should start Serially</th>

                    </tr>
                    <tr>
                        <th>
                            Typical
                        </th>
                        <th> Specific </th>


                        <th>
                            Typical
                        </th>
                        <th> Specific </th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td>
                            <label class="checkbox-inline"><input type="checkbox" name="bechk" value="be" <?php if (isset($bequan) && $bequan=="be") echo "checked";?>>Blanket, Electric
                            </label>
                        </td>
                        <td name="be1">400</td>
                        <td><input type="number" name="ber" /> </td>
                        <td name="be2">0</td>
                        <td><input type="number" name="bes" /> </td>
                        <td><input type="number" name="bequan" /> 
                        <p style="color:red"> <?php echo $bequanerr;?></p></td>
                        <td><input type="radio" name= "berad" class="rdo"  value="P" <?php if (isset($bs)) echo "checked";?> />  </td>
                        <td><input type="radio" name= "berad" class="rdo"  value="S" <?php if (isset($bs)) echo "checked";?> />  </td>

                    </tr>
                </tbody>
        </table>
        <div class="control-group">
                                <!-- Button -->
                                <div class="controls col-xs-offset-3 col-xs-2">
                                    <input type="submit" value="Submit" id="reg">
                                </div>

        </div>
    </div>
    </div>
</div>
</form>
</div>
</div>
<?php ob_end_flush(); ?>
</body>


</html>

2 个答案:

答案 0 :(得分:0)

elseif之后添加一个quantity >= 0语句。如果|| !empty($_POST["bequan"]),则添加一个或多个初始条件 像下面一样

if ($_SERVER["REQUEST_METHOD"] == "POST"):

            if (!empty($_POST["bechk"]) || !empty($_POST["bequan"])):
                $c++;
                if(empty($_POST["bequan"])):
                    echo "Hello";
                    $bequanerr="Please enter a quantity value";
                else:
                    $bequan=test_input($_POST["bequan"]);
                    if($bequan <=0):
                         $bequanerr="Please enter a valid quantity value greater than 0";
                    elseif(empty($_POST["bechk"])):
                        $bequanerr="Please check the check box";
                    else:
                        if(empty($_POST["ber"])):
                            $ber=400;
                            echo "400";
                        else:
                            $ber=test_input($_POST["ber"]);

                        endif;
                        if(empty($_POST["bes"])):
                            $bes=0;
                        else:
                            $bes=test_input($_POST["bes"]);
                        endif;
                    endif;

                echo $ber." " . $bes. " " . $bequan;
                // check if name only contains letters and whitespace

                endif;
            endif;

endif;

答案 1 :(得分:0)

  

请使用这个

<!DOCTYPE html>
<?php
session_start();
ob_start();
?>
<html lang="en">
    <head>
        <title>
            Power For Light
        </title>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="stylesheet" href="bootstrap/css/bootstrap.min.css">
        <script type="text/javascript" src="jquery.min.js"></script>
        <script type="text/javascript" src="bootstrap/js/bootstrap.min.js"></script>
        <style type="text/css">
            .colht{
                height:200px;
                border: 5px solid red;
            }
            .colht1{
                height:100px;
                border: 5px solid yellow;
            }
            .navbar-custom {
                background-color:#ffffff;
                color:#ffffff;
                border-radius:0;
                padding:10px;
            }
            .ht{
                height:200px;
            }
            h2,h3{
                text-align: center;
            }
            sup{
                color:red;
            }
        </style>
    </head>

    <body onload="document.myForm.reset();">
        <?php
        $bequanerr = $bechkErr = "";
        $bequan = $ber = $bes = $bechk = $berad = "";

        $c = 0;
//$_SESSION['postid']=rand(10, 30);

        if ($_SERVER["REQUEST_METHOD"] == "POST"):
            $berad = $_POST['berad'];
            if (!empty($_POST["bechk"])):
                $bechk = $_POST["bechk"];
                $c++;
                if (empty($_POST["bequan"])):
                    echo "Hello";
                    $bequanerr = "Please enter a quantity value";


                else:
                    $bequan = test_input($_POST["bequan"]);
                    if ($bequan <= 0):
                        $bequanerr = "Please enter a valid quantity value greater than 0";

                    else:
                        if (empty($_POST["ber"])):
                            $ber = 400;
                            echo "400";
                        else:
                            $ber = test_input($_POST["ber"]);

                        endif;
                        if (empty($_POST["bes"])):
                            $bes = 0;
                        else:
                            $bes = test_input($_POST["bes"]);
                        endif;
                    endif;

                    echo $ber . " " . $bes . " " . $bequan;
                // check if name only contains letters and whitespace

                endif;
            endif;

        endif;

        function test_input($data) {
            $data = trim($data);
            $data = stripslashes($data);
            $data = htmlspecialchars($data);
            return $data;
        }
        ?>
        <nav class="navbar navbar-inverse">
            <div class="container">
                <div class="navbar-header">
                    <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>                        
                    </button> 
                    <a class="navbar-brand" href="index.php"><img src="images/powerforlight.jpg" style= "max-height:50px; margin-top:-14px; "/></a>
                </div>
                <div class="collapse navbar-collapse" id="myNavbar">
                    <ul class="nav navbar-nav">
                        <li class="active"><a href="index.php">Home</a></li>
                        <li class=""><a href="aboutus.php">About Us</a></li>
                        <li class="">
                            <a class="" href="vendorcf.php">Vendor Contact Form</a>
                        </li>
                        <li class="">
                            <a class="" href="contactus.php">Contact Us</a>
                        </li>
                    </ul>
                </div>
            </div>
        </nav>

        <div class="container">
            <div class="row">
                <form  name="myForm" class="form-horizontal " role="form" method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>"  autocomplete="off">
                    <div class="panel panel-primary">
                        <div class="panel-heading text-center">
                            <h2 class="text-center">Bootstrap Design table</h2>
                        </div>
                        <div class="panel-body">
                            <div class="table-responsive">          
                                <table class="table table-bordered table-hover">
                                    <thead>
                                        <tr>        
                                            <th rowspan="2">Household Items</th>
                                            <th colspan="2">Running Wattage

                                            </th>
                                            <th colspan="2">Starting Wattage

                                            </th>
                                            <th rowspan="2">Quantity</th>
                                            <th rowspan="2">Should start Parallely</th>
                                            <th rowspan="2">Should start Serially</th>
                                        </tr>
                                        <tr>
                                            <th>
                                                Typical
                                            </th>
                                            <th> Specific </th>


                                            <th>
                                                Typical
                                            </th>
                                            <th> Specific </th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <tr>
                                            <td>
                                                <label class="checkbox-inline"><input type="checkbox" name="bechk" value="be" <?php if (isset($bechk) && $bechk == "be") echo "checked"; ?>>Blanket, Electric
                                                </label>
                                            </td>
                                            <td name="be1">400</td>
                                            <td><input type="number" name="ber" /> </td>
                                            <td name="be2">0</td>
                                            <td><input type="number" name="bes" /> </td>
                                            <td><input type="number" name="bequan" value="<?php echo $bequan; ?>"/> 
                                                <p style="color:red"> <?php echo $bequanerr; ?></p></td>
                                            <td><input type="radio" name= "berad" class="rdo"  value="P" <?php if (isset($berad) && $berad == "P") echo "checked"; ?> />  </td>
                                            <td><input type="radio" name= "berad" class="rdo"  value="S" <?php if (isset($berad) && $berad == "S") echo "checked"; ?> />  </td>

                                        </tr>
                                    </tbody>
                                </table>
                                <div class="control-group">
                                    <!-- Button -->
                                    <div class="controls col-xs-offset-3 col-xs-2">
                                        <input type="submit" value="Submit" id="reg">
                                    </div>

                                </div>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
        </div>
        <?php ob_end_flush(); ?>
    </body>
</html>