Pat Green
Pat Green

Reputation: 110

PHP form validation, keep field values after refresh

I am currently making a registration/validation script for a website and stumbled across a question.

I am using all PHP to validate and using if statements to echo the array of errors.

My question is: How can I keep the user's input values in the form after refresh? Must I learn AJAX?

form_handle.phpCODE:

    $errors = array();

if( empty($safe_fname) || 
    empty($safe_lname) || 
    empty($safe_email) || 
    empty($safe_email_again) || 
    empty($safe_password) )
        { 
        $errors = '<p>One or more fields left empty'; 
        }
elseif( strlen($safe_password) < 6)
    { 
    $errors = '<p>Password must be atleast 6 charcters long.</p>'; 
    }

elseif($safe_email != $safe_email_again)
    { 
    $errors = '<p>E-mails do not match.</p>'; 
    } 

The form CODE:

       <h2>Sign up Today!</h2>
          <form method="post" action="">
                   <span class="p" id="p1"></span><br/> 
          <input name="first_name" id="first" class="register_form cap" type="text" placeholder="First Name" />
              <br/><span class="p" id="p2"></span><br/> 
      <input name="last_name" id="second" class="register_form cap" type="text" placeholder="Last Name" />
          <br/><span class="p" id="p3"></span><br/> 
      <input name="email" id="third" class="register_form" type="text" placeholder="E-mail" />
          <br/><span class="p" id="p4"></span><br/> 
      <input name="email_again" id="fourth" class="register_form" type="text" placeholder="Re-enter E-mail" />
          <br/><span class="p" id="p5"></span><br/> 
      <input name="password" type="password" class="register_form" placeholder="Password" />
          <br/><span class="p" id="p1"></span><br/>   
      <input name="submit" id="fifth" class="register_form" type="submit" value="Create!" />

      </form>
</div>

Upvotes: 0

Views: 8057

Answers (4)

alberto
alberto

Reputation: 33

A bit late, but since PHP 7.0.x you can use the null coalescing (??) binary operator. The left-hand operand is the expression being evaluated, and the right-hand operand contains the value to be used in case the expression does not exist or is null. This eases code reading and eliminates chances of typo errors due to expression replication:

<expression> ?? <if-null-value>

is equivalent to:

isset(<expression>) ? <expression> : <if-null-value>

Based on that, I suggest the following answer to the question:

<input type='text' name='first_name' value='<?php echo $_POST['first_name'] ?? ''; ?>' />

Upvotes: 0

Epodax
Epodax

Reputation: 1828

A quick way to do this and avoid lots of code and errors are as such:

<input type='text' name='first_name' value='<?php echo isset($_POST['first_name']) ? $_POST['first_name'] : ''; ?>' />

Upvotes: 2

Asim Shahzad
Asim Shahzad

Reputation: 1609

Your code should be like

<input type='text' name='first_name' value='<?php echo $_POST['first_name']; ?>' />

Upvotes: 4

Md. Khalakuzzaman Khan
Md. Khalakuzzaman Khan

Reputation: 194

You can try like as follows-

<input name="first_name" id="first" class="register_form cap" type="text"
    <?php
    if(isset($_POST['first_name'])) {
        echo "value='".$_POST['first_name']."'";
    } else {
        echo "placeholder='First Name'";
    }
    ?>

    />

Upvotes: 0

Related Questions