user1537415
user1537415

Reputation:

PHP Form validation not working properly

I have a registration script on my page, and it's handled like this:

<?php
include "inc/config.php";
        $firstname = $_POST['firstname'];
        $lastname = $_POST['lastname'];
        $email = $_POST['email'];
        $pass = $_POST['pass'];
        $conditions = $_POST['conditions'];
        if($firstname==""||$lastname==""||$email==""||$pass==""||$conditions==""){
        echo "
        <p>Tiedoissa oli puutteita :(</p>
            <ul>";
            if($firstname == ""){
                echo "<li>Etunimi puuttuu.</li>";
            }
            if($lastname == ""){
                echo "<li>Sukunimi puuttuu. </li>";
            }
            if($email == ""){
                echo "<li>Sähköposti puuttuu. </li>";
            }
            if($pass == ""){
                echo "<li>Salasana puuttuu. </li>";
            }
            if($conditions == ""){
                echo "<li>Luitko ehdot? </li>";
            }
        echo "</ul>";
        return false;
        }
        $rq = "INSERT INTO Users (Firstname,Lastname,Email,Password) VALUES ($firstname,$lastname,$email,$pass)";
        if(!mysqli_query($dblink,$rq)){
            echo "Rekisteröityminen epäonnistui tuntemattomasta syystä! :(";
        }
        else{
            echo "Rekisteröinti onnistui!<br>
            Käyttäjätunnuksesi on <strong>$email</strong> ja salasanasi on <strong>$pass</strong>.<br>
            Voit nyt kirjautua sisään. 
            ";
        }


?>

I'm having troubles finding the right method of comparing the form value, as I've tried ==,=== and NULL on comparison. But I'm always getting all of the error messages, or none of them. What I'm doing wrong?

Here's the html of the form:

<form class="form-horizontal span6" method="post" id="registrationform">
          <div class="control-group">
            <label class="control-label">Etunimi</label>
            <div class="controls">
              <input type="text" name="firstname">
            </div>
          </div>
          <div class="control-group">
            <label class="control-label">Sukunimi</label>
            <div class="controls">
              <input type="text" name="lastname">
            </div>
          </div>
          <div class="control-group">
            <label class="control-label">Sähköposti</label>
            <div class="controls">
              <input type="text" name="email">
            </div>
          </div>
          <div class="control-group">
            <label class="control-label">Salasana</label>
            <div class="controls">
              <input type="password" name="pass">
            </div>
          </div>
          <div class="control-group">
            <div class="controls">
              <label class="checkbox">
                <input type="checkbox" name="newsletter"> Tilaan uutiskirjeen
              </label>
              <label class="checkbox">
                <input type="checkbox" name="conditions"> Hyväksyn ehdot
              </label>
              <button type="submit" class="btn btn-success" id="registerbtn">Rekisteröidy</button>
            </div>
          </div>
        </form>

Upvotes: 0

Views: 279

Answers (2)

Hanky Panky
Hanky Panky

Reputation: 46900

Since your code is not in a function hence return false; is not working as you expect, and code continues to execute below. Your comparisons may already be fine. Change that return false; to die();

Ok so here is probably where the problem lies

if($firstname=="" || $lastname=="" || $email=="" || $pass=="" || $conditions==""){

You are checking for that and that check is working even when you dont submit the form and it appears like everything has an error. So you need to make that comparison only when the form is submitted and not while normal load. So wrap your php code around some isset like

if(isset($_POST["firstname"]) && isset($_POST["lastname"]))
{
 // all your php code
}

Other than that, your code is working fine. It compares like you expect it to.

Upvotes: 1

Zz Oussama
Zz Oussama

Reputation: 229

Hi you can use php fonction for example use if(!isset($firstname)) or if(empty($firstname)) if you want to now if $firstname is Null

Upvotes: 1

Related Questions