I have an associative array of file names and their paths. I can successfully echo the contents of the array using a foreach loop. The number of items in the array will vary.
foreach($filepath as $filename => $filepath) {
echo "Filename=" . $filename . ", Filepath=" . $filepath."<br>";
}
I want to be able to use the array contents within a form like below:
<input type="checkbox" data-url="filepath1 />filename1
<input type="checkbox" data-url="filepath2 />filename2
<input type="checkbox" data-url="filepath3 />filename3
<input type="checkbox" data-url="filepath4 />filename4
I have tried this:
<form action="#" id="myform">
<?php
foreach($filepath as $filename => $filepath) {
?>
<input type="checkbox" data-url="<?php echo $filepath ?>" /><?php echo $filename ?>
<?php
}
?>
<button type="submit">Submit</button>
</form>
but all I get is the submit button - no checkbox or filename. I think that I may need to add id and name to the input statement but am not sure how to do this so that it is unique on each loop. Thank you for any help.
CodePudding user response:
Please try following code.
<form action="#" id="myform">
<?php
foreach($filepath as $filename => $filepath) {
?>
<input type="checkbox" name="filepath[]" value="<?php echo $filepath ?>" /><?php echo $filename ?>
<?php
}
?>
<button type="submit">Submit</button>
</form>
CodePudding user response:
Try this one:
<form action="#" id="myform">
<?php
$count = 1;
foreach($filepath as $filename => $path) {
?>
<input id="<?php echo 'checkbox_' . $count; ?>" type="checkbox" data-url="<?php echo $path ?>" /><?php echo $filename ?>
<?php
$count ;
}
?>
<button type="submit">Submit</button>
</form>