-3

At the beginning of my website, I ask for a name. I then pass the name over to the homepage that has the same $name variable. In their homepage, they can press the button "something" to redirect them to the something webpage WITH the variable $name in the URL. On the homepage, it displays the value of the $name variable in the echo "<h1>$name's Profile</h1>";, but for some reason is undefined when I use it on the link. Here is the code:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Profile</title>
</head>
<body>
<center>
<?php
$name = $_GET['name'];
echo "<h1>$name's Profile</h1>";
echo "<hr>";
echo "<br>";
echo "<h3>Choose an option</h3>";
if(isset($_GET['btn'])) {
    $name = $_GET['name'];
    $loc = 'something.php?name=' . $name;
    header("Location: " . $loc);
    exit();
}

?>
<input type="Submit" value="Something" name="btn">
</center>
</body>
</html>
drlife
  • 3
  • 2
  • The code assumes you have a variable called `$name's` not `$name`. Close the " or use `{}` in the string like `echo {$name}'s` – Andreas Nov 03 '18 at 12:19
  • That's because you have no form. Edit: Given if that is your actual/full code. – Funk Forty Niner Nov 03 '18 at 12:21
  • Don't you think the form is on the previous page directing to this? – Andreas Nov 03 '18 at 12:22
  • Voting this as unclear. The part about getting input from a user and using a GET method, I don't get that (no pun intended). – Funk Forty Niner Nov 03 '18 at 12:25
  • 2
    First thing coming to my mind is the fact that you are [trying to modify header information after you started outputting](https://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php). But that doesn't explain the undefined bit. – Ivar Nov 03 '18 at 12:30

2 Answers2

0

Following should work

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Profile</title>
</head>
<body>
<center>
<?php
//First check if $_GET['name'] is set
if(isset($_GET['name'])){

  $name = $_GET['name'];
  //So here concatinate the strings and variables properly as following
  echo "<h1>".$name."'s Profile</h1>".
       "<hr>".
       "<br>".
       "<h3>Choose an option</h3>";

  if(isset($_GET['btn'])) {
      $name = $_GET['name'];
      $loc = "something.php?name=" . $name;
      header("Location: " . $loc);
      die();
  }else{
    //There was no btn parameter in the url means $_GET['btn'] isn't set notify the user
    echo "Can't Redirect The Page\nReason: Details Missing...";
  }
}else{
  //There was no name parameter in the url means $_GET['name'] isn't set notify the user
    echo "Can't Process The Request\nReason: Details Missing...";
}
?>
<input type="Submit" value="Something" name="btn">
</center>
</body>
</html>
Ussaid Iqbal
  • 786
  • 1
  • 7
  • 16
  • 1
    Please add what you changed and why that makes a difference. Right now, it is just a game of "Spot the difference" which isn't very useful for future visitors. – Ivar Nov 03 '18 at 13:14
-1

The code assumes you have a variable called $name's not $name.

Close the " and make it:

echo "<h1>" . $name . "'s Profile</h1>";

or use {} in the string like:

echo "<h1>{$name}'s Profile</h1>";
Andreas
  • 23,610
  • 6
  • 30
  • 62
  • what will they/you do about the input? there's no form and they're asking the user from input. Look at their question again. Edit: Being *"At the beginning of my website, I ask for a name."*. – Funk Forty Niner Nov 03 '18 at 12:22
  • The input on this page is "something" not name as the GET is using. This is probably page 2 of 3. First is the input name, second is output name and input something. And who knows about the third – Andreas Nov 03 '18 at 12:24
  • 2
    Side note: If you get a downvote, you can rest assured that it would not have been mine. I'll stick around for a bit to see how this unravels. – Funk Forty Niner Nov 03 '18 at 12:27