PHP 不会将变量放入 link

PHP won't put variable in link

在我的网站开头,我要求一个名字。然后我将名称传递给具有相同 $name 变量的主页。在他们的主页中,他们可以按按钮 "something" 将他们重定向到 URL 中带有变量 $name 的网页。在主页上,它显示 echo "<h1>$name's Profile</h1>"; 中的 $name 变量的值,但由于某种原因,当我在 link 中使用它时,它是未定义的。这是代码:

<!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>

该代码假定您有一个名为 $name's not $name 的变量。

关闭“并使其成为:

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

或在字符串中使用 {},例如:

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

以下应该有效

<!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>