我是PHP新手。我正在为简单的Cuboid计算器编写代码。如果长度,宽度和高度为负,为空或不是数字,则必须显示错误消息。
我尝试了以下代码。当长度字段输入不正确时,它会显示出不确定的错误,包括错误和错误。
<?php
//get the data from form
$length = $_GET['length'];
$breadth=$_GET['breadth'];
$height=$_GET['height'];
//calculate volume and tsa
if($length <=0 || empty($length) || !is_numeric($length) ){
$errlength = "You must enter valid length greater than zero";
$volume = '';
$tsa = '';
}
else if($breadth <=0 || empty($breadth) || !is_numeric($breadth) ){
$errbreadth= "You must enter valid length greater than zero";
$volume = '';
$tsa = '';
}
else if($height <=0 || empty($height) || !is_numeric($height) ){
$errheight = "You must enter valid length greater than zero";
$volume = '';
$tsa = '';
}
else{
$volume = $length*$breadth*$height;
$tsa = 2*($length*$breadth + $breadth*$height + $height*$length);
}
if(!empty($volume) || !empty($tsa))
{
$errlength='';
$errbreadth='';
$errheight='';
}
?>
HTML>>
<html>
<body>
<form action = "cuboid.php" method= "get">
Length<input type="text" name="length">
Breadth<input type="text" name="breadth">
Height<input type="text" name="height">
<input type="submit" name="Volume" value="Volume">
<input type="submit" name="TSA" value="TS">
</form>
<h1>Cuboid Volume Calculator</h1>
<label>Length: </label>
<span><?php echo $length?></span><span><?php echo $errlength?></span><br>
<label>Breadth: </label>
<span><?php echo $breadth?></span><span><?php echo $errbreadth?></span><br>
<label>Height: </label>
<span><?php echo $height?></span><span><?php echo $errheight?></span><br>
<label>Volume: </label>
<span><?php echo $volume?></span><br>
<label>Total Surface Area: </label>
<span><?php echo $tsa?></span><br>
</body>
</html>
我的代码怎么了?
答案 0 :(得分:0)
从未设置$errlength
,$errbreadth
和$errheight
的可能性。
您可以通过将以下代码添加到PHP文件顶部来解决此问题:
$errlength='';
$errbreadth='';
$errheight='';
您也可以检查是否设置了这些变量,例如上面的@Jeff所说的,但是当我知道要使用哪种类型时,我个人比较舒服。
希望这会有所帮助,