Jump to content

Required fields in contact form


chartkor

Recommended Posts

Hi, I am completely new to php

I have a following php script running contact form for me

<?php
$field_name = $_POST['cf_name'];
$field_email = $_POST['cf_email'];
$field_postalcode = $_POST['cf_postalcode'];
$field_telephone = $_POST['cf_telephone'];
$field_size = $_POST['cf_size'];
$field_bedrooms = $_POST['cf_bedrooms'];
$field_bathrooms = $_POST['cf_bathrooms'];
$field_startdate = $_POST['cf_startdate'];
$field_cats = $_POST['cf_cats'];
$field_dogs = $_POST['cf_dogs'];
$field_starttime = $_POST['cf_start'];
$field_estimate = $_POST['cf_estimate'];
$field_service = $_POST['cf_service'];
$field_frequency = $_POST['cf_frequency'];
$field_message = $_POST['cf_message'];


$mail_to = '[email protected]';
$subject = 'Customer '.$field_name;

$body_message .= 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Postal Code: '.$field_postalcode."\n";
$body_message .= 'Telephone: '.$field_telephone."\n";  
$body_message .= 'Size:' .$field_size."\n";
$body_message .= 'Bedrooms:' .$field_bedrooms."\n";
$body_message .= 'Bathrooms:' .$field_bathrooms."\n";
$body_message .= 'Start date:' .$field_startdate."\n";
$body_message .= 'Cats:' .$field_cats."\n";
$body_message .= 'Dogs:' .$field_dogs."\n";
$body_message .= 'Start time:' .$field_starttime."\n";
$body_message .= 'Estimate:' .$field_estimate."\n";
$body_message .= 'Service Type: '.$field_service."\n"; 
$body_message .= 'Cleaning Frequency: '.$field_frequency."\n"; 
$body_message .= 'Message: '.$field_message;

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

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

if ($mail_status) { ?>
	<script language="javascript" type="text/javascript">
		alert('Thank you for the message. We will contact you shortly.');
		window.location = 'bookonline.html';
	</script>
<?php
}
else { ?>
	<script language="javascript" type="text/javascript">
		alert('Message failed. Please, send an email to [email protected]');
		window.location = 'bookonline.html';
	</script>
<?php
}
?>

Now, I'd like to make some of the fields mandatory, what should I do? Is that going to be seperate script, or just addon to the existing one?

Thanks in advance

Link to comment
https://forums.phpfreaks.com/topic/279052-required-fields-in-contact-form/
Share on other sites

It doesn't look like the quotes are correct, are they?  If not, then swap single, and double quotes.

 

The quotes look fine.

 

To make some of the form fields required, you can run a series of if tests on the information. If the name is required, for example, check that the corresponding variable contains a value. If everything looks good, the e-mail can be sent. Otherwise, send them back to the form...or display an error message.

 

Note that there are many tutorials online for making fields required.

https://www.google.com/search?q=php+make+form+fields+required

 

Try a few things out and if you get stuck, feel free to post again.  :happy-04:

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.