0

My simple php form validation script:

$email      = (isset($_POST['email'])) ? $_POST['email'] : '' ;
$password       = (isset($_POST['password'])) ? $_POST['password'] : '' ;
$fname      = (isset($_POST['fname'])) ? $_POST['fname'] : '' ;
$lname      = (isset($_POST['lname'])) ? $_POST['lname'] : '' ;

if (!empty($email) && !empty($password) && !empty($fname) && !empty($lname)) {
    $auth->createAccount($email, $password, $fname, $lname);
}

For some reason it doesn't look very good to me.

How can I check each form field to indicate which one is the one that needs filling out and then at the end check to make sure all fields are filled out before calling the createAccount() function.

Note: the createAccount() function sanitizes the input so there is no need to do this here.

12
  • you can use jquery for this.. Commented Sep 3, 2015 at 1:28
  • 3
    I don't know why you're using a ternary then checking if they're empty. It defeats the purpose really. Commented Sep 3, 2015 at 1:30
  • 1
    Yes I can use jQuery but what if JavaScript is turned off? Commented Sep 3, 2015 at 1:31
  • how about using <noscript>? if js is disabled/doesn't support js put something in there to notify user that js is turned off? Commented Sep 3, 2015 at 1:34
  • why not put required in input and validate with jQuery, make users life easy when they filling the form Commented Sep 3, 2015 at 1:39

2 Answers 2

2

Try this:

<?php
$form_names = array(
    'email' => 'e-mail', 
    'password' => 'password',
    'fname' => 'firstname',
    'lname' => 'lastname'
); //So that the user understands the errors better
$errors = array();
foreach ($_POST as $field=>$val) {
    if (empty($val)) {
        array_push($errors, 'Please fill out ' . $form_names[$field]);
    }
}
if (count($errors) == 0) {
    //No errors call the submit PHP function
}
else {
    $string = implode('<br/>', $errors);
    echo '<p style="color:red;">' . $string . '</p>';
}
?>

However, the setback is that form names should be appropriate to echo out to the user in an intelligible way. As some may require spaces, you can create an array for form names connected to their actual and use that value to echo out to the user via. $good_name = $form_names[$field]

Sign up to request clarification or add additional context in comments.

5 Comments

No need to put in all the POST arrays for this ;-) that's what I also had in mind after seeing what the OP seems to want to achieve.
I like your simple solution. If you could include the array of form fields that you described as the setback it would make your answer even better.
I included an edit, the new array just makes the errors make more sense (for example fname vs. firstname). Also @Fred-ii- , sorry if I'm misunderstanding you, but what exactly do you mean by "all the POST arrays"?
@Valkyrie Meaning, that one doesn't have to to do $_POST['var1'] - $_POST['var2'] - $_POST['var3'] etc. the foreach takes care of that, as you wrote it.
Oh! Sorry I mis-read your comment. I thought you meant I was checking over redundant array values in $_POST. Sorry about that, and thanks!
2

Most common way to deal with this that I've seen is to create an array:

if (!$_POST['email']) {$errors[] = 'Email field is empty.';}
if (!$_POST['password']) {$errors[] = 'Password field is empty.';}
if (!$_POST['fname']) {$errors[] = 'First name field is empty.';}
if (!$_POST['lname']) {$errors[] = 'Last name field is empty.';}

And then loop through the errors to display them:

foreach ($errors as $error) {
    echo $error.'<br>';
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.