Jump to content

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!

Edited by Ron916
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.

Edited by TOA

the form2 variable doesn't have any value you need to pass a value from the iteration like

 

$form2 = '<option value='$counter'>$counter</option>'; // this will contain a value for your variable form2.

Edited by JohnTipperton
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.