0

I'm making a simple website for school where we have to include php to fill in forms. I'm not able to prefill and then save the content passed in the field. I've been following a pluralsight guide on how to prefill these fields, but it just aint working with me...

if (isset($_POST['submit'])){
    $ok = true;
    if (!isset($_POST['email']) || $_POST['email'] === ''){
        $ok = false;                    
    }
    else{
        $email = $_POST['email'];
    }
    if (!isset($_POST['password']) || $_POST['password'] === ''){
        $ok =false;
    }
    else{
        $password = $_POST['password'];
    }
    if ($ok){
        printf('Email: %s
        <br>Password: %s',
        htmlspecialchars(email),
        htmlspecialchars(password));


    }


}?>   <figure class="col-lg-6">
        <form method="post" action="login.php">
            <div class="row login">
                <div class="col-md-6">
                    <h3>Login</h3>
                    <div class="form-group">
                        <label for="exampleInputEmail">Email</label>
                        <input type="email" class="form-control" placeholder="Enter email*"  name="email" value="<?php echo htmlspecialchars($email);?>">
                    </div>
                </div>
            </div>
            <div class="row login">
                <div class="col-md-6">
                    <div class="form-group">
                        <label for="InputPassword">Password</label>
                        <input type="password" class="form-control" placeholder="Enter password*" name="password" value="<?php echo htmlspecialchars($password);?>">
                    </div>
                </div>
            </div>
            <button type="submit" class="btn tf-btn btn-default" name="login" >Login</button>
        </form>
    </figure>   
Noedel
  • 9
  • 2

1 Answers1

1

Here is the working example:

<?php
if (isset($_POST['submit'])){
    $ok = true;
    if (!isset($_POST['email']) || $_POST['email'] === ''){
        $ok = false;                    
    }
    else{
        $email = $_POST['email'];
    }
    if (!isset($_POST['password']) || $_POST['password'] === ''){
        $ok =false;
    }
    else{
        $password = $_POST['password'];
    }
    if ($ok){
        printf('Email: %s
        <br>Password: %s',
        htmlspecialchars($email),
        htmlspecialchars($password));


    }


}?>   <figure class="col-lg-6">
        <form method="post" action="#">
            <div class="row login">
                <div class="col-md-6">
                    <h3>Login</h3>
                    <div class="form-group">
                        <label for="exampleInputEmail">Email</label>
                        <input type="email" class="form-control" placeholder="Enter email*"  name="email" value="<?php echo isset($email) ? htmlspecialchars($email) : '';?>">
                    </div>
                </div>
            </div>
            <div class="row login">
                <div class="col-md-6">
                    <div class="form-group">
                        <label for="InputPassword">Password</label>
                        <input type="password" class="form-control" placeholder="Enter password*" name="password" value="<?php echo isset($password) ? htmlspecialchars($password) : '';?>">
                    </div>
                </div>
            </div>
            <input type="submit" class="btn tf-btn btn-default" name="submit"/>
        </form>
    </figure> 

In a nutshell: The <button type="submit"> does not submit the "submit" key in the $_POST array. Thus, it is always false. You need to use <input type="submit" name="submit"> to achieve that.

Chris-AT
  • 61
  • 2