如何回显由 php 表单发送的变量

How to echo a variable which is sent by php form

我有 php 从其他页面获取变量的表单。代码如下:

<?php
    $fname = "$_POST[fname]";
    $lname = "$_POST[lname]";
//these value getting from another form
?>
<form action="test.php" method="post" class="form-inline vh_pre_reg_form">
<!-- Now I want to send them by this form-->
            <div class="form-group">
                <label>First Name</label>
                <span><?php echo $fname; ?></span>
            </div>
            <div class="form-group">
                <label>Last Name</label>
                <span><?php echo $lname; ?></span>
            </div>
<input name="submit" id="btnValidate" type="submit" value="Submit"/>
</form>

我想要,当我按下提交按钮时,这些将在操作页面 test.phptest.php页面,我的值会回显或插入到mysql数据库 但我无法回显或插入 mysql 在 test.php 上查询它们我的test.php页面代码如下:

<?php
if(count($_POST)>0) {

    /* Validation to check if Terms and Conditions are accepted */
    if(!isset($_POST["submit"])) {
    $message = "<h1>404 error !</h1><br /> <h2>Page not found !</h2>";
    }

    if(!isset($message)) {
        echo "I got submit butt press";
        echo "<br />";
        echo "here is the value : ".$_POST['$fname'];
    }
}
?>

请解决我的问题,如果可能的话,给我完整的代码..

您不应该将变量封装在字符串中,在超级全局 $_POST 上使用数组访问器。您还应该使用三元赋值来检查变量是否已设置,以便您的下一个问题不是关于 undefined index 错误。

$fname = isset($_POST["fname"]) ? $_POST["fname"] : false;
$lname = isset($_POST["lname"]) ? $_POST["lname"] : false;

接下来,不要检查 post 的计数是否大于零,而是检查您的表单变量是否 posted。

if(isset($fname, $lname)){
    echo $fname . ' - ' . $lname;
}

请注意,您还有 $_POST['$fname'] 这也不是正确的访问方式。

将其存储到表单中 submit 按钮之前的隐藏元素中,如下所示:

<input type="hidden" name="fname" value="<?php echo $fname; ?>" />
<input type="hidden" name="lname" value="<?php echo $lname; ?>" />
<input name="submit" id="btnValidate" type="submit" value="Submit"/>

并在您的 test.php 文件中回应它。

echo $_POST['fname'];   // name of the hidden input element
echo $_POST['lname'];   // name of the hidden input element