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="[email protected]";
$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

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

// Getformdataandcreateemail
$Email2="[email protected]";
$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");
}

?>

// Getformdataandcreateemail
$Email2 = "[email protected]";
$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.

Archived

This topic is now archived and is closed to further replies.

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