繁体   English   中英

表单提交后输入字段不显示值?

[英]Input fields not displaying value after form submit?

提交表单后,显示表单值时出现一个小问题。 最主要的是,提交表单后,所有输入字段都将变为空白。 但是,当“详细信息”未更新时,字段会显示(从if语句开始)。

<?php
require 'core/init.php';

$auth = new Auth();

if (isset($_SESSION['customer_id'])) {
        $rows = DBPDO::getInstance()->get('customer', array(
                array('id', '=', $_SESSION['customer_id'])
            ));

        if ($_SERVER['REQUEST_METHOD'] == 'POST') {

            $password1 = $_POST['password1'];
            // Verify Password Matches Account
            $password1 = $rows->first()->user_salt . $password1;
            $password1 = $auth->hashData($password1);
            // New Password
            $newsalt = $auth->randomString();
            $password2 = $_POST['password2'];
            $newpassword2 = $newsalt . $password2;
            $newpassword2 = $auth->hashData($password2);

            $business = $_POST['businessname'];
            $name     = $_POST['contactname'];
            $email    = $_POST['contactemail'];
            $code     = $_POST['contactcode'];
            $phone    = $_POST['contactphone'];

            if (!empty($password1) && $password1 == $rows->first()->password && empty($password2)) {

                $update = DBPDO::getInstance()->update('customer', $_SESSION['customer_id'], array(
                            'businessName'      => $_POST['businessname'],
                            'contactName'       => $_POST['contactname'],
                            'email'             => $_POST['contactemail'],
                            'code'              => $_POST['contactcode'],
                            'phone'             => $_POST['contactphone'],
                            'deliveryAddress'   => $_POST['deliveryaddress']
                        ));
                $_SESSION['errmsg'] = "Details Updated!";
                header("Location: account.php");

            } elseif (!empty($password1) && $password1 == $user->first()->password && !empty($password2)) {
                echo 'Details and Password Updated';
            } else {
                echo 'Details not Updated';
            }

        }  

    } else {
        header('Location:index.php');
    }


?>

HTML

<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">    
<div class="col-md-6">
    <div class="form-group">
        <label for="businessname">Business Name</label>
        <div class="input-group input-group-lg">
            <input type="text" name="businessname" id="businessname" class="form-control" value="<?php echo $rows->first()->businessName; ?>" aria-required="true"/>
        </div>
    </div>
</form>

我刚刚确认添加:

$rows = DBPDO::getInstance()->get('customer', array(
                    array('id', '=', $_SESSION['customer_id'])
                ));

数据库更新解决了问题之后,但是我真的不想再次查询数据库。 是否可以不再次查询? 还是没关系吗?

您在account.php上方引用的页面是吗?

如果是这样,则header("Location: account.php"); 基本上是将浏览器重定向到相关页面,并因此丢失所有发布数据。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM