简体   繁体   中英

Input fields not displaying value after form submit?

I am having a little issue getting the form values to display after i have submitted the form. The main thing is when the form is submitted then all input fields go blank. But the fields DO DISPLAY when Details are not Updated (from if statment).

<?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>

i have just confirmed that adding:

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

After the Database Update solves the issue, HOWEVER i dont really want to have to query the Database again. Is it possible not to query again? Or doesnt it matter much?

Is the page you've quoted above account.php?

If so, header("Location: account.php"); is basically redirecting the browser to the page in question, and with that, losing all of the post data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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