Page 1 of 1

Help with usin checkboxes

Posted: Mon May 15, 2006 9:09 am
by spudmclard
Hi can anyone help with this...

What im after is to be able to select several checkboxes and depending upon the selections these are added to a flat text file..
Code Follows

Code: Select all

<?php
$file_name = "test.txt";
$fp = fopen($file_name, "r");
echo "<br><form action='$_SERVER[PHP_SELF]'>";
while($line = fgets($fp))
{ 
echo "<input name='$line' type='checkbox' value='$line'>$line<br>";
}
echo "<br>";
fclose($fp); 
echo "<input type='submit'>";
?>
<HR>
<? 
if (isset($_POST['submit'])) {
$name = $_POST['$line'];
$fp = fopen("list.txt", "a");
if(!$fp) {
echo 'Error, the file could not be opened';
exit;
}
fwrite($fp, $name."n");
}
?>

HTML and more

Posted: Mon May 15, 2006 9:23 am
by tr0gd0rr
Start with a few things, then let us know where exactly you are having trouble.

Code: Select all

echo "<br><form action='$_SERVER[PHP_SELF]'>"; // should literally print "'Array[PHP_SELF]'"
echo "<br><form action=\"{$_SERVER['PHP_SELF']}\">"; // correct

Code: Select all

echo "<input type='submit'>"; // cannot be accessed by $_POST['submit'] 
echo '<input type="submit" name="submit">'; // correct

Code: Select all

$name = $_POST['$line']; // is looking for the literal string "$line"
$name = $_POST[$line]; // correct
I would suggest a careful read of the following page of the PHP manual: http://us3.php.net/manual/en/language.types.string.php

PS. Don't forget to close your form tag.

Posted: Mon May 15, 2006 9:39 am
by spudmclard
Thanks

Ive made the changes.

The problem im having is nothing is being written to the 'list.txt' file. All file permissions are ok, so id appreciate any help.
Thanks