0

Would this be the proper way to add a default value to a drop down menu it HTML?

$vendor_name is determine by a query to get an array of results; that is iterated through to create table rows in HTML. So this value changes dependent on the iteration.

<option value='$vendor_name'>$vendor_name</option>";
while($row = mysqli_fetch_array($result))
{
    echo "<option value='".$row['vendor_id']."'>".$row['name']."</option>";
}

When I attempt this it shows the default value; however, the table that is being created here is used to update an SQL table. If i change any other value in the row related to the $vendor_name without changing the $vendor_name it will not update. Is this because I set the default value?

1
  • Yes, you have set default value wrongly, I will share correct example in a while. Commented Dec 10, 2017 at 5:52

2 Answers 2

1
`<?php

while ($row = mysqli_fetch_array($result)) {

echo "<option value='" . $row['vendor_id'] . "'" .( ( $vendor_name == $row['name'])? 'selected': '' ) . ">" . $row['name'] . "</option>"; } ?>

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

Comments

0

Below is the correct way of setting default item in DropDown with PHP.

<?php
while ($row = mysqli_fetch_array($result)) {
  $selected = '';
  if ($vendor_name == $row['name']) {
    $selected = ' selected';
  }
  echo "<option value='" . $row['vendor_id'] . "'" . $selected . ">" . $row['name'] . "</option>";
}
?>

Comments

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.