Forum Moderators: coopster

Message Too Old, No Replies

Writing to Multiple File Names

         

inveni0

9:39 pm on Dec 29, 2005 (gmt 0)

10+ Year Member



Here's what I have:

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.

BadGoat

9:47 pm on Dec 29, 2005 (gmt 0)

10+ Year Member



For checkboxes, I create my SQL table to have a default value of 'NO', and have the checkbox field as such:

<input type="checkbox" name="headache" value="YES">

When a checkbox is checked, it enters a YES into the table.

And then query your table to find where the value = YES and display.

inveni0

9:50 pm on Dec 29, 2005 (gmt 0)

10+ Year Member



That's not what I'm looking for. I'm not wanting to deal with Tables. I'm looking to allow a user to actually write data to multiple text files by entering their data into a text field and then selecting the filename from a series of checkboxes or a list/menu.

Mr_Fern

9:54 pm on Dec 29, 2005 (gmt 0)

10+ Year Member



Set the checkbox name as an array

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);
}

inveni0

10:16 pm on Dec 29, 2005 (gmt 0)

10+ Year Member



I'm not sure I understand. How does the PHP code seperate the different checkboxes if they're all named, say, "checkbox[]". Do I have to use "checkbox[1]", "checkbox[2]", etc... in the HTML? And do I define each in the PHP script?

Could you explain a bit more? Here's what I'm using:

HTML:

<form name="form1" method="post" action="multiedit.php">
<p>&nbsp;</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;
}
?>

inveni0

10:27 pm on Dec 29, 2005 (gmt 0)

10+ Year Member



I guess what I'm really needing is a PHP expression that can list each variable string sent to it from the HTML form. Is there anything that can do this? Then I'd just need to add some code to take turns writing to each file name until all have been used.

Mr_Fern

10:33 pm on Dec 29, 2005 (gmt 0)

10+ Year Member



the brackets signify an array

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

inveni0

10:39 pm on Dec 29, 2005 (gmt 0)

10+ Year Member



Right. So, if I user the code name="checkbox[]" in HTML, I would still have to define each checkbox in PHP as:

_POST(checkbox[1]);
_POST(checkbox[2]);

(nevermind my syntax, lol, i forgot how you typed it)

Is that correct? That my PHP code would have to define each "key" of name="checkbox[]"?

Mr_Fern

11:01 pm on Dec 29, 2005 (gmt 0)

10+ Year Member



You wouldn't have to define them, they're defined after the form is submitted. The foreach code handles getting each value.

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.

inveni0

2:00 am on Dec 30, 2005 (gmt 0)

10+ Year Member



Thank you, I will try it at work tomorrow!

inveni0

5:26 pm on Dec 31, 2005 (gmt 0)

10+ Year Member



It works! You're a stud. Thanks much for your detailed explanation!