Jump to content

[SOLVED] Can this exsiting code be modified to check fields have been filled in


leet8845

Recommended Posts

Hi there,

 

I sometimes get blank emails from contact form on my site. It happens when users click submit by mistake. My exsiting code doesnt check the fields have any content before sending the form.

 

I need to add some code to check that fields have been filled out on my form.

 

Does anyone know if its possible to add anything to exsiting code below to do this.

 

I'm mainly bothered that the fields just have some text, nothing more complex really.

 

Here's my code:

<?php

// Getformdataandcreateemail
$Email2="mail@mysite.co.uk";
$email=$_POST['Email']; 
$name=stripslashes($_POST['Name']);
$subject=stripslashes($_POST['Subject']);
$messagecont=stripslashes($_POST['Message']);
$message=
<<<EOD
--------------------------------
Enquiry from your website
--------------------------------

Name: $name
Subject: $subject
EmailAddress: $email
Message: $messagecont

--------------------------------
End of Message
--------------------------------
EOD;

//Sendemail
@mail($Email2,$subject,$message, "From:$Email2");
header("Location:thankyou.html");


?>

 

any help truly appreciated

Link to comment
Share on other sites

<?php
// Check email is not empty
if (! empty ($_POST['Email']) )
{

// Getformdataandcreateemail
$Email2="mail@mysite.co.uk";
$email=$_POST['Email']; 
$name=stripslashes($_POST['Name']);
$subject=stripslashes($_POST['Subject']);
$messagecont=stripslashes($_POST['Message']);
$message=
<<<EOD
--------------------------------
Enquiry from your website
--------------------------------

Name: $name
Subject: $subject
EmailAddress: $email
Message: $messagecont

--------------------------------
End of Message
--------------------------------
EOD;

//Sendemail
@mail($Email2,$subject,$message, "From:$Email2");
header("Location:thankyou.html");

}
else
{
    // No email was sent
    header("Location:form.html");
}

?>

Link to comment
Share on other sites

// Getformdataandcreateemail
$Email2 = "mail@mysite.co.uk";
$email = (isset($_POST['Email'])) ? $_POST['Email'] : die("Please enter a valid Email address");
$name = (isset($_POST['Name'])) ? stripslashes($_POST['Name']) : die("Please enter a \"From\" name");
$subject = (isset($_POST['Subject'])) ? stripslashes($_POST['Subject']) : die("Please enter a Subject for your message");
$messagecont = (isset($_POST['Message'])) ? stripslashes($_POST['Message']) : die("Please enter a message in the textbox");

 

Should help you out there.

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.