我正在设计一个用于客户申请服务升级的网络表单。我目前停留在我必须测试的If / Else语句中 如果在" Ganged Positions *"上选择了下拉菜单。 AND文本框gangPosition是空白的。
如果他们选择" Ganged Positions"那么仅它旁边的文本框是否需要验证。如果这个条件成立,那么我想要将文本框验证为仅数值。这是我的代码块
//GANGED Position
if($meterBase == "Ganged Position*" && $gangPosition == "")
{
$gangPositionError = "Positions Number required";
$error = 1;
}
else if($gangPosition != "" && !is_numeric($gangPosition))
{
$gangPositionError = "Numbers Only";
$error = 1;
}
else
{
$gangPosition = $_POST['GangedPositions'];
$gangPositionError = "";
echo "THIS IS WORKING";
}
目前初始双重检查有效,只有在" Ganged Positions *"被选中。但它只是被卡在那里并且即使输入了数据也无法摆脱错误。这是作为潜在援助发生的形式部分。
<tr>
<td align="right" id="meterbaselabel">Meter Base Location:</td>
<td align="left">
<select class="my_dropdown" name="MeterBaseLocation" id="MeterBaseLocation" style="width: 150px" title="Select the location of the Meter">
<option value="-1" selected>[select--location]</option>
<option <?php if($meterBase=="Existing Outside") echo 'selected="selected"'; ?> value="Existing Outside">Existing Outside</option>
<option <?php if($meterBase=="Inside Moving Out") echo 'selected="selected"'; ?> value="Inside Moving Out">Inside Moving Out</option>
<option <?php if($meterBase=="Relocate") echo 'selected="selected"'; ?> value="Relocate">Relocate</option>
<option <?php if($meterBase=="Ganged Position*") echo 'selected="selected"'; ?> value="Ganged Position*">Ganged Position*</option>
</select>
<td align="left"><input type="text" id="gangedPosition" name="GangedPositions" size="5" title="If meter location requires a Ganged (multiple) position installation, how many positions are needed?"/>*Positions</td>
</td>
</tr>
<tr>
<td></td>
<td><div align="center" class="error"><?php echo $meterBaseError;?></div></td>
<td><div align="center" class="error"><?php echo $gangPositionError;?></div></td>
</tr>
答案 0 :(得分:0)
清理代码有几件事。
1。)在php.ini中启用详细here的php短开标签。这样您就可以在html文件中使用<?
代替<?php
。
2。)简化你的if / else语句。只需要其他人。
最终结果应该是看起来像这样的代码。我猜错了,因为您在设置之前检查了$gangPosition
。
$gangPosition = $_POST['GangedPositions'];
if ($meterBase === 'Ganged Position*' && !is_numeric($gangPosition)) {
$gangPositionError = "Gang positions must be numeric."
$error = 1;
return false;//Depending on the rest of your code returning may not be needed.
}