PHP Questions Home
Categories
Arrays
Files
Forms
Creating PDFs
PHP Errors
Functions
Images
MySQL
Numbers
Others
Strings
Website

PHP Forums
Forums Index
MySQL Forum
PHP Forum
Flash/Actionscript
Javascript
Web design

More PHP
PHP Functions
Top Questions
Ask a Question
PHP Interview Questions
PHP Jobs
Ecommerce
IT Questions
Business Websites

Sponsors

sponsor ad
My Salary
PHP Developer Salary
Advertise Here

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!"; }
?>

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?

Can I use a variable from a .php file to give a value to an HTML FormMail form field thus: ? Thank you

This may seem simple but I am just starting out. After the action of my submit button on my html page sends the user and the information to my php page how do I get the user back to the page with the form without having to get the user to have to click o

How do I remove HTML tags from a string?

How do I remove escape characters from data?



Custom Search