【问题标题】:PHP, error when using $_GET['pFullName'];?PHP,使用 $_GET['pFullName']; 时出错?
【发布时间】:2016-05-17 11:52:13
【问题描述】:

我在尝试回显玩家姓名时遇到致命错误。

我对 PHP 还是很陌生,这可能是菜鸟的错误,但我什么都试过了。

这是我的代码:

<!doctype html>
<html>
    <head>
        <meta charset="utf-8">
        <title>A1S | e-tracker</title>
        <link rel="stylesheet" href="css/menu.css">
        <link rel="author" href="humans.txt">
    </head>
    <body>

        <form action="#" method="get">
            Name: <input type="text" placeholder="Player Full Name" name="pFullname"><br>
            Date of Birth: <input type="date" name="pDob"><br>
            Weight: <input type="number" name="pWeight" placeholder="Pounds" min="0"><br>
            Height: <input type="number" name="pHeight" placeholder="Centimeters" min="0"><br>
            <input type="submit">

            <?php
            $playerName = $_GET['pFullName'];
              echo $playerName;
             ?>
        </form>
        
        <script src="script.js"></script>
    </body>
</html>

任何帮助将不胜感激,谢谢!

【问题讨论】:

  • 您需要提交表单并将您的PHP代码放在form之外。
  • “我遇到了一个致命错误” - 我在您的帖子中看不到任何错误消息。

标签: php get


【解决方案1】:

使用下面的代码:

$playerName = isset($_GET['pFullname']) ?$_GET['pFullname'] :"";
echo $playerName;

【讨论】:

    【解决方案2】:

    另一种解决方案(如果您使用的是 PHP7):

    <?php
    $playerName = $_GET['pFullname']??'';
    echo $playerName;
    

    ?? - Null coalescing operator

    已添加空合并运算符 (??) 作为语法糖,用于需要将三元组与 isset() 结合使用的常见情况。如果存在且不为 NULL,则返回其第一个操作数;否则返回第二个操作数。

    所以等价于

    $playerName = isset($_GET['pFullname']) ? $_GET['pFullname'] : '';
    

    注意: 您使用的是pFullName 而不是pFullname。后面是控件名

    【讨论】:

      猜你喜欢
      • 2011-12-29
      • 2014-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-27
      相关资源
      最近更新 更多