Forum Moderators: coopster

Message Too Old, No Replies

php variable in html

         

gtman

1:48 pm on Mar 19, 2009 (gmt 0)

10+ Year Member



Hello everybody,
in this php code i want after print mysql results...create 6 checkbox with different id. For example: first checkbox has id=1, second checkbox id=2 ...etc. But <?php echo $j; ?> doesn't work...Please help me...

Here is a part of php code
<?
while ($row = mysql_fetch_assoc($result)){
foreach ($row as $col=>$val){
print " $val " ;

$j=0;
for ($i=0; $i<=5; $i++){
$j=$j+1;
echo ' <input id="<?php echo $j; ?>" name="1" type="checkbox" value="" />

';
}
// $content .= "<div align=\"center\">"; <input id='$j'; name="1" type="checkbox" value="" />';
print "<br /> <br />";
}}
?>

d40sithui

2:36 pm on Mar 19, 2009 (gmt 0)

10+ Year Member



Hey try this::

<?
$j=0;
for ($i=0; $i<=5; $i++){
$j++;
echo '<input id="'.$j.'" name="1" type="checkbox" value="" />';
}
?>

Variables inside single quotes will not be evaluated, which is why you need to concatinate. Alternatively, you can also use echo "<input id=\"$j\" name=\"1\" type=\"checkbox\" value=\"\" />";

penders

2:42 pm on Mar 19, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



echo ' <input id="<?php echo $j; ?>" name="1" type="checkbox" value="" />

You are already in a PHP code block, so you shouldn't be trying to open another code block within it...

echo ' <input id="chk'.$j.'" name="chk1" type="checkbox" value="" />

Also, I've added a prefix of 'chk' to your ID and NAME attributes since these must start with a letter (Use any letter - HTML standard).

gtman

3:14 pm on Mar 19, 2009 (gmt 0)

10+ Year Member



Thank you very very much! you are perfect! Penders...if id not start with a letter where is the problem? just for html standard?

penders

3:32 pm on Mar 19, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



...if id not start with a letter where is the problem? just for html standard?

ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").
Source: http://www.w3.org/TR/html4/types.html#type-name

Yes, HTML standard. But if you don't follow the standards then it might not work in all/future browsers. There is usually a reason for the standards, they are not intended to make things harder. It could also be a problem with PHP. If

register_globals
is set then PHP will create a variable that matches your NAME attribute. PHP cannot have variables that start with a number either.

gtman

3:40 pm on Mar 19, 2009 (gmt 0)

10+ Year Member



Thank you...:)