-1

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>
Adam Lee
  • 43
  • 10
  • 1
    Why is `$gangPosition = $_POST['GangedPositions'];` in the `else` clause? That assignment should be executed before the `if` statement. – Chris R. Timmons May 01 '17 at 17:47
  • New to web development, I was trying to make it such that the info wouldn't post unless it was valid. Meaning the form submit would bounce back unless everything is OK. – Adam Lee May 01 '17 at 17:52

1 Answers1

0

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.
}
Community
  • 1
  • 1
HopAlongPolly
  • 1,347
  • 1
  • 20
  • 48