Selecting listbox results

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tomchesley
Forum Newbie
Posts: 2
Joined: Sat May 09, 2009 4:22 pm

Selecting listbox results

Post by tomchesley »

I'm a newbee with php and mysql. I am able to successfuly display the results of my database table to a web page.
Now I am working with making a list box that displays all the names, and I wish to be able to edit any of the fields based on what is selected in the list box.
I felt good about being able to get the list box displayed in the order I wanted, but stumped on creating text boxs on the same page with the contents of the fields based on the name selected.
Table Structure is
JediName VARCHAR(50)
Level INTEGER
Status VARCHAR(15)
Updated DATE
Updater VARCHAR(50)
Email VARCHAR(50)
Comments VARCHAR(50)
Jnum INTEGER (AUTO) (KEY)

If i can see how to get 1 textbox like Status with the status of the name selected in the list box, I should be ok from there.

Here is what I have so far:

Code: Select all

 
<?php header("Content-type: text/html; charset=utf-8");?>
<HTML>
<HEAD>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<TITLE>Test of Listbox Display of Jedi Members Table</TITLE>
<BODY>
<p>Select Member<br>
 
<?
// Get $username $password $database from this included file
include("jedi.inc.php");
 
mysql_connect(localhost,$username,$password);
 
@mysql_select_db($database) or die("Unable to select database");
 
$query="SELECT JediName FROM Members ORDER BY JediName";
 
// this will be needed to get special characters
mysql_query("SET NAMES 'utf8'");
 
$result=mysql_query($query);
echo "<select name=listbox size=10>Student Name</option>";
// printing the list box select command
 
while ($line = mysql_fetch_array($result)){//Array or records stored in $line
foreach ($line as $value)
{
echo "<OPTION value='$value'";
}
echo ">$value</OPTION>";
}
 
 
// $num=mysql_numrows($result);
 
mysql_close();
echo "</SELECT>";
?>
 
</BODY>
</HEAD>
 
 
david64
Forum Commoner
Posts: 53
Joined: Sat May 02, 2009 8:12 am
Location: Wales

Re: Selecting listbox results

Post by david64 »

You need to add a selected attribute to the selected option with the value selected.

Code: Select all

<?php
foreach ($line as $value)
{
if( $value == $_POST['listbox'] )
echo "<option value='$value' selected='selected'>????</option>";
else
echo "<option value='$value'>????</option>";
}
?>
I've developed a form building framework that does this kind of thing automatically.

You can see an example here using a select element:

http://semlabs.co.uk/products/xfl/demos/a

The script will automatically fill in fields for you.

You can download it here:

http://semlabs.co.uk/products/xfl

and there is documentation here:

http://semlabs.co.uk/docs/xfl
Last edited by Benjamin on Sun May 10, 2009 12:31 pm, edited 1 time in total.
Reason: Changed code type from text to php.
tomchesley
Forum Newbie
Posts: 2
Joined: Sat May 09, 2009 4:22 pm

Re: Selecting listbox results

Post by tomchesley »

Ahh thank you, this might not be neat yet but here is what I have now and does what I was looking for.

Code: Select all

 
<?php header("Content-type: text/html; charset=utf-8");?>
<HTML>
<HEAD>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<TITLE>Test of Listbox Display of Jedi Members Table</TITLE>
<BODY>
<p>Select Member<br>
 
<?
// Get $username $password $database from this included file
include("jedi.inc.php");
 
mysql_connect(localhost,$username,$password);
 
@mysql_select_db($database) or die("Unable to select database");
 
$query="SELECT JediName FROM Members ORDER BY JediName";
 
// this will be needed to get special characters
mysql_query("SET NAMES 'utf8'");
 
$result=mysql_query($query);
//this line required for post back results
echo "<form method=post action=select_member.php>";
//the select name listbox is for getting results
//the size = # of lines to show in the listbox
echo "<select name=listbox size=10>Jedi Name</option>";
 
// printing the list box select command
 
while ($line = mysql_fetch_array($result)){//Array or records stored in $line
foreach ($line as $value)
{
echo "<OPTION value='$value'";
}
echo ">$value</OPTION>";
}
 
 
// $num=mysql_numrows($result);
 
mysql_close();
echo "</SELECT><br>";
?>
 
<?
// Next php code for selection
echo "<input type=submit value=Submit>";
 
$sel=$_POST[listbox];
echo "<br>";
echo $sel;
?>
 
<?
//if (isset($_POST['Submit'])){
//now that $sel has who was selected lets get all the info based on it
 
mysql_connect(localhost,$username,$password);
 
@mysql_select_db($database) or die("Unable to select database");
 
$query="SELECT * FROM Members where JediName='";
$query .= $sel ."'";
 
echo "<br>";
echo $query;
echo "<br>";
 
$result=mysql_query($query);
$row = mysql_fetch_array($result);
$JediName=$row['JediName'];
$Level=$row['JediLevel'];
$Status=$row['Status'];
$Updated=$row['Updated'];
$Updater=$row['Updater'];
$Email=$row['Email'];
$Comments=$row['Comments'];
$Jnum=$row['Jnum'];
 
mysql_close();
echo $JediName . " " . $Level . " " . $Status . " " . $Updated . " ";
echo $Updater . " " . $Email . " " . $Comments . " " . $Jnum;
//}
 
 
?>
<input type="text" name="something" value="<?php echo $JediName;?>"> 
<input type="text" name="something" value="<?php echo $Level;?>"> 
<input type="text" name="something" value="<?php echo $Status;?>"> 
<input type="text" name="something" value="<?php echo $Updated;?>"> 
<input type="text" name="something" value="<?php echo $Updater;?>"> 
<input type="text" name="something" value="<?php echo $Email;?>"> 
<input type="text" name="something" value="<?php echo $Comments;?>"> 
<input type="text" name="something" value="<?php echo $Jnum;?>"> 
 
<! <textarea name="example" rows="10" cols="35">
<!<?php echo $Level;?></textarea> 
</BODY>
</HEAD>
 
Post Reply