Jump to content

echo $_POST variable from function


michaelp07

Recommended Posts

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

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.

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.