PHP Help
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
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
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]
<?
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]


