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

报告相同问题?

悬赏问题

  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛