Asım Gündüz
Asım Gündüz

Reputation: 1297

Validating type password inputs with PHP

Hi I'm creating a register form and with PHP I want to check if the user inputs are valid. I can check email,username but I fail to check the password fields.

My PHP Code:

<?php 

if(isset($_POST['registerButton'])){



if(isset($_POST['regEmail'])){
        $regEmail = $_POST['regEmail'];
    }
    else{
        $regEmailError = 'Please Fill in Email field';
    }

    if(isset($_POST['regUsername'])){
        $regUsername = $_POST['regUsername'];
    }
    else{
        $regUsernameError = 'Please Fill in Username field';
    }

    if(($_POST['regPassword']) and ($_POST['regPassword2'])){


        if( isset($_POST['regPassword']) == isset($_POST['regPassword2']) ){
            $regPassword = $_POST['regPassword'];
        }
        else{
            $regPasswordError = 'Passwords does not match!';
        }
    }
    else{
        $regPasswordError = 'Please Fill in Password fields';
    }
}

?>

My HTML Code:

<input class="form-control " name="regEmail" type="email" placeholder="Email" required autofocus>

<input class="form-control " name="regUsername" type="text" placeholder="Username" required >

<input class="form-control " name"regPassword" type="password" placeholder="Password" required >

<input class="form-control " name="regPassword2" type="password" placeholder="Confirm Password" required>

What I have realised is that If I place an echo "Success"; before the following line I can see the print but if I place the echo "Success; after the following line I can't see the output so I'm doing something wrong here and can not figure it out, everything seems fine to me. Any help is very appreciated.

if(($_POST['regPassword']) and ($_POST['regPassword2'])){

EDIT: IF I replace the complete if statement that is checking the password fields to:

if(isset($_POST['regPassword'])){
echo "Hello World";
}

it still does not display anything..

EDIT 2: I just solved the issue, the problem is that I'm missing the "=" in the HTML code after the name value

Upvotes: 1

Views: 90

Answers (2)

Philipp
Philipp

Reputation: 15639

You currently compare two isset values, which are always true, because they were true in the condition before. Just remove the isset statement to compare the actual values

if(($_POST['regPassword']) and ($_POST['regPassword2'])){
    if($_POST['regPassword'] == $_POST['regPassword2']){
        $regPassword = $_POST['regPassword'];
    }
    else{
        $regPasswordError = 'Passwords does not match!';
    }
}
else{
    $regPasswordError = 'Please Fill in Password fields';
}

you should also think about a helper function which takes away complexity

function httppost($name) {
   return isset($_POST[$name]) ? trim($_POST[$name]) : false;
}

this way you could just use

$regPassword = httppost('regPassword');
$regPassword2 = httppost('regPassword2');
if($regPassword && $regPassword2){
    if($regPassword != $regPassword2){
        $regPasswordError = 'Passwords does not match!';
        $regPassword = false;
    }
} else {
    $regPasswordError = 'Please Fill in Password fields';
}

Upvotes: 0

Kostas Mitsarakis
Kostas Mitsarakis

Reputation: 4747

Change to:

if (isset($_POST['regPassword']) && isset($_POST['regPassword2'])) {
    if ($_POST['regPassword'] == $_POST['regPassword2'] ){
        $regPassword = $_POST['regPassword'];
    } else {
        $regPasswordError = 'Passwords does not match!';
    }
}

Upvotes: 1

Related Questions