PHP Questions Home

Categories

Arrays
Files
Forms
Functions
Images
MySQL
Numbers
Others
Strings
Website


PHP Functions

PHP Functions


More PHP

Top Questions
Ask a Question

How do I make a website field mandatory?


There is no way within standard HTML code to make a field mandatory because it does no evaluation. For this you need JavaScript or, more robustly, PHP.

First, it is a good idea in your HTML to say which fields are mandatory or not, for instance by marking them with a '*' by them.

Then you do the check in your PHP code after the form has been submitted. So let's say we have a field called 'mandatory' and the form was submitted by post, here is how we can check for a value:

<?php
$anyvalue 
$_POST[mandatory];
if(!
$anyvalue) { echo "No value - please go back and fill in"; }
//don't use if you want '0' to be a valid value

//a better test for the variable to have some contents

$length strlen($anyvalue);
if(!
$length) { echo "No value - please go back and fill in!"; }
?>



More forms PHP Questions

How do I know if magic quotes is switched on?
How do I check that a valid email address is entered?
What are magic quotes?
How can I access information sent through POST on a form?
How do I remove HTML tags from a string?