简体   繁体   中英

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 = 'you@yourdomain.com';
    $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
    }
?>

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

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
}

use the code like below:-

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

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM