Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法回显某些PHP变量_Php - Fatal编程技术网

无法回显某些PHP变量

无法回显某些PHP变量,php,Php,我试图接受表单的输入,并将某些元素回显到html页面中。但由于某些原因,我定义的变量没有显示出来 以下是我检索表单并设置变量的代码: <?php $firstName = $lastName = $email = $phone = $message = ""; if ($_SERVER["REQUEST_METHOD"] == "POST"){ $firstName = test_input($_POST['firstName']); $lastName = test_i

我试图接受表单的输入,并将某些元素回显到html页面中。但由于某些原因,我定义的变量没有显示出来

以下是我检索表单并设置变量的代码:

<?php 
$firstName = $lastName = $email = $phone = $message = "";
if ($_SERVER["REQUEST_METHOD"] == "POST"){
    $firstName = test_input($_POST['firstName']);
    $lastName = test_input($_POST['lastName']);
    $email = test_input($_POST['email']);
    $phone = test_input($_POST['phone']);
    $message = test_input($_POST['message']);
}

function test_input($data){
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
} ?>

这是我使用的表格:

     <div style="width: 50%; margin:0 auto;border-style: solid;">
        <?php echo $_POST["message"]; ?>
        <?php echo $message; ?>
        <form action="<?php echo htmlspecialchars($_SERVER["$_PHP_SELF"]) ?>" method="POST" id="contactForm">
            First Name: <input type="text" id="firstName" name="firstName"><br>
            Last Name: <input type="text" id="lastName" name="lastName"><br>
            Email: <input type="text" id="email" name="email"><br>
            Phone: <input type="text" id="phone" name="phone"><br>
            Leave a message:<br>
            <textarea id="message" name="message"></textarea><br>
            <input type="submit">   
        </form>
    </div>


test\u输入不返回任何内容,因此
$message
的内容为空$消息可能正在回显,它回显“nothing”。

您的函数不会返回任何内容

这里是更新的代码

function test_input($data){
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
}

您好,David,我想当您到达HTML时,变量丢失了,所以我会尝试检查其他变量是否正在返回您的
test\u input
函数没有返回任何内容,但您正在将其返回值分配给这些变量…实际上是“nothing”。而且
test\u input()
是一个糟糕的函数名。它不是这样做的,最好将其称为
任意_-escaping()
。在你发现推荐的地方留下一张便条。另请参见:我真的希望您不要在任何数据库操作之前使用
test\u input()
来转义数据,因为这远远不够。不,数据不会进入数据库。我只是想避免脚本被注入html。哇,谢谢。我找了很长时间,不敢相信我忽略了这一点。