Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I've got a PHP form that executes on the contact form's page. I've got it working well - but it currently only checks to make sure name and email are entered. I'm trying to get it to check for a message too, but my attempts just cos the page not to load after the php. Here's what I have:
<?php
$to = "me#gmail.com" ;
$from = $_REQUEST['Email'] ;
$name = $_REQUEST['Name'] ;
$headers = "From: $from";
$subject = "Web Contact Data";
$startmonth = $_REQUEST['StartMonth'];
$startyear = $_REQUEST['StartYear'];
$endmonth = $_REQUEST['EndMonth'];
$endyear = $_REQUEST['EndYear'];
$message = $_REQUEST['Message'];
$fields = array();
$fields{"Name"} = "Name";
$fields{"Email"} = "Email";
$fields{"Phone"} = "Phone";
$selectedProjects = 'None';
if(isset($_POST['projects']) && is_array($_POST['projects']) && count($_POST['projects']) > 0){
$selectedProjects = implode(', ', $_POST['projects']);
}
$selectedSkills = 'None';
if(isset($_POST['skills']) && is_array($_POST['skills']) && count($_POST['skills']) > 0){
$selectedSkills = implode(', ', $_POST['skills']);
}
$selectedNoRush = 'None';
if(isset($_POST['norush']) && is_array($_POST['norush']) && count($_POST['norush']) > 0){
$NoRush= implode(', ', $_POST['norush']);
}
$selectedWhenReady = 'None';
if(isset($_POST['whenready']) && is_array($_POST['whenready']) && count($_POST['whenready']) > 0){
$WhenReady= implode(', ', $_POST['whenready']);
}
$selectedBudget = 'None';
if(isset($_POST['budget']) && is_array($_POST['budget']) && count($_POST['budget']) > 0){
$selectedBudget= implode(', ', $_POST['budget']);
}
$body = "We have received the following information:\n\n"; foreach($fields as $a => $b){$body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]);}
$body .= "\n" . 'Selected Projects: ' . $selectedProjects . "\n";
$body .= 'Selected Skills: ' . $selectedSkills . "\n\n";
$body .= 'Start Date: ' . $startmonth . " " . $startyear . " " . $NoRush . "\n";
$body .= 'End Date: ' . $endmonth . " " . $endyear . " " . $WhenReady . "\n";
$body .= 'Budget: ' . $selectedBudget . "\n\n";
$body .= 'Message:' . $message . "\n";
$headers2 = "From: me#gmail.com";
$subject2 = "Thank you for contacting us";
$autoreply = "Thank you for contacting us. Somebody will get back to you as soon as possible, usually within 48 hours.";
if($from == '') {print "You have not entered an email, please go back and try again";}
else {
if($name == '') {print "You have not entered a name, please go back and try again";}
else {
$send = mail($to, $subject, $body, $headers);
$send2 = mail($from, $subject2, $autoreply, $headers2);
if($send)
{print "Thank you. Your request has been successfully submitted.";}
else
{print "We encountered an error sending your mail, please check your details are correct or email us at hello#lyonempire.co.uk"; }
}
}
?>
So the above all works fine, but when I add the following code after the name/email checks, it breaks:
if($message == '') {print "You have not entered a message, please go back and try again";}
else {
What am I doing wrong?
Thanks! MC
if($name == '')
{
print "You have not entered a name, please go back and try again";
}
else if($message == '') {
// do what ever you want
}
else {
$send = mail($to, $subject, $body, $headers);
$send2 = mail($from, $subject2, $autoreply, $headers2);
.... Rest goes here
i cant tell from the code you've shown, but have you closed the curly bracket on your else statement?
if($message == '') {print "You have not entered a message, please go back and try again";}
else {} <----
Related
Im trying to send an email to 2 variables currently I have tried to add 3 emails to an array and put that into a variable and get the other email from the form, I then dont know how to put both of those variables together when sending so they each have their own "to" but this does not work or something doesnt and I dont know what?? And yes I have real emails that I use these are placeholders for this!!!
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (isset($_POST['submit'])) {
$email = $_POST['LOGSE'];
$email2 = array("test#test.com", "test#test.com", "test#test.com");
$name = $_POST['full_name'];
$eventtitle = $_POST['EventT'];
$InCharge = $_POST['InCharge'];
$Venue = $_POST['Venue'];
$VenY = $_POST['VenR'];
if($VenY != "Yes"){
$ava = " Have not checked if";
}
else{
$ava = "";
}
$dates = $_POST['dateS'];
$datee = $_POST['dateE'];
$adults = $_POST['Adults'];
$children = $_POST['Children'];
$catreq = $_POST['CateReq'];
if (catreq != ''){
$catreq = $catreq;
}
else{
$catreq = "No Catering Needed";
}
$logreq = $_POST['LogReq'];
if (logreq != ''){
$logreq = $logreq;
}
else{
$logreq = "No Logistic Equipment Needed";
}
$itreq = $_POST['ITReq'];
if (itreq != ''){
$itreq = $itreq;
}
else{
$itreq = "No IT Needed";
}
$tran = $_POST['TransR'];
if($tran != Yes){
$tran = "NO ";
}
else{
$tran = "";
}
$Risk = $_POST['RiskR'];
if($Risk != Yes){
$Risk = "NO ";
}
else{
$Risk = "";
}
$othern = $_POST['OtherN'];
// The Email:
$from = 'test#test.com';
$to = $email;
$to = $email2;
$subject = 'Event Form ' .$eventtitle;
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type:text/html;charset=UTF-8' . "\r\n";
$headers .= 'From: Event Form<test#test.com>' . "\r\n";
$headers .= 'Cc: test#test.com, test#test.com' . "\r\n";
$body = '<html>
<head>
<title>Event</title>
<style>
h1 {
font-family: "Century Gothic", CenturyGothic,
AppleGothic, sans-serif;
}
h3 {
font-family: "Century Gothic", CenturyGothic,
AppleGothic, sans-serif;
}
</style>
</head>
<body>
<h1>Event Details</h1>
<h3>Name: '.$name.'</h3>
<h3>Event Title: '.$eventtitle.'</h3>
<h3>Event Manager: '.$InCharge.'</h3>
<h3>Venue: '.$Venue.' - '.$ava.' Available</h3>
<h3>Date Start: '.$dates.'</h3>
<h3>Date End: '.$datee.'</h3>
<h3>Adults Attending: '.$adults.' Children Attending:
'.$children.'</h3>
<h3>Catering Requirements: '.$catreq.'</h3>
<h3>Logistic Requirements/Equipment: '.$logreq.'</h3>
<h3>IT Requirements: '.$itreq.'</h3>
<h3>Other Notes: '.$othern.'</h3>
<h3><font color="red">'.$tran.'</font>Transport Has Been
Booked</h3>
</body>
</html>' . "\r\n";
mail( $to, $subject, $body ,$headers );
/* echo "An Email Has Been Sent<br>Thank You."; */
header('Location: ./thanks.html');
}
}
The $to in mail function should be a string. If there are more than one email address then those should be comma separated. In your case you might need to do like following.
$to = $email . ','. implode(',', $email2);
Add email address with the comma
$email_to = "test#test.com,some#other.com,yet#another.net";
Also you can add
$headers = "Bcc: someone#domain.com";
Use a foreach on the array:
foreach($email2 as $to){
mail( $to, $subject, $body ,$headers );
}
Or like others are suggesting:
$to = $email.",".implode(",",$email2);
That doesn't make sense:
$to = $email;
$to = $email2;
Parmaeter $to should be a string of comma-seperated email-addresses, e.g.:
$to = $email1 . ", " . $email2 . ", " . $email3;
I'm building an ajax/php contact form for my project with the following fields:
Name (required), Email (required), Subject (Not required) and Website (Not required)
Everything's working fine the only problem now is if the user doesn't type anything in Subject and or Website fields the email I receive shows those 2 fields like so:
Subject: (shows blank)
Website: (shows blank)
Is it possible not to show those 2 fields at all if the user didn't type anything so on the email I receive I only get:
Name: [user name]
Email: [user email address]
I'm just posting the PHP code as I believe it's something to do with PHP only and not the Ajax script:
<?php
$errorMSG = "";
// NAME
if (empty($_POST["name"])) {
$errorMSG = "Name is required ";
} else {
$name = $_POST["name"];
}
// EMAIL
if (empty($_POST["email"])) {
$errorMSG .= "Email is required ";
} else {
$email = $_POST["email"];
}
// SUBJECT
$subject = $_POST["subject"];
// WEBSITE
$website = $_POST["website"];
// MESSAGE
if (empty($_POST["message"])) {
$errorMSG .= "Message is required ";
} else {
$message = $_POST["message"];
}
$EmailTo = "email#myemail.com";
$Subject = "New Message Received";
// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $name;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $email;
$Body .= "\n";
$Body .= "Website: ";
$Body .= $website;
$Body .= "\n";
$Body .= "Subject: ";
$Body .= $subject;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $message;
$Body .= "\n";
// send email
$success = mail($EmailTo, $Subject, $Body, "From:".$email);
// redirect to success page
if ($success && $errorMSG == ""){
echo "success";
}else{
if($errorMSG == ""){
echo "Something went wrong :(";
} else {
echo $errorMSG;
}
}
?>
Thank you all!
You can check for conditions with an if statement. Something like this:
if (!empty($subject)) {
$Body .= "Subject: ";
$Body .= $subject;
$Body .= "\n";
}
You don't use formData, so disabling the field won't work.
If you want to get rid of empty variables, build the data according to a condition like this
function submitForm() {
var data = {
name: $("#name").val(),
email: $("#email").val()
}
if($("#subject").val() !== "") {
data.subject = $("#subject").val()
}
if($("#website").val() !== "") {
data.website = $("#website").val()
}
$.ajax({
type: "POST",
url: "php/form-process.php",
data: data,
success: function(text) {
if (text == "success") {
formSuccess();
} else {
formError();
submitMSG(false, text);
}
}
});
}
In my contact form i recently added a selector ( http://shopzuinig.nl/contact.html ) and styled it the way i wanted, but when i fill in the form and press send, the choice for a location is not included in the e-mail i receive. Can someone provide me with the PHP code to make this happen?
Here is my current PHP code:
<?php
error_reporting (E_ALL ^ E_NOTICE);
$post = (!empty($_POST)) ? true : false;
$replyto='restaurant#dellitalia.nl';
$subject = 'Verzoek via de website';
if($post)
{
function ValidateEmail($email)
{
$regex = "/([a-z0-9_\.\-]+)". # name
"#". # at
"([a-z0-9\.\-]+){2,255}". # domain & possibly subdomains
"\.". # period
"([a-z]+){2,10}/i"; # domain extension
$eregi = preg_replace($regex, '', $email);
return empty($eregi) ? true : false;
}
$name = stripslashes($_POST['name']);
$email = trim($_POST['email']);
$message = stripslashes($_POST['message']);
$phone = stripslashes($_POST['phone']);
$answer = trim($_POST['answer']);
$verificationanswer="6"; // plz change edit your human answer
$from=$email;
$to=$replyto;
$error = '';
$headers= "From: $name <" . $email . "> \n";
$headers.= "Reply-to:" . $email . "\n";
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers = "Content-Type: text/html; charset=utf-8\n".$headers;
// Checks Name Field
if(!$name || !$email || $email && !ValidateEmail($email) || $answer <> $verificationanswer || !$message || strlen($message) < 1)
{
$error .= 'De velden zijn niet correct ingevuld.<br />';
}
if(!$error)
{
$messages.="Name: $name <br>";
$messages.="Email: $email <br>";
$messages.="Message: $message <br>";
$mail = mail($to,$subject,$messages,$headers);
if($mail)
{
echo 'OK';
if($autorespond == "yes")
{
include("autoresponde.php");
}
}
}
else
{
echo '<div class="error">'.$error.'</div>';
}
}
?>
Location Missing in your message. Include location to get location details in your mail.
if(!$error)
{
$mydropdown=$_POST['mydropdown'];
$mydropdown=mysql_real_escape_string($mydropdown);
$messages.="Name: $name <br>";
$messages.="Email: $email <br>";
$messages.="Message: $message <br>";
$messages.="Location: $mydropdown<br>"; // Missing. Include Location Here
$mail = mail($to,$subject,$messages,$headers);
if($mail)
{
echo 'OK';
if($autorespond == "yes")
{
include("autoresponde.php");
}
}
}
I am having a strange problem with this form. I have made it yesterday night and it was working fine, sending all the emails as it should. However, I've run it today and it won't simply work at all. I am always getting the error message. Any clues? Thank you.
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$depart = $_POST['departamento'];
$headers = "From: $email\r\n";
$headers .= "Reply-To: $email\r\n";
$corpo = "Nova Mensagem\n";
$corpo .= "De: " . $name . "\n";
$corpo .= "Email: " . $email . "\n";
$corpo.=" Para o departamento " . $depart. "\n";
$corpo .= "Mensagem: " . $message . "\n";
if ($depart = administrativo)
{
$email_to = '';
}
elseif ($depart = financeiro)
{
$email_to = '';
}
elseif ($depart = Suporte)
{
$email_to = '';
}
else
{
$email_to = '';
}
$status = mail($email_to, $subject, $corpo, $headers);
if($status) {
echo "<script> window.location.href = ''; </script>";
}
else {
echo "<script> window.location.href = ''; </script>";
}
?>
Instead of = use == for comparison
for example - instead of:
if( $depart = administrativo)
use
if( $depart == "administrativo" )
You should enclose strings within quotes. Moreover, == (comparing objects of different types) && === (comparing objects of same types) are used for comparing and = is used for assigning. So, change the code as follows (inside the if statements) :
if ($depart == 'administrativo')
{
$email_to = '';
}
elseif ($depart == 'financeiro')
{
$email_to = '';
}
elseif ($depart == 'Suporte')
{
$email_to = '';
}
else
{
$email_to = '';
}
Hey, no idea why this sint working, but the message is being sent as "0". I think the e-mail fIELD is what is causing it
<?php
if ($_POST['check'] == 'checked'){
header("location: /nospamplease.html");
exit();
}
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$date = $_POST['date'];
$children = $_POST['children'];
$hot = $_POST['hot'];
$comments = $_POST['comments'];
/*echo $name;
echo $email;
echo $phone;
echo $date;
echo $children;
echo $hot;
echo $comments;*/
if($name == "" || $email == "" || $phone == "" || $date == "" || $children == "" || $hot == "" || $comments == ""){
echo "Please ensure all fields were filled out!";
exit();
}else{
$to = "######";
$subject = "Birthday enquiry";
$message = "Name: ".$name;
$message += "Email: ".$email;
$message += "Phone: ".$phone;
$message += "Date: ".$date;
$message += "Children: ".$children;
$message += "Hot or cold: ".$hot;
$message += "Comments: ".$comments;
//echo $message;
if(mail($to, $subject, $message)){
echo "Thank you for your enquiry, we will contact you within the next 24 hours! <br /> Click <a href='###'> here</a> to go back to the website!";
}else{
echo "There was an error, contact us directly: <a href='mailto:##'>email</a>";
}
}
?>
I echoed out the variables, they all are getting posted fine
Thanks guys
You can't use + to concatenate strings in PHP. Use .
$message .= "Email: ".$email;