Jeremy Blazé
Jeremy Blazé

Reputation: 155

Required alert on PHP contact form

I've been looking around for ages and haven't found an easy way of editing this code so that when the user hasn't typed anything into a required field (all of the fields are required) an alert is shown asking the user to please enter something.

Can anyone help?

<?php
    $field_name = $_POST['name'];
    $field_email = $_POST['email'];
    $field_message = $_POST['message'];

    $mail_to = '[email protected]';
    $subject = 'Site Mail';

    $body_message = 'From: '.$field_name."\n";
    $body_message .= 'Email: '.$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('Thanks! Your email has been sent.');
            window.location = 'index.html';
        </script>
    <?php
    }
    else { ?>
        <script language="javascript" type="text/javascript">
            alert('Sorry, something went wrong.');
            window.location = 'index.html';
        </script>
    <?php
    }
?>

Upvotes: 0

Views: 1236

Answers (4)

Tom
Tom

Reputation: 34376

The most common way to check for required fields is both on the client side (to inform the user) and the server side (to check the data is legit before we send it).

On the client side, at the point at which the form is submitted we should check to see if the fields are correct and ask the user to fix mistakes. There are a bunch of great helper libraries available to do this, a couple of the more common libraries are http://parsleyjs.org/ and http://jqueryvalidation.org/ both use jQuery and have easy examples to get started.

On the server side, as some commenters have noted, your current script is vulnerable to header injection and other nasty stuff. You might want to switch to using a library such as swiftmailer which will prevent a lot of bad stuff happening. Here is a really simple example of how to send email http://swiftmailer.org/docs/sending.html

To add server side validation to you example, as simple approach would be to check the values of $_POST['name']; before calling $mailer->send($message);

Upvotes: 0

Ripa Saha
Ripa Saha

Reputation: 2540

use the code like below:-

$('.required').each(function(){
    if($(this).val()=="")
    {
         alert("error");
    }
});

Upvotes: 0

ygesher
ygesher

Reputation: 1161

PHP

<?php
$fields = array([0]=>$field_name,[1]=>$field_email,[2]=>$field_message);
foreach($fields as $field){
    if !$field{
        echo "Please enter a value.";
    }
}
?>

Alternatively, you could use javascript validation, that would look something like this:

<script>
function formValid(){
  var valid = true;
  var fields = getElementByTag(input);
  for (var i;i>=fields.length;i++){
    if (!fields[i]){
      getElementById('warning').style.display='inline'; // assuming the error message element is called 'warning'
      getElementById('valid').value=1; // assuming we have an input that will indicate to the php code whether the form is validated  
    }
  }
}
</script>

for this script we would then add to the beginning of the form-action php block:

if(isset($_POST['submit'])&&$_POST['valid']!=1){
// form action code goes here
}

Upvotes: 0

Joe
Joe

Reputation: 8292

You can use required attribute if you want to take advantage of html5 features.

Try this LINK. It shows a simple demo of how to use the required attribute. This one too

Upvotes: 1

Related Questions