Jump to content

Html Forms Saved As Variables And Echo'd


Ron916

Recommended Posts

Mornin',

 

I've created phpforms.php and am using that for quick access to forms. Inserting html and php data into a variable. Not even sure if this is a good idea... works ok until I put a while statement in, too many quotes??

 

This code works to populate a drop down list with the last 120 years:

 

<html>
<body>
<form action='./createuser.php' method='POST'>
<select name='year'>
<?php
$counter = date('Y');
while($counter > date('Y')-120)
{
echo "<option value='$counter'>$counter</option>";
$counter = $counter - 1;
}
</select>
</form>
</body>
</html>

 

 

But I'd like to insert this code into a variable as such:


<?php
$form2 = "
<form action='./createuser.php' method='POST'>
<select name='year'>
<?php
$counter = date('Y');
while($counter > date('Y')-120)
 {
 echo '<option value='$counter'>$counter</option>';
 $counter = $counter - 1;
 }
?>
</select>
</form>
";
?>


<html>
<body>
<?php echo $form2; ?>
</body>
</html>

 

The above code gives me a drop down box full of nottthiiinnngggggg, the only difference is that the whole thing is within double quotes now o.O

 

I'm not sure if/how I can replace the double quotes in the echo statement... or if there's a better way?

 

As always any and all comments are smexy and welcome!

Link to comment
https://forums.phpfreaks.com/topic/269537-html-forms-saved-as-variables-and-echod/
Share on other sites

Try this:

$form2 = "
<form action='./createuser.php' method='POST'>
<select name='year'> ";

$counter = date('Y');
while($counter > date('Y')-120)
	 {
	 $form2 .= '<option value='$counter'>$counter</option>';
	 $counter = $counter - 1;
	 }

$form2 .= "
</select>
</form>
";

 

Your php wasn't running inside those quotes. You only need the resulting html to be saved in the variable.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.