Jump to content

Best way to display a form with a variable in PHP


VelvetMirror
 Share

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?

Edited by VelvetMirror
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...