user6184639
user6184639

Reputation: 15

PHP output echo wrong order

I have an issue with output in PHP I am trying to print out: Welcome back! Admin You are tester number: 3 Then underneath the buttons Logout and Comment. But it first outputs the buttons then the text.

Output result

Code:

<?php
session_start();
if (isset($_SESSION['id'])) {
	// session variables into local variables.
	$id = $_SESSION['id'];
	$username = $_SESSION['username'];
	$result = "Welcome back! <br>".$username. "<br> You are tester number: ".$id;
	echo ' <button class="btn" type="button" onclick=window.parent.location.href="logout.php" target="_parent">Log out</button>
	<button class="btn" type="button" onclick=window.parent.location.href="blog/post.php" target="_parent">Comment</button>
	';

	} else {
		$result = "You are not logged in yet";

	}

?>
<?php
echo $result;
?>
<title>Welcome - <?php echo $username ;?></title>

Upvotes: 0

Views: 1561

Answers (2)

DIEGO CARRASCAL
DIEGO CARRASCAL

Reputation: 2129

you are echoing the buttons and then $result... do this:

<?php
 session_start();
 if (isset($_SESSION['id'])) {
  // session variables into local variables.
   $id = $_SESSION['id'];
   $username = $_SESSION['username'];
   $result = "Welcome back! <br>".$username. "<br> You are tester number: ".$id."</br>";
   $result.=' <button class="btn" type="button" onclick=window.parent.location.href="logout.php" target="_parent">Log out</button><button class="btn" type="button" onclick=window.parent.location.href="blog/post.php" target="_parent">Comment</button>';

?>
<?php
   echo $result;
?>
<title>Welcome - <?php echo $username ;?></title>

Hope it helps.

Upvotes: 0

lisa
lisa

Reputation: 579

You echo the html part for the button before echoing the Welcome text.

What you could do:

$result = "Welcome back! <br>".$username. "<br> You are tester number: ".$id;
$result .= ' <button class="btn" type="button" onclick=window.parent.location.href="logout.php" target="_parent">Log out</button>
<button class="btn" type="button" onclick=window.parent.location.href="blog/post.php" target="_parent">Comment</button>
';

Upvotes: 1

Related Questions