Eiolon Posted July 10, 2010 Share Posted July 10, 2010 I have a form that is displayed through an echo. Since it is already inside PHP tags, I am trying to figure out how to make the value of the form field echo: <?php if (isset($_POST['category'])) echo $_POST['category']; ?> so if there is an error it does not lose the value submitted. Here is my form: <?php if ($show_form == 1) { echo ' <form id="add_category" name="add_category" method="post" action="" class="form"> <input type="text" id="category" name="category" style="width:400px" maxlength="100" title="Enter a new category name here" value="" /> <input type="submit" id="submit" name="submit" class="submit" value="Submit"/> </form> '; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/207384-echo-within-an-echo/ Share on other sites More sharing options...
Pikachu2000 Posted July 11, 2010 Share Posted July 11, 2010 <?php if ($show_form == 1) { echo ' <form id="add_category" name="add_category" method="post" action="" class="form"> <input type="text" id="category" name="category" style="width:400px" maxlength="100" title="Enter a new category name here" value="'; if (isset($_POST['category'])){ echo $_POST['category']; } echo '" /><input type="submit" id="submit" name="submit" class="submit" value="Submit"/></form>'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/207384-echo-within-an-echo/#findComment-1084251 Share on other sites More sharing options...
BillyBoB Posted July 11, 2010 Share Posted July 11, 2010 That or <?php if ($show_form == 1) { echo(" <form id=\"add_category\" name=\"add_category\" method=\"post\" action=\"\" class=\"form\"> <input type=\"text\" id=\"category\" name=\"category\" style=\"width:400px\" maxlength=\"100\" title=\"Enter a new category name here\" value=\"".$_POST['category']?$_POST['category']:''."\" /> <input type=\"submit\" id=\"submit\" name=\"submit\" class=\"submit\" value=\"Submit\"/> </form> "); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/207384-echo-within-an-echo/#findComment-1084253 Share on other sites More sharing options...
.josh Posted July 11, 2010 Share Posted July 11, 2010 you don't technically need to only echo it out on a condition...if it doesn't exist nothing will be output. Also, for chunks of html it is a lot cleaner to use heredoc style syntax: echo <<<FORMENT <form id="add_category" name="add_category" method="post" action="" class="form"> <input type="text" id="category" name="category" style="width:400px" maxlength="100" title="Enter a new category name here" value="{$_POST['category']}" /> <input type="submit" id="submit" name="submit" class="submit" value="Submit"/> </form> FORMENT; It gives you the benefit of parsing the string like double quotes but without having to escape quotes. Also, you don't need to break out of quotes for variables (if you are using double quotes or heredoc). Wrapping {..} around the variable will tell php your intended variable boundaries. This will also keep your code more readable. Quote Link to comment https://forums.phpfreaks.com/topic/207384-echo-within-an-echo/#findComment-1084280 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.