如何在php中重新加载页面后重新填充数据

问题描述 投票:2回答:1

我被困在一点。我有一个PHP表格,在填写所有细节后,用户必须按提交按钮。如果数据没有正确提交,那么我正在重新加载页面。

所以我的问题是。

我想将用户输入的所有先前值设置为没有请求方法的相应字段。

我怎么能实现这个目标?

到目前为止我做了什么

我的user.php

<?php
$name = '';

if(isset($_REQUEST[name]))
{
   $name = $_REQUEST[name]; 
}

if(isset($_POST["submit"]))
{
   $name = $_POST["txtname"];

  header('Location:user.php?name=<?php echo $name; ?>');

}

?>

<hrml>
<body>
<form name="form1" id="form1" method="POST">
<label>Name:</label>
<input type="text" name="txtname" id="txtname" value="<?php echo $name; ?>">
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>

编辑

apply_now.php

    <?php 
    $CLASS__BL = 'apply_now_bl';
    require_once('apply_now_bl.php');

    ?>

    <form name="form1" id="form1" action="" method="post" enctype="multipart/form-data">


                    <div class="form-group">
                      <label for="input-label">Full Name: *</label>
                      <input name="txtname" id="txtname" type="text" class="form-control" value="<?= @$page_bl->full_name; ?>">
                    </div>
</form>

apply_now_bl.php

<?php 
require_once('admin/db_lib/candidates.php');
require_once('includes/general_include.php');

$page_bl = apply_now_bl();
page_load();

class apply_now_bl
{

    var $full_name = '';

function page_load()
    {
        $this->obj_candidates = new candidates();

        if(isset($_POST["submit"]))
        {
            $this->submit_data();
        }

    }

    function submit_data()
    {
        $this->full_name = get_post_val("txtname"); 
        $this->obj_candidates->full_name =  $this->full_name;

        redirect(apply_now.php); //common function for page redirect
    }
}
php forms http-post
1个回答
0
投票

无需通过php标头位置重定向。因为操作是空白的,这意味着一旦表单提交,它将重定向到user.php(当前页面)。

所以下面是更新的代码

<?php
$name = "";
if (isset($_POST["submit"])) {
    $name = $_POST["txtname"];
}
?>
<html>
    <body>
        <form name="form1" id="form1" method="POST">
            <label>Name:</label>
            <input type="text" name="txtname" id="txtname" value="<?php echo $name; ?>">
            <input type="submit" name="submit" value="submit">
        </form>
    </body>
</html>
© www.soinside.com 2019 - 2024. All rights reserved.