Page 1 of 1

how to make an html tag value a php variable?

Posted: Mon Dec 28, 2009 11:52 am
by imsoconfused
Hello everyone,
I am lost this is the problem.

I am creating a group of html form checkboxes and if any of them are outputted i am sending the values to a php array..

Code: Select all

 
echo '<form action="<span style='color:blue' title='I&#39;m naughty, are you naughty?'>smurf</span>.php" method="post" />';
 
for ($i=0; $i<5; $i++)
{
echo '<input type="checkbox" name="$eraseThis[]" value="$i">';
}
echo '</form>';
 
 
this will output 5 checkboxes and each box should have a value of $i so that when array $eraseThis IS accessed, if any of the checkboxes were checked the array will contain the value of the box's selected.

the value is not going through how can i get the html value to be a php variable?

Re: how to make an html tag value a php variable?

Posted: Mon Dec 28, 2009 12:07 pm
by AbraCadaver
Several problems:

1. PHP variables, $i in your example aren't expanded in single quotes, only double quotes or when outside of the single quotes
2. $eraseThis[] is not a valid HTML form input name, try eraseThis[]
3. You need a submit button

Code: Select all

echo '<input type="checkbox" name="eraseThis[]" value="'.$i.'">';
In the target smurf.php you will access the array as $_POST['eraseThis'].

Re: how to make an html tag value a php variable?

Posted: Mon Dec 28, 2009 12:31 pm
by manohoo
Let me expand on AbraCadaver's reply. Try this:

Code: Select all

$eraseThis = array ('a','b','c','d','e'); // for example
echo '<form action="smurf.php" method="POST">';
$i=0; // initialize variable
foreach ($eraseThis as $row)
{
$i++; // increase by 1 at each iteration
echo "$row <input type='checkbox' name='$row' value='$i'><br />";
}
echo '<input type="Submit" value="Submit">';
echo '</form>';
"foreach" is designed for arrays, it works much better than "for" loops