【问题标题】:How to echo a variable which is sent by php form如何回显由 php 表单发送的变量
【发布时间】:2016-03-30 15:39:35
【问题描述】:

我有从其他页面获取变量的 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 数据库 上回显或插入,但我无法在 test 上回显或插入到 mysql 查询它们.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'];
    }
}
?>

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

【问题讨论】:

  • 但是form中没有f_name和l_name的字段...;

标签: php mysql html forms submit


【解决方案1】:

将其存储到表单中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

【讨论】:

    【解决方案2】:

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

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

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-03
      相关资源
      最近更新 更多