Making form items required.

the mail() function

Moderators: macek, egami, gesf

Post Reply
seandisanti
php-forum Fan User
php-forum Fan User
Posts: 973
Joined: Mon Oct 01, 2012 12:32 pm

Re: Making form items required.

Post by seandisanti » Tue Apr 29, 2014 10:13 pm

if you're using html5, you can just set the fields to be required, or you can have a javascript function check all of the values when they try to submit, or you can add a loop to your form handler that checks the values when they come in like:

Code: Select all

if ($_POST){
$required_fields = array('name','email','blood_type');
foreach ($required_fields as $key){
if (empty($_POST[$key]) || !custom_validation_function_to_protect_me_from_hostile_user_input($_POST[$key])){
die($key . ' is not an optional field, or your answer is unacceptable.  Go back and try again');
}
}
 
not tested, but just thrown together. obviously replace field names etc with your own

seandisanti
php-forum Fan User
php-forum Fan User
Posts: 973
Joined: Mon Oct 01, 2012 12:32 pm

Re: Making form items required.

Post by seandisanti » Wed Apr 30, 2014 8:15 am

Don't have your form post to a mailto, have it post to a another page as a form handler. That page should then either generate the mail once info is validated, or fail prior to sending.

seandisanti
php-forum Fan User
php-forum Fan User
Posts: 973
Joined: Mon Oct 01, 2012 12:32 pm

Re: Making form items required.

Post by seandisanti » Wed Apr 30, 2014 8:18 am

and per your request, here is a great video by Jesse Boyer of jream.com giving a demonstration of passing info with post/get/ and request. It is a couple of years old so i can't remember his exact advice on using $_REQUEST, but no matter what he says about it, don't use it today. All $_REQUEST does is giving attackers an easy attack vector via the address bar. $_GET and $_POST variables are far from secure either, but $_REQUEST is just a security hole disguised as a blanket solution.

gauravkachhadiya

Re: Making form items required.

Post by gauravkachhadiya » Wed Apr 30, 2014 10:45 pm

you make required field using required keyword in input tag.The required keyword in HTML5.

Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests