Jump to content

[SOLVED] Echo multiple lines of html code


Recommended Posts

Hi guys,

 

I would like to know if there is a way to echo multiple lines of html codes.

 

I intend to echo a form if a variable is set and nothing if it is not set. And right now, I echo each line of html code, which works well but is a lot to do for such a thing. I am pretty sure there should be a better way to do it but I can't find how...

 

Any thoughts?

Link to comment
https://forums.phpfreaks.com/topic/178229-solved-echo-multiple-lines-of-html-code/
Share on other sites

It depends on if there are any php variables or other php statements in the code.

 

If it is just a block of HTML, without any php in it, it is usually easier to put a closing ?> php tag, put the HTML, then put an opening <?php tag. You can also put multiple lines in a single echo statement, there is no need to put each line into a single echo statement.

 

Which method you use depends on what you are doing. Without seeing what you are actually doing, it is not possible to suggest the best method for the specific situation.

Sorry, I should have put the code I am using now. Here it is:

<?php
			if (!isset($_GET['actor'])) {

			} else {
				echo "<form action=\"edit_actor.php?actor=" . urlencode($sel_actor['id']) . " method=\"post\">";
				echo "<p>First name:<br />";
				echo "<input type=\"text\" name=\"first_name\" value=\"" . $sel_actor['first_name'] . "\" id=\"first_name\"></input></p>";
				echo "  ";
				echo "<p>Last name:<br />";
				echo "<input type=\"text\" name=\"last_name\" value=\"" . $sel_actor['last_name'] . "\" id=\"last_name\"></input></p>";
				echo "</p";
				echo "<p>Visible:";
				echo "<input type=\"radio\" name=\"visible\" value=\"0\"";
				if ($sel_actor['visible'] == 0) { echo " checked"; }
				echo "/> No  <input type=\"radio\" name=\"visible\" value=\"1\"";
				if ($sel_actor['visible'] == 1) { echo " checked"; }
				echo "/> Yes";
				echo "</p>";
				echo "<p>Content:<br />";
				echo "<textarea id=\"content\" name=\"content\" rows=\"5\" cols=\"60\">" . $sel_actor['accents_dialects'] . "</textarea>";
				echo "</p>";
				echo "<input type=\"submit\" name=\"submit\" value=\"Edit Actor's page\" />";
				echo "  ";
				echo "<a href=\"delete_actor.php?actor=" . urlencode($sel_actor['id']) . "&currentpage=" . $currentpage . " onclick=\"return confirm('Are you sure you want to DELETE this ACTOR's page?');\">Delete Actor's page</a>";
				echo "</form>";
				echo "<br />";
				echo "<a href=\"content_actor.php?currentpage=" . $currentpage . "\">Cancel</a>";
			}
		?>

 

There are variables in the form, that's why I was using echo...

Could this work:

<?php
			if (!isset($_GET['actor'])) {

			} else { ?>
				<html><body><form action=\"edit_actor.php?actor=" . urlencode($sel_actor['id']) . " method=\"post\">
				<p>First name:<br />
				<input type=\"text\" name=\"first_name\" value=\"" . $sel_actor['first_name'] . "\" id=\"first_name\"></input></p>
				  
				<p>Last name:<br />
				<input type=\"text\" name=\"last_name\" value=\"" . $sel_actor['last_name'] . "\" id=\"last_name\"></input></p>
				</p>
				<p>Visible:
				<input type=\"radio\" name=\"visible\" value=\"0\"
				<?php if ($sel_actor['visible'] == 0) { echo " checked"; ?>
				/> No  <input type=\"radio\" name=\"visible\" value=\"1\"
				<?php if ($sel_actor['visible'] == 1) { echo " checked"; ?>
				/> Yes
				</p>
				<p>Content:<br />					<textarea id=\"content\" name=\"content\" rows=\"5\" cols=\"60\"><php? " . $sel_actor['accents_dialects'] . " ?></textarea>";
				</p>
				<input type=\"submit\" name=\"submit\" value=\"Edit Actor's page\" />
				  
				<a href=\"delete_actor.php?actor=<?php " . urlencode($sel_actor['id']) . "&currentpage=" . $currentpage . " ?> onclick=\"return confirm('Are you sure you want to DELETE this ACTOR's page?');\">Delete Actor's page</a>";
				</form>
				<br />";
				<a href=\"content_actor.php?currentpage=" . $currentpage . "\">Cancel</a>
<?php

}
		?>

 

Nopt sure but wouldn't something like that work>?

Nope, it does not work Kaboom, the form does not show up, even though the variable is set... :(

 

leave the }else{ ?> part then do your html code using <?php PHP CODE HERE ?> inside the html then at the bottom put

<?php
       }
?> 

to close the whole thing and it should work. I didn't do that on all your code

Because you have php functions, variables, and logic, I would use printf -

 

<?php
            if (!isset($_GET['actor'])) {
               
            } else {
			$format = '<form action="edit_actor.php?actor=%s" method="post">
				<p>First name:<br />
				<input type="text" name="first_name" value="%s" id="first_name"></input></p>
				  
				<p>Last name:<br />
				<input type="text" name="last_name" value="%s" id="last_name"></input></p>
				</p>
				<p>Visible:
				<input type="radio" name="visible" value="0" %s /> No  
				<input type="radio" name="visible" value="1" %s /> Yes
				</p>
				<p>Content:<br />
				<textarea id="content" name="content" rows="5" cols="60">%s</textarea>
				</p>
				<input type="submit" name="submit" value="Edit Actor\'s page" />
				  
				<a href="delete_actor.php?actor=%s&currentpage=%s" onclick="return confirm(\'Are you sure you want to DELETE this ACTOR\'s page?\');">Delete Actor\'s page</a>
				</form>
				<br />
				<a href="content_actor.php?currentpage=%s">Cancel</a>';
			printf($format,
			urlencode($sel_actor['id']),
			htmlentities($sel_actor['first_name']),
			htmlentities($sel_actor['last_name']),
			($sel_actor['visible'] == 0) ? " checked='checked'" : "",
			($sel_actor['visible'] == 1) ? " checked='checked'" : "",
			htmlentities($sel_actor['accents_dialects']),
			urlencode($sel_actor['id']),
			$currentpage,
			$currentpage);
            }
         ?>

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.