Jump to content

[SOLVED] Setting the selected value of a drop down list


Ugluth

Recommended Posts

Hello there i have a page that edits some records on my database. Usually i'm using textfields to display the current value and just update it into the database and let the user decide which values he wants to change and which he doesn't. The problem is that one of the fields in this table is a foreign key to another table, so the textbox shows the user some primary key which doesn't really help him understand to what he can change it. Anyway here's the form as it is now with textboxes

 

<form id="frmDirector" name="frmDirector" method="post" action="edit_director.php">
<input name="menuDirector" type="hidden" value="<?php echo $_POST['menuDirector']; ?>" />
<?php
$query_director = "SELECT * FROM directors WHERE Director_ID = '$director'";
$result_director = mysql_query($query_director) or die("Query failed: " . mysql_error());
while ($line = mysql_fetch_array($result_director, MYSQL_NUM)) {
?>
  <p>Director ID
    <input name="txtID" type="text" id="txtID" value="<?php echo $line[0]; ?>" />
</p>
  <p>Director Name
    <input name="txtName" type="text" id="txtName" value="<?php echo $line[1]; ?>" />
</p>
  <p>Nationality
    [b]<input name="txtNation" type="text" id="txtNation" value="<?php echo $line[2]; ?>" />[/b]
  </p>
  <p>
    <input type="submit" name="Submit2" value="Submit" />
  </p>
  <?php
}
?>
</form>

 

What i want to do is replace this textbox

<input name="txtNation" type="text" id="txtNation" value="<?php echo $line[2]; ?>" />

with a dropdown list that will take all of the records from that table and have as selected the value that this textbox would show.

 

Here's the dropdown list i would use, just without having as selected value the above mentioned.

 

  <select name="menuNation">
  <option value=" "> </option>
<?php
$query_menu = "SELECT * FROM nationalities";
$result_menu = mysql_query($query_menu) or die("Query failed: " . mysql_error());
while ($line_menu = mysql_fetch_array($result_menu, MYSQL_NUM)) {
   echo "<option value=\"$line_menu[0]\"";
       if ($_POST["menuNation"]==$line_menu[0])
       	   echo " selected" ;
   echo ">".$line_menu[1]." </option>" ;
  }
?>
  </select>

 

Hope this post made sense and thanks in advance!

Link to comment
Share on other sites

try this out:

 

<form id="frmDirector" name="frmDirector" method="post" action="edit_director.php">
<input name="menuDirector" type="hidden" value="<?php echo $_POST['menuDirector']; ?>" />
<?php
   //Get nationalities
   $nationalities = array();
   $query_menu = "SELECT * FROM nationalities";
   $result_menu = mysql_query($query_menu) or die("Query failed: " . mysql_error());
   while ($line_menu = mysql_fetch_array($result_menu, MYSQL_NUM)) {
      $nationalities[$line_menu[0]] = $line_menu[1];
   }

   $query_director = "SELECT * FROM directors WHERE Director_ID = '$director'";
   $result_director = mysql_query($query_director) or die("Query failed: " . mysql_error());
   while ($line = mysql_fetch_array($result_director, MYSQL_NUM)) {
?>
  <p>Director ID
    <input name="txtID" type="text" id="txtID" value="<?php echo $line[0]; ?>" />
</p>
  <p>Director Name
    <input name="txtName" type="text" id="txtName" value="<?php echo $line[1]; ?>" />
</p>
  <p>Nationality
    <select name="txtNation" id="txtNation">
      <option value=" "> </option>
<?php
   foreach($nationalities as $natKey=>$natTitle){
      echo "<option value=\"$natKey\"";
       if ($line[2]==$natKey)
             echo " selected" ;
      echo ">".$natTitle." </option>" ;
  }
?>
    </select>
  </p>
  <p>
    <input type="submit" name="Submit2" value="Submit" />
  </p>
  <?php
}
?>
</form>

 

Edit: I moved the Nationalities SELECT statement outside of the loop to prevent redundant queries

Link to comment
Share on other sites

Hmm i didn't realise that you actually edited your post, sorry my bad, but the first one you posted works just fine, any specific reason on changing it and putting it outside of the main loop? Performance would be my guess but not that sure, and thank you again!

Link to comment
Share on other sites

Edit: I moved the Nationalities SELECT statement outside of the loop to prevent redundant queries

 

...if it's inside, you are performing the same SELECT statement for every entry in the main loop. i just glanced at the code again though...is Director_ID the primary key? aka there is only ever one entry returned?

Link to comment
Share on other sites

yeah...i would ditch the while loop, then move the SELECT back down:

 

<form id="frmDirector" name="frmDirector" method="post" action="edit_director.php">
<input name="menuDirector" type="hidden" value="<?php echo $_POST['menuDirector']; ?>" />
<?php
   $query_director = "SELECT * FROM directors WHERE Director_ID = '$director'";
   $result_director = mysql_query($query_director) or die("Query failed: " . mysql_error());
   $line = mysql_fetch_array($result_director, MYSQL_NUM) or die("No record found");
?>
  <p>Director ID
    <input name="txtID" type="text" id="txtID" value="<?php echo $line[0]; ?>" />
</p>
  <p>Director Name
    <input name="txtName" type="text" id="txtName" value="<?php echo $line[1]; ?>" />
</p>
  <p>Nationality
    <select name="txtNation" id="txtNation">
      <option value=" "> </option>
<?php
   $query_menu = "SELECT * FROM nationalities";
   $result_menu = mysql_query($query_menu) or die("Query failed: " . mysql_error());
   while ($line_menu = mysql_fetch_array($result_menu, MYSQL_NUM)) {
      echo "<option value=\"$line_menu[0]\"";
       if ($line[2]==$line_menu[0])
             echo " selected" ;
      echo ">".$line_menu[1]." </option>" ;
  }
?>
    </select>
  </p>
  <p>
    <input type="submit" name="Submit2" value="Submit" />
  </p>
</form>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.