John_Nil
John_Nil

Reputation: 135

Keep Form Data after php validation (POST method)

I'm validating my form with php and displaying error messsages but after every submit , the form is refreshed and the inputs are blank again . I was wondering if there was a way i could re-insert the user past data into the fields again .

I'm using POST method .

<form id="registration-form" action="" method="post">


<input type="text" name="username" id="username" placeholder="Username">

<button type="submit" name="_submit" id="submit" class="button-submit">Submit</button>


</form>

PHP function :

if( !isset( $_POST['_submit'] ) ){
        return array( $array, $arrayexport );
    }

$username = $_POST['username'];

// test function for username

if ( ($valid_username ) != 0 ) {

array_push($array, "Input Required"); }

else{

header( 'Location: /thankyou.php' );
}

Upvotes: 0

Views: 1532

Answers (2)

John_Nil
John_Nil

Reputation: 135

made a cleaner version .

Thanks to Abhik Chakraborty

<input type="text" name="username" id="username" placeholder="Username" value="<?php user()   ?>">

External php file :

function user(){
        if(isset($_POST['username'])){ echo $_POST['username'];}else{ echo '';}}

Upvotes: 0

Abhik Chakraborty
Abhik Chakraborty

Reputation: 44844

YOu can do something as

<input type="text" name="username" 
id="username" placeholder="Username" 
value="<?php if(isset($_POST['username'])){ echo $_POST['username'];}else{ echo '';}?>">

Upvotes: 1

Related Questions