Jump to content

Best way to display a form with a variable in PHP


VelvetMirror

Recommended Posts

First way:

<?php$name = "Introduce your name";echo "	<form action='index.php'>	<input type='text' id='name' value='$name' />	</form>";?>

Second way:

<?php$name = "Introduce your name";echo "<form action='index.php'>";echo "<input type='text' id='name' value='$name' />";echo "</form>";?>

Third way:

<form action='index.php'>	<input type='text' id='name' value='$name' /></form>

This last one wouldn't work since the $name variable isn't between php tags so it wouldn't be recognized. Obviously I could just type the variable value between the single quotes and it would work fine, but I want to use a variable, because if it was a $_GET variable for example, I couldn't do that, since I wouldn't know it's value beforehand. I don't like much the first two because eliminates the autoformat and highlighting of the code (I use netbeans)Any better alternative?

Link to comment
Share on other sites

You could heredoc:

echo <<< FORM<form action='index.php'>	<input type='text' id='name' value='$name' /></form>FORM;

Or you could just put in the PHP tags:

<form action='index.php'>	<input type='text' id='name' value='<?php echo $name; ?>' /></form>

Link to comment
Share on other sites

it will be something like

<form action='index.php'>	<input type='text' id='name' value='<?php echo $name; ?>' /></form>

<?php ?> inside this block php will parse the code. which will evaluate the value of $name

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...