Notices
Computer & Technology Related Post here for help and discussion of computing and related technology. Internet, TVs, phones, consoles, computers, tablets and any other gadgets.

PHP Help

Thread Tools
 
Search this Thread
 
Old Feb 2, 2004 | 10:32 AM
  #1  
marcmann's Avatar
marcmann
Thread Starter
Scooby Regular
 
Joined: Oct 2002
Posts: 163
Likes: 0
Post

Hi,

I've created a PHP script which lists a directory on my web server. It then dynamically creates a table which assigns a row to each file that it finds in the directory and also a Form checkbox for each file. It also dynamically assigns the name of the file as the value for the checkbox.

The user will only be able to select one item at a time. On the next page (once the user has submitted the form) I want to take the file that the user has selected and do some additional work on it - importantly though I need to know the name of the file the user has selected.

Here's my problem:

Normally I'd use the $_POST[valueofcheckbox] to process a checkbox from a form. However as each Checkbox value is dynamically generated I have no way of knowing what the value will be (as it's value will be a filename).

Does anyone know how I can do this?

I hope this all makes sense.

Marc
Reply
Old Feb 2, 2004 | 11:20 AM
  #2  
legacyPete's Avatar
legacyPete
Scooby Regular
 
Joined: Dec 2001
Posts: 202
Likes: 0
Post

If you only want to select 1 file it would be a lot easier to use radio buttons instead of checkboxes.

<?
if($_POST['file']) {
echo "You selected ".$_POST['file'];
} else {
?>
<form name="form1" method="post" action="list.php">
<?
$dir = "images/*.gif";
$shell_command = "ls $dir -1";
exec($shell_command, $file_list, $return);

for($i=0; $i<count($file_list); $i++) {
echo $file_list[$i].'<input type="radio" name="file" value="'.$file_list[$i].'">
<br>';
}
?>
<input type="submit" name="Submit" value="Submit">
</form>
<?
}
?>

If you do use checkboxes, you should set them with the same name so you can access them as an array in PHP:

<input type="checkbox" name="chkbox[]" value="filename1">
<input type="checkbox" name="chkbox[]" value="filename2">

When submitted, these checkboxes would be accessed via an array: $_POST['chkbox'][0] / $_POST['chkbox'][1]

It would be up to you to make sure only 1 is selected / processed.

Edit: stupid bbs code!

[Edited by legacyPete - 2/2/2004 11:21:50 AM]
Reply
Old Feb 2, 2004 | 11:22 AM
  #3  
marcmann's Avatar
marcmann
Thread Starter
Scooby Regular
 
Joined: Oct 2002
Posts: 163
Likes: 0
Post

thanks for the reply Pete - seems to make sense....i'll give it ago and let you know...

Marc
Reply




All times are GMT +1. The time now is 01:37 PM.