Jump to content

Form Valildation - Empty Fields


a1ias

Recommended Posts

Well, after 4 years away from it, I've decided to rekindle my relationship with PHP, and it appears there have been some changes...

 

Anyway, starting from the ground up with a simple login script, and beginning with the submission of a login form and the validation thereof, I have come across my first 'this used to work I'm sure of it'....so here goes my snippet

 

if (isset($_POST['Login'])) { // Has the button been pressed, still seems straightforward

    if (!isset($_POST['username']) || !isset($_POST['password'])) die('Found blank fields']); // Uh oh! This no longer works, PHP skips straight over it.

    if (!$_POST['username']) | (!$_POST['password']) die('Found blank fields'); // And nor does this.

    // I CAN get this to work but for some reason it doesn't feel like the best solution.
    if ($_POST['username'] == "") || ($_POST['password'] == "") die('Found blank fields');

 

 

So, just to save my sanity, the top 2 form field checks did used to work didn't they? And is the 3rd option the best way to vaildate field entry?

 

I really need to buy myself an up-to-date PHP/mySQL book so if anyone can recommend one I would be most grateful.

 

Many thanks in advance..

 

a1ias

Link to comment
https://forums.phpfreaks.com/topic/199693-form-valildation-empty-fields/
Share on other sites

Maybe they're actually set. print_r($_POST)

 

You'd be better off to use if( empty($POST['username']) || empty( $_POST['password']) ) or even check that strlen($_POST['whatever']) is at least as long as your minimum requirement.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.