Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => Mail

Making form items required.

the mail() function

Moderators: macek, egami, gesf

Making form items required.

Postby bbowman1170 » Mon Apr 28, 2014 7:09 pm

I am trying to make form items required for a registration form. The basic code works fine. It sends the registration email, but how do I make it fail to send mail if fields are left blank? I am clearly VERY new to php. Please assist.

<?php
$field_name = $_POST['cf_name'];
$field_age = $_POST['cf_age'];
$field_phone = $_POST['cf_phone'];
$field_address = $_POST['cf_address'];
$field_city = $_POST['cf_city'];
$field_state = $_POST['cf_state'];
$field_zip = $_POST['cf_zip'];
$field_grade = $_POST['cf_grade'];
$field_ename = $_POST['cf_ename'];
$field_ephone = $_POST['cf_ephone'];

$mail_to = 'bbowman1170@gmail.com';

$subject = 'Message from a site visitor '.$field_name;

$body_message = 'From: '.$field_name."\n";
$body_message .= 'Age: '.$field_age."\n";
$body_message .= 'Grade: '.$field_grade."\n";
$body_message .= 'Phone: '.$field_phone."\n";
$body_message .= 'Address: '.$field_address."\n";
$body_message .= 'City: '.$field_city."\n";
$body_message .= 'State: '.$field_state."\n";
$body_message .= 'ZIP: '.$field_zip."\n";
$body_message .= 'Emergency Contact: '.$field_ename."\n";
$body_message .= 'Emergency Phone Number: '.$field_ephone;

$headers = 'From: '.$field_name."\r\n";
$headers .= 'Reply-To: '.$field_."\r\n";

$mail_status = mail($mail_to , $subject, $body_message, $headers);
$mail_error = 'null';

if(empty($field_name) || empty($field_phone) || empty($field_age)) { echo $mail_error; ?>

<script language="javascript" type="text/javascript">
alert('Submition failed. Please make sure you have filled out all categories. If you did this, and see this message, send an email to bbowman1170@gmail.com.');
window.location = 'testreg.html';
</script>

<?php
if ($mail_status) { ?>
<script language="javascript" type="text/javascript">
alert('Thank you for the message. We will contact you shortly.');
window.location = 'testreg.html';
</script>
<?php
}
else { ?>
<script language="javascript" type="text/javascript">
alert('Message failed. Please, send an email to bbowman1170@gmail.com');
window.location = 'testreg.html';
</script>
<?php }
} ?>
bbowman1170
New php-forum User
New php-forum User
 
Posts: 4
Joined: Mon Apr 28, 2014 7:04 pm

Re: Making form items required.

Postby 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: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: Making form items required.

Postby bbowman1170 » Wed Apr 30, 2014 5:12 am

Neither solution worked. My main goal is to have the script not send me a blank form. :? Are there any good resources that explain basic php attributes? Thanks so much for your help.
Last edited by bbowman1170 on Wed Apr 30, 2014 5:26 am, edited 1 time in total.
bbowman1170
New php-forum User
New php-forum User
 
Posts: 4
Joined: Mon Apr 28, 2014 7:04 pm

Re: Making form items required.

Postby bbowman1170 » Wed Apr 30, 2014 5:26 am

Ok so to clarify. The code functions fine and does distinguish between the required fields being complete or not. This is evident because of 2 different submit messages(using a javascript popup). The only frustration is that is sends an email No Matter What! How do I get it to fail to send if there is 1 or more missing fields?
bbowman1170
New php-forum User
New php-forum User
 
Posts: 4
Joined: Mon Apr 28, 2014 7:04 pm

Re: Making form items required.

Postby 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: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: Making form items required.

Postby 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.
seandisanti
php-forum Fan User
php-forum Fan User
 
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: Making form items required.

Postby gauravkachhadiya » Wed Apr 30, 2014 10:45 pm

you make required field using required keyword in input tag.The required keyword in HTML5.
gauravkachhadiya
New php-forum User
New php-forum User
 
Posts: 5
Joined: Wed Apr 23, 2014 2:37 am


Return to PHP coding => Mail

Who is online

Users browsing this forum: Yahoo [Bot] and 2 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.