dynamic creation of drop down box - selected options blank

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
pizzipie
New php-forum User
New php-forum User
Posts: 19
Joined: Sun Aug 04, 2019 4:10 pm

Mon Dec 30, 2019 5:12 pm

Hi,
I'm trying to create a drop-down box populated with database files to pick from. I am getting blank values. Code below:

Results here:
Select.png
Select.png (11.03 KiB) Viewed 183 times
Selected.png
Selected.png (12.66 KiB) Viewed 183 times

Code: Select all

 <?php 
 
error_reporting (E_ALL ^ E_NOTICE);
include("../myPhpFunctions.inc");          // contains lf();   sp();  myprint; check_input(); 
//                                            shows   break   space   print_r  on browser  
 
$orgdir=getcwd();
$dir="/home/rick/DB-sql/"; 
$test=$files=array();

// =====================================================

// change to working directory where the databases are and get the *.db files
 
chdir($dir); 

// get the available db file names

if (is_dir($dir)){
  if ($dh = opendir($dir)){                        // open dir and read contents
   	while (($file = readdir($dh)) !== false){
    		if(substr($file, -2)=="db") {
				$files[]=$file;
			}	
		}
	}	
}

//  =====================================================

$test[0]="zero.db";       // this array for testing only in lieu of the $files array above 
$test[1]="one.db";
$test[2]="two.db";

?>
 
 <!DOCTYPE HTML>  
 
 <html>
<head> 
</head>
<body>

<form action="#" method="post">
  	    	
<p>Database</p>

<select multiple="multiple" name="database[]">

<?php
foreach($test as $file) {         

?> 
	
<option value=><?php echo $file; ?></option>


<?php 
} // foreach 

?>
</select>

<p><input type="submit" name="submit" value="Get Selected Values" /></p>
</form>
<br />

</body>
</html>


<?php

//  ============================   shows all empty elements  ===============

if(isset($_POST['submit'])){
	
myprint($_POST['database']); bye(77);	
	
	foreach ($_POST['database'] as $select) {
		echo "You have selected :" .$select; // Displaying Selected Value
	}
}
    closedir($dh);


chdir($orgdir);
?> 
User avatar
hyper
php-forum GURU
php-forum GURU
Posts: 927
Joined: Mon Feb 22, 2016 5:52 pm

Tue Dec 31, 2019 9:28 am

Run this code (your code stripped back):

Code: Select all

<?php 

$test[0]="zero.db";       // this array for testing only in lieu of the $files array above 
$test[1]="one.db";
$test[2]="two.db";

foreach ($test as $file) {?>
<option value=><?php echo $file; ?></option>
<?php }
and have a look at the html source produced (ctrl+u).
pizzipie
New php-forum User
New php-forum User
Posts: 19
Joined: Sun Aug 04, 2019 4:10 pm

Tue Dec 31, 2019 10:00 am

Thanks for the help,

I figured it out. My solution:

Code: Select all

<form action="#" method="post">
  	    	
<p>Database</p>

<select multiple="multiple" name="database[]">

<?php
	foreach($files as $file) {         
		[b]echo "<option value=".$file.">".$file."</option>".lf();   <====== this does it![/b]
	} // foreach 
?>

</select>

<p><input type="submit" name="submit" value="Get Selected Values" /></p>
</form>
User avatar
hyper
php-forum GURU
php-forum GURU
Posts: 927
Joined: Mon Feb 22, 2016 5:52 pm

Tue Dec 31, 2019 10:48 am

Well done, it can be difficult to see the wood for the trees sometimes :)
pizzipie
New php-forum User
New php-forum User
Posts: 19
Joined: Sun Aug 04, 2019 4:10 pm

Tue Dec 31, 2019 1:15 pm

Thank You.

HAPPY NEW YEAR !

R
Post Reply