how to make an html tag value a php variable?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
imsoconfused
Forum Newbie
Posts: 15
Joined: Mon Oct 05, 2009 10:55 pm

how to make an html tag value a php variable?

Post 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?
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

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

Post 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'].
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
manohoo
Forum Contributor
Posts: 201
Joined: Wed Dec 23, 2009 12:28 pm

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

Post 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
Post Reply