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

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 can I access information sent through POST on a form?

I have a form that I need to do two things: send all the input values to a confirmation/pay page, and send to me via email. Payment is based on what the customer picks (naturally). The way I have it set up is the form action goes to the confirmat

how to display like table from database records?

hello I found this code somewhere and tried to create online dictionary with it. I managed to insert the code in to my template index.php. But after the user click submit button the result shows in other blank self created page something like index.php?a

i just wanted to put a textbox beside the query of the evaluation form. how can i do that? ========================== here is my php form index.php PERFORMANCE APPRAISAL INSTRUMENT var Event = { add: function(obj,type,fn) {



Custom Search