0

Hi what i would like to do in my dropdown is to put a condition if($formdata[0]['Phase']==1){ to show only the option values 0,1 and 2 in dropdown but if($formdata[0]['Phase']==3){ to show only the option values 0,3 and 4 in dropdown here is my code

<?php } ?>
                                        
<div class="form-group col-md-12 col-xs-12">
<label><?php echo $t[$tmpvalues[0]['Lang']]['poles'];?></label>
<label>NUMBER OF POLES</label> 
<select class="form-control el-text-box" name="polesi" id="polesi" onchange="this.form.submit()">
                                                
<option value="0" <?php echo $formdata[0]['polesi'] == "0" ? "selected" : ""; ?> >None</option>        
<option value="1" <?php echo $formdata[0]['polesi'] == "1" ? "selected" : ""; ?> >1</option>
<option value="2" <?php echo $formdata[0]['polesi'] == "2" ? "selected" : ""; ?> >2</option>
<option value="3" <?php echo $formdata[0]['polesi'] == "3" ? "selected" : ""; ?> >3</option>
<option value="4" <?php echo $formdata[0]['polesi'] == "4" ? "selected" : ""; ?> >4</option>
</select>                    
</div> 

2
  • OK, what have you tried so far? That should be quite straightforward to do. What about if the value is a different value than 1 or 3? Display all of them? Commented Apr 22, 2021 at 18:07
  • only these 2 values exist under 'Phase' conditions Commented Apr 22, 2021 at 18:09

1 Answer 1

1

There are different ways

1st using if condition as below

<?php if($formdata[0]['Phase']==1){  ?>    
<option value="1" <?php echo $formdata[0]['polesi'] == "1" ? "selected" : ""; ?> >1</option>
<option value="2" <?php echo $formdata[0]['polesi'] == "2" ? "selected" : ""; ?> >2</option>
<?php } ?>


<?php if ($formdata[0]['Phase']==3) { ?>

<option value="3" <?php echo $formdata[0]['polesi'] == "3" ? "selected" : ""; ?> >3</option>
<option value="4" <?php echo $formdata[0]['polesi'] == "4" ? "selected" : ""; ?> >4</option>
<?php } ?>

2nd way is using CSS

<option value="3" style="<?php echo $formdata[0]['Phase'] == "3" ? "display:block" : "display:none"; ?>"3</option>

same for other options according to your condition

Sign up to request clarification or add additional context in comments.

7 Comments

second is not working as it doesnt show any values on dropdown menu and 1st has a problem, i am not sure if i am doing something wrong
Can you share what the error is? Also, I have updated the second answer it was 'polesi' it should have been 'Phase' (this is incomplete you need to do for others option accordingly)
1st option the if...... it makes the <option value WHITE
1st now works with some modification thank you so much
``` <div class="form-group col-sm-6 col-xs-12"> <label for="d13"><?php echo $t[$tmpvalues[0]['Lang']]['d13'];?></label> <select class="form-control el-text-box" name="d13" onchange="this.form.submit()"> <?php for ($j=0; $j<sizeof($ahk); $j++){ $selected="";if($ahk[$j]==$formdata[0]['d13']){$selected="selected";} ?> <option value="<?php echo $ahk[$j];?>" <?php echo $selected; ?>><?php echo $ahk[$j];?></option> <?php } ?> </select> </div>
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.