我正在设计一个用于客户申请服务升级的网络表单。我目前卡在一个IF/else语句上,我必须测试如果在"帮派位置*"上选择下拉列表,并且文本框帮派为空白。
这就是这样,如果他们选择"帮派位置",则需要对其旁边的文本框进行验证。如果此条件是正确的,则我要验证文本框仅为数字值。这是我的代码块
//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";
}
当前初始双检查工作起作用,仅当选择"帮派位置*"时,它才会出现错误。但是,它只是被卡在那里,即使输入数据也不会摆脱错误。这是作为潜在援助的形式的部分。
<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>
几件事可以清理您的代码。
1。(在此处详细介绍的php.ini中启用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.
}