0

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>

7
  • Well you´re echoing the buttons before the result ... Try putting the echo with buttons below echo $result; Commented Apr 18, 2016 at 14:05
  • Exactly what @Daan says, change your echo to this: $result .= ' Commented Apr 18, 2016 at 14:06
  • I have tried switching it other way around it is still thesame. Commented Apr 18, 2016 at 14:07
  • Did you try to change the code like I said? Commented Apr 18, 2016 at 14:07
  • 1
    @user6184639 Then you didn't switch it the other way around like I said. Commented Apr 18, 2016 at 14:09

2 Answers 2

1

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>
';
Sign up to request clarification or add additional context in comments.

2 Comments

@user6184639 Be careful when you mix 'delayed echoing' (store string in variable and echo it later) and directly echoing your string. That was your problem here. You should choose one and stick with it :-)
You should also define $username in the else part of your if/else statement otherwise you'll encounter an undefined variable error (you use it in the title tag but if the user isn't in the session, it won't have any value). For example : $username = 'Guest';
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.