How do I make a website field mandatory?
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!"; }
?>
Comments on this Question/Answer
Can anyone please tell me the script I need to make the "name/nickname" field mandatory?By: Sylvia Lisi, 06 Aug 2009 12:04:49
Reply to comments / post your own comment >>>
ASK A QUESTION
More forms PHP Questions
How do I email the contents of a form to me?I am having a form. Here, i am submitting the form using java script as its necessary. If the form method is get, i am able to get in redirection page and can get the hidden values in the form. But if the form method is post, i am not able to get those hi
How do you check for an empty field in a form?
How do I check for an empty field in a form?
how do i validate the name into form ?
