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
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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.