Board index   FAQ   Search  
Register  Login
Board index PHP PHP Scripts

Show a select record (field)

Links for php scripts

Moderators: macek, egami, gesf

Show a select record (field)

Postby kiwi » Wed Sep 08, 2004 7:58 am

this is a drop menu code, i want to select and id from the menu, after select the id, i want to show the name of the selected id, how to do it, can some one help me, thanks

<select name="select">
<?php
do {
?>
<option value="<?php echo $row_rsPart['TypeID']?>"><?php echo $row_rsPart['TypeID']?></option>
<?php
} while ($row_rsPart = mysql_fetch_assoc($rsPart));
$rows = mysql_num_rows($rsPart);
if($rows > 0) {
mysql_data_seek($rsPart, 0);
$row_rsPart = mysql_fetch_assoc($rsPart);
}
?>
</select>
kiwi
New php-forum User
New php-forum User
 
Posts: 124
Joined: Sun Feb 29, 2004 6:26 am

Postby gesf » Wed Sep 08, 2004 11:26 am

Example:
Code: Select all
<?php

if(isset($_POST['DoIt'])){
    // Prints the 'id' of the selected option
    print $_POST['mysel'];
}
else{
    // Display select menu
    ShowSel();
}


function ShowSel() {
   $result = mysql_query("SELECT id, name FROM table ORDER BY id");

   print '<form name="f" method="post" action="">' . "\n";   
   print '<select name="mysel">' . "\n";   
 
   while($row = mysql_fetch_array($result)) {
      print '<option value="' . $row['id'] . '">' . $row['name'] . '</option>' . "\n";
   }
   
    print '</select>' . "\n";
    print '<input type="submit" name="DoIt" value="View">' . "\n";
    print '</form>' . "\n";
}

?>

You can put the $row['name'] in the value part of the option instead of the id.

If you realy need the id, you can use javascript for that by adding a hidden input to the form:
Code: Select all
<input type="hidden" name="myname" value="">
And in your select option add something like:
Code: Select all
<select onchange="document.f.myname.value = this.options[this.selectedIndex].text">
So everytime you choose an option, your hidden input will have the respective 'name' that you can 'get' with:
Code: Select all
<?php

print $_POST['myname'];

?>


Cheers
User avatar
gesf
Moderator
Moderator
 
Posts: 1717
Joined: Sun Dec 29, 2002 5:03 am
Location: Portugal / Sweden


Return to PHP Scripts

Who is online

Users browsing this forum: No registered users and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.