Forum Moderators: coopster
An HTML file with a form. The form contains ten filenames (ex. 1.txt, 2.txt, etc...). Each filename is paired with a checkbox. At the bottom of the form, there is a textfield and submit button.
I also have a PHP file that writes text to a file based on which file the user picks via a RADIO BUTTON. This works fine.
Now, I need to enable the new HTML form (the one with checkboxes) and PHP file so that the user can write the same text (defined in the textfield) to multiple files via the checkboxes.
How do I get PHP to detect which boxes are checked?
Also, I'd like to be able to add more file names to the HTML form without having to update the PHP code. I need the PHP code to auto-detect any checkboxes passed through via the form.
Instead of:
<input type="checkbox" name="update_file" value="{filename}">
You'd have:
<input type="checkbox" name="update_file[]" value="{filename}">
This way if you say checked 3 boxes, they'd be accessible as:
$_POST['update_file'][0], $_POST['update_file'][1], and $_POST['update_file'][2]
Your update process could be done as such:
$file_contents = $_POST['textfield'];
foreach ($_POST['update_file'] as $key => $file_name) {
$fp = fopen($file_name,"w");
fwrite($fp, $file_contents);
fclose($fp);
}
Could you explain a bit more? Here's what I'm using:
HTML:
<form name="form1" method="post" action="multiedit.php">
<p> </p>
<p>
<label>
<input type="checkbox" name="checkbox[]" id="checkbox[]" value="photo1.txt">
photo1.txt</label>
<br>
<label>
<input name="checkbox[]" type="checkbox" id="checkbox[]" value="photo2.txt">
photo2.txt</label>
</p>
<p> <br>
</p>
<p>
<input name="textfield" type="text" size="100" maxlength="250">
<input type="submit" name="Submit" value="Submit">
</p>
</form>
PHP:
<?
$usertext = $textfield;
foreach ($_POST['checkbox[]'] as $key => $file_name) {
$fp = fopen($file_name,"w");
fwrite($fp, $usertext);
fclose($fp);
print $file_name;
}
?>
Array_Variable[key] = value
If you wrote:
<input type="hidden" name="testing[]" value="value1">
<input type="hidden" name="testing[]" value="value2">
the PHP script after submission has
$_POST['testing'][0], which equals: value1
$_POST['testing'][1], which equals: value2
If you wrote:
<input type="hidden" name="testing['name']" value="inveni0">
<input type="hidden" name="testing['site']" value="webworld">
the PHP script after submission has
$_POST['testing']['name'], which equals: inveni0
$_POST['testing']['site'], which equals: webworld
Ok so you say there's 10 checkboxes (and you want to add more). The PHP code I wrote as a guide doesn't care how many possible checkboxes there are, that's handled at runtime.
Example (5 checkboxes):
<input type="checkbox" name="checkbox[]" value="photo.txt">
<input type="checkbox" name="checkbox[]" value="name.txt">
<input type="checkbox" name="checkbox[]" value="testing.txt">
<input type="checkbox" name="checkbox[]" value="games.txt">
Suppose I click the checkbox for photo.txt and games.txt
Suppose for the text field I put "test data"
After submitting the form, the PHP goes through foreach loop
$text = $_POST['textfield'];
foreach($_POST['checkbox'] as $key => $value) {$file = "/path/to/$value";
$fp = fopen($file,"w");
fwrite($fp,$text);
fclose($fp);}
What Happens:
Foreach reads the first array key and value pair, which is 0 => photo.txt, opens photo.txt, writes "test data" and closes the file. It then goes to the next array key and value pair, which is 1 => games.txt, opens games.txt, writes "test data" and closes the file. There are no more array values, so it's done.
Extra Note:
The key isn't important, just the value
You could also use
foreach($_POST['checkbox'] as $value)
to make that clearer. It works the same as the first.