michaelp07 Posted January 6, 2009 Share Posted January 6, 2009 I am trying to create a custom function that allows me to simplify form creations. Here is my code: function text($input) { ## Open TR echo "\t<tr>\n"; ## Label TD, descriptor class echo "\t\t<td class=\"descriptor\">"; echo "<label for=\"$input\">" . ucwords(str_replace('_', ' ', $input)) . "</label>"; echo "</td>\n"; ## Form Input TD echo "\t\t<td>"; echo "<input type=\"text\" name=\"$input\" id=\"$input\" value=\"<?=\$$input?>\" />"; echo "</td>\n"; echo "\t</tr>\n"; } text('firstname'); It works just fine, except that it puts "<?=$input?>" as the value, instead of sending the php code. When the user submits the form, I will have a something like $firstname = htmlspecialchars($_POST['firstname']); I want it to have that $firstname in the value of the input, so that if the submitted form is rejected, the user retains the information. I'm not an expert. Please be merciful, and I am willing to learn anything I need. Thanks in advance. Link to comment https://forums.phpfreaks.com/topic/139729-echo-_post-variable-from-function/ Share on other sites More sharing options...
DamienRoche Posted January 6, 2009 Share Posted January 6, 2009 This line: echo "<input type=\"text\" name=\"$input\" id=\"$input\" value=\"<?=\$$input?>\" />"; should be: echo "<input type=\"text\" name=\"$input\" id=\"$input\" value=\"$input\" />"; You're already in php when you are echoing so there is no need to use the php tags. Link to comment https://forums.phpfreaks.com/topic/139729-echo-_post-variable-from-function/#findComment-731050 Share on other sites More sharing options...
michaelp07 Posted January 6, 2009 Author Share Posted January 6, 2009 Thanks for the reply. You helped me reach a solution. The code works as this: echo "<input type=\"text\" name=\"$input\" id=\"$input\" value=\"$_POST[$input]\" />"; Link to comment https://forums.phpfreaks.com/topic/139729-echo-_post-variable-from-function/#findComment-731095 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.