Я написал код, в котором, если кто-то выбирает префикс флажка для элемента, он должен ввести значение количества 
Проблема в том, что когда он устанавливает флажок для соответствующего элемента, а затем нажимает кнопку «Отправить», вместо ввода какого-либо значения количества возникает ошибка, но флажок снимается, а затем, если он вводит значение в поле количества, даже если он не выбирает соответствующий флажок снова отправит страницу. Это ошибка в моей программе, но я не знаю, как ее решить
Пожалуйста, дайте мне знать, что всякий раз, когда он вводит значение количества и соответствующий флажок, если он не установлен, должна быть выдана ошибка. Ниже приведен мой код
<!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>



![Безумие обратных вызовов в javascript [JS]](https://i.imgur.com/WsjO6zJb.png)


Добавьте одно утверждение 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;
Please use this one
<!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>
Я просто вношу изменения в код. Добавление некоторых переменных и условий, вот и все.
Некоторое объяснение того, что вы изменили / почему OP должен использовать этот код, было бы неплохо.