Search code examples
phphtmlforms

The selected option is not saved in the form, but it stores another option


I have three options in the drop down list:

  1. Deployed
  2. Not Deployed
  3. Faulty

When i chose "deployed" ,its saved correctly. When i chose "Not Deployed" ,its saved with "Faulty" When I chose "Faulty", its saved with "Faulty"

Can you please help me resolve this issue so that form is saved with the selected option only. Thanks.

</div>
                    </div>
                    <?php if(isset($vehicledetails[0]['v_is_active'])) { ?>
                    <div class="col-sm-6 col-md-3">
                      <div class="form-group">
                        <label for="v_is_active" class="form-label">Machinery Status</label>
                        <select id="v_is_active" name="v_is_active" class="form-control " required="">
                          <option value="">Select Machinery Status</option> 
                          <option <?php echo (isset($vehicledetails) && $vehicledetails[0]['v_is_active']==1) ? 'selected':'' ?> value="1">Deployed</option> 
                          <option <?php echo (isset($vehicledetails) && $vehicledetails[0]['v_is_active']==0) ? 'selected':'' ?> value="0">Not Deployed</option> 
                          <option <?php echo (isset($vehicledetails) && $vehicledetails[0]['v_is_active']==0) ? 'selected':'' ?> value="0">Faulty</option>
                        </select>
                      </div>
                    </div>
                  <?php } ?>

Can you please help me resolve this issue so that form is saved with the selected option only.


Solution

  • I assume you have an array of $vehicledetails data like

    $vehicledetails = array(
            array(
                'v_is_active' => 1
            )
        );
    

    So, the problem with your code is that you have repeated the same condition for each of the three options.

    As a result, if any of the conditions are met, all options will appear as selected.

    The conditions for each option should be updated to reflect the appropriate value from $vehicledetails[0]['v_is_active'].

    Furthermore, the values for the "Not Deployed" and "Faulty" options appear to be incorrect, as they both have a value of 0.

    Revised Code:

    <?php if(isset($vehicledetails[0]['v_is_active'])) { ?>
        <div class="col-sm-6 col-md-3">
            <div class="form-group">
                <label for="v_is_active" class="form-label">Machinery Status</label>
                <select id="v_is_active" name="v_is_active" class="form-control" required="">
                    <option value="">Select Machinery Status</option> 
                    <option <?php echo ($vehicledetails[0]['v_is_active'] == 1) ? 'selected' : ''; ?> value="1">Deployed</option> 
                    <option <?php echo ($vehicledetails[0]['v_is_active'] == 0) ? 'selected' : ''; ?> value="0">Not Deployed</option> 
                    <option <?php echo ($vehicledetails[0]['v_is_active'] == 2) ? 'selected' : ''; ?> value="2">Faulty</option>
                </select>
            </div>
        </div>
    <?php } ?>
    
    

    Hope this help.