Ron916 Posted October 16, 2012 Share Posted October 16, 2012 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 More sharing options...
TOA Posted October 16, 2012 Share Posted October 16, 2012 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. Link to comment https://forums.phpfreaks.com/topic/269537-html-forms-saved-as-variables-and-echod/#findComment-1385558 Share on other sites More sharing options...
Ron916 Posted October 16, 2012 Author Share Posted October 16, 2012 woooooooooooot!! I had to put double quotes on: $form2 .= '<option value='$counter'>$counter</option>'; But its perfect nonetheless! Thank you! Link to comment https://forums.phpfreaks.com/topic/269537-html-forms-saved-as-variables-and-echod/#findComment-1385564 Share on other sites More sharing options...
TOA Posted October 16, 2012 Share Posted October 16, 2012 I had to put double quotes on: $form2 .= '<option value='$counter'>$counter</option>'; Oh, yeah, good catch. Missed that. Link to comment https://forums.phpfreaks.com/topic/269537-html-forms-saved-as-variables-and-echod/#findComment-1385575 Share on other sites More sharing options...
JohnTipperton Posted October 18, 2012 Share Posted October 18, 2012 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. Link to comment https://forums.phpfreaks.com/topic/269537-html-forms-saved-as-variables-and-echod/#findComment-1386061 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.