2

I have been working on a form that uses PHP form validation. I also have a class in my CSS file that should be added to each of the invalid inputs. You can view a JSFiddle here. The class I am trying to add is only visible when the input is returned invalid. The class name is .invalid which you can find in the CSS. Thanks in advance, and any help is appreciated.

5
  • I'm not sure, but the browser may also be doing form validation via your html markup. If the form is not valid to the browser (required fields empty for example), the submit event will never reach your php backend because the browser detected an invalid form... and so those classes wont be added since php never received the request. Commented May 30, 2020 at 19:30
  • @mfink Thanks! So, should I just add the novalidate attribute to the form element? So, the HTML doesn't add its own validation? Commented May 30, 2020 at 19:31
  • I'm not quite familiar with the novalidate attribute, but give it a shot? perhaps remove any validation or required attributes from your form elements. Commented May 30, 2020 at 19:33
  • @mfink Thanks, again. But even after removing HTML validation, the form will be validated with PHP, with no problem. The only issue being that when an input is invalid, I would like to have that input get added a class of .invalid (this class is described in CSS of having dark red border and red background). That's why I was wondering if there was a way to add classes with PHP. Commented May 30, 2020 at 19:37
  • Please include all relevant code in the question itself. Commented May 30, 2020 at 19:49

2 Answers 2

2

You can have PHP add a class to your inputs if a field is invalid, but you may need to adjust your approach.

If you want to maintain using a form POST method (as you have it), you'd want to move your validation code to be processed on the same page that renders your view (the easiest way to do this is to have it within the same page). You can still use and include a separate file to pull in a PHP class or other system you want to use for validation if you want to keep your code neat. Here's an example:

<?php
// filter_input returns null or false if invalid.
$firstname = filter_input(INPUT_POST, 'firstname', FILTER_SANITIZE_STRING);
?>

<form method="POST" action="<?php echo getenv('REQUEST_URI'); ?>">
<input type="text" name="firstname" value="<?php echo $firstname; ?>" class="<?php echo $firstname ? '' : 'invalid'; ?>" />
<input type="submit" name="submit" value="Submit" />
</form>

However, I would recommend a more robust approach which would be to use an AJAX handler for submitting your form. This will give your users a better experience, and it also allows you to neatly have your form processing entirely separate from your view. That solution is a bit beyond the scope of this question, but ultimately you would have your PHP AJAX handler return information as to whether or not the submission was valid or what fields are invalid and have the javascript add your invalid class to the fields in error.

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

1 Comment

I recommend to all PHP devs to look into the filter_input function for capturing form values. Here is an article explaining a bit about it and form security in general. dev.to/lawrencejohnson/improving-form-and-ajax-security-4g29
0

Let's assume there are two inputs and as you want to validate via PHP the validation process will be after submitting the form as

HTML

<input type="text" title="First Name" aria-placeholder="First Name" id="name" required />
<input type="text" title="Last Name" aria-placeholder="Last Name" id="name" required />

PHP

for($i = 0;$i < sizeof($_POST);$i++){
if($_POST[$i] == null || $POST[$i] == ""){
echo "Please insert :".$_POST[$i];
 }
}

Note: PHP will validate the form after the form has been submitted for the validation before the submission you will need use javascript.

5 Comments

So does this add the class of .invalid into the invalid input, or does it just say "Please insert: ..."?
PHP cannot add the class for you for that you will need javascript because while handling forms PHP validates the form after the form has been submitted.
See as your question tells you need php to do this work but it can't because php is a server side language and javascript is a client side hence it only can be done by javascript.
If my answer helped can you please accept it as answer?
Thanks a lot glad to help you:)

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.