Jump to content

how to validate contact form fields? (name, email and message)


foevah

Recommended Posts

Hi everyone I was just wondering how to make my contact form recognize if the user has actually entered any information into the fields. Right now if the user just clicks submit then an email will be sent to me without them having to enter any info into the fields.. Please can someone tell me how to prevent this? I would also like to know how I can filter the message field from letting the user enter in profanity?

 

This is the HTML for my contact form:

<form method="post" action="contact.php"><p class="class2"> your name:<br />
   <input type="text" name="name" size="19"><br />your email:<br />
   <input type="text" name="email" size="19"><br />subject:<br />
   <textarea rows="5" name="message" cols="30"></textarea>
<br />
   <input type="submit" value="submit" name="submit">
</form>

 

This is the PHP for my contact form:

<?php
if(isset($_POST['submit'])) {

$to = "jecgard@gmail.com";
$subject = "Form Tutorial";
$name_field = $_POST['name'];
$email_field = $_POST['email'];
$message = $_POST['message'];

$body = "From: $name_field\n E-Mail: $email_field\n Message:\n $message";

echo "Data has been submitted to $to!";
mail($to, $subject, $body);

} else {

}
?>

Link to comment
Share on other sites

my question is:

 

I want to make my contact form recognize if the user hasn't enter in all the fields. I want my form to say next to the fields that haven't got the correct info in them to say something like (after submission) "you have forgotten to enter your name.." or if they use a swear word in the message field I want it to say "no swearing allowed!"

 

any ideas? can you show me some example code please

Link to comment
Share on other sites

here is a simple error i use on mine.

<?php

if($_POST[variable]==""){ echo $error[variable]="Please post a variable here"; } else {unset ($error[variable]);}

if ($error){ ?>



HTML CODE

<?php  } else { more php code } ?>

 

then if you want to echo out the errors if the form comes up empty find the input for the post variable and after it put

<?php echo $error[variable]; ?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.