Adding validation to contact.php

Discussion in 'Software' started by RichardH, Mar 25, 2013.

  1. RichardH

    RichardH Private E-2

    Hi
    I have created a very simple "Contact Us" php form on my website which is working fine.
    However I am now getting quite a lot of empty/spam forms coming back, and would like to add simple validation to ensure that the data fields contain data!
    Here is my contact.php form at the moment:
    Code:
    <?php
    $field_name = $_POST['name'];
    $field_subject = $_POST['subject'];
    $field_email = $_POST['email'];
    $field_message = $_POST['message'];
    
    $mail_to = 'richard@richardhorsfield.com';
    $subject = 'Website Enquiry from '.$field_name;
    
    $body_message = 'Subject: '.$field_subject."\n";
    $body_message = 'Name: '.$field_name."\n";
    $body_message .= 'E-mail: '.$field_email."\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 = 'index.html';
    	</script>
    <?php
    }
    else { ?>
    	<script language="javascript" type="text/javascript">
    		alert('Message failed. Please, send an email to richard@richardhorsfield.com');
    		window.location = 'contact.html';
    	</script>
    <?php
    }
    ?>
    All I want to do is make all the fields mandatory, so that if they are not completed a message comes up to say "Please enter your email" or something.
     

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds