Jump to content

Echo within an echo


Eiolon

Recommended Posts

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>

';
}
?>

Link to comment
https://forums.phpfreaks.com/topic/207384-echo-within-an-echo/
Share on other sites

<?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>';
}
?>

Link to comment
https://forums.phpfreaks.com/topic/207384-echo-within-an-echo/#findComment-1084251
Share on other sites

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>
");
}
?>

Link to comment
https://forums.phpfreaks.com/topic/207384-echo-within-an-echo/#findComment-1084253
Share on other sites

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.

 

 

Link to comment
https://forums.phpfreaks.com/topic/207384-echo-within-an-echo/#findComment-1084280
Share on other sites

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.