dpxw7293 2017-05-01 17:29
浏览 24
已采纳

PHP如果/多个条件不能按预期工作? 帮帮我理解为什么?

I'm designing a web form to be used for customers to apply for a service upgrade. I'm currently stuck on an If/Else statement I have to test IF the dropdown is selected on "Ganged Positions*" AND the textbox gangPosition is blank.

This is so that only if they select "Ganged Positions" will the text box beside it need to be validated. IF this condition is true, I then want to validate the textbox to be numeric values only. Here is my code chunk

    //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";
        }

Currently the initial double check works, it only spits an error if the "Ganged Positions*" is selected. But it just get's stuck in there and won't get rid of the error even if data is entered. Here is the section of form where it takes place as a potential aid.

        <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条回答 默认 最新

  • dongzhou1865 2017-05-01 17:48
    关注

    A couple of things to clean up your code.

    1.) Enable php short open tags in your php.ini as detailed here. This will allow you to use <? instead of <?php in your html files.

    2.) Simplify your if/else statements. Only the one else is needed.

    The end result should be code that looks like this. I'm guessing you error occurred because you were checking $gangPosition before you were setting it.

    $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.
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮