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
Hello: I receive two error messages when I try to submit a form. The messages are: Warning: mysqli_query() [function.mysqli-query]: Empty query in /home/content/PRIVATE/html/riskyjobs_riskybusiness_Database_TableSetup_riskyjobs_Table_Chapter9_Page5DYnamic Dropbox when I select a value it has to add a textbox to a specific position in the form how do I do it?? If I could change the dropbox in a textbox and make it add Textboxes depending on the number typed it would be even better but dont think th
How can I check a value entered in a field on my form is a number?
if i send values to database ,but in that table zeros is added ,how can i rectify the error
What are magic quotes?
