0

This is for a PHP application using PDO for the database binding. I'm working on an edit form and what I have so far works perfectly except for the drop down menu. I can't seem to get it to show the current value of the device I'm editing. It only shows a blank line. Any advice would be greatly appreciated.

<select name='connectedTerminal' id='connectedTerminal'>
<option value='0'>Select Terminal</option>
<option value='$row[connectedTerminal]' selected='selected' text='$row[connectedTerminal]'></option>
$options_terminal;
</select> 
John Haag
  • 11
  • 3
  • possible duplicate of [How to set default value for HTML select element?](http://stackoverflow.com/questions/3518002/how-to-set-default-value-for-html-select-element) – Sagar Naliyapara Jul 08 '15 at 11:50

5 Answers5

1

Use if condition

<select name='connectedTerminal' id='connectedTerminal'>
<option value='0'>Select Terminal</option>
<option <?php if($row[connectedTerminal] == '1') echo "selected " ?> value='1'>$row[connectedTerminal]</option>
<option <?php if($row[connectedTerminal] == '2') echo "selected " ?> value='2'>$row[connectedTerminal]</option>
$options_terminal; // what it is for?
</select> 
Muhammad Hassaan
  • 7,296
  • 6
  • 30
  • 50
0

The text to display for a select element goes inbetween the <option> tags, the same as you already have for "Select Terminal"

<select name='connectedTerminal' id='connectedTerminal'>
<option value='0'>Select Terminal</option>
<option value='$row[connectedTerminal]' selected='selected'>$row[connectedTerminal]</option>
$options_terminal;
</select>
Ross McLellan
  • 1,872
  • 1
  • 15
  • 19
0

To set default value use selected="selected"

<select name='connectedTerminal' id='connectedTerminal'>
  <option selected="selected" value='0'>Select Terminal</option> 
  <option value="<?php echo $row[connectedTerminal]; ?>"><?php echo $row[connectedTerminal];?></option>
</select> 
Community
  • 1
  • 1
Abey
  • 1,408
  • 11
  • 26
0

Try this

<select name='connectedTerminal' id='connectedTerminal'>
<option value='0'>Select Terminal</option>
<option value='<?php echo $row[connectedTerminal] ;?>' selected='selected' text='<?php echo $row[connectedTerminal];?>'><?php echo $options_terminal; ?></option>
</select> 

You are trying to out put PHP variable inside html.

Jpec
  • 300
  • 2
  • 14
0

Say you have an array of terminals

$terminals = array();
$terminals[1] = 'one';
$terminals[2] = 'two';
$terminals[3] = 'three';
$terminals[4] = 'four';
$terminals[5] = 'five';

And you are currently modifying record with terminal id 4 and the variable is $terminal_id

<select name='connectedTerminal' id='connectedTerminal'>
<option value='0'>Select Terminal</option>
<?php
if (! empty($terminals)) {
  foreach ($terminals as $tid => $tval) {
    $selected = ($tid == $terminal_id) ? 'selected="selected"' : '';
?>
<option value="<?php echo $tid;?>" <?php echo $selected;?>><?php echo $tval;?></option>
<?php
  }
}
</select>
Pupil
  • 23,834
  • 6
  • 44
  • 66