Undefined error in contact Form [duplicate] - php

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 5 years ago.
I recently created a contact form with php as well but everytime I click submit, I only get an undefined error, nothing else. Can anyone please give an insight into what I missed?
HTML
<form id="main-contact-form" class="contact-form" name="contact-form" method="post" action="contactengine.php">
<div class="col-sm-5 col-sm-offset-1">
<div class="form-group">
<label>Name *</label>
<input type="text" name="Name" class="form-control" required="required">
</div>
<div class="form-group">
<label>Email *</label>
<input type="email" name="Email" class="form-control" required="required">
</div>
<div class="form-group">
<label>Phone *</label>
<input type="number" name="Phone" class="form-control">
</div>
<div class="form-group">
<label>Company Name</label>
<input type="text" class="form-control">
</div>
</div>
<div class="col-sm-5">
<div class="form-group">
<label>Subject *</label>
<input type="text" name="subject" class="form-control" required="required">
</div>
<div class="form-group">
<label>Message *</label>
<textarea name="message" id="Message" required="required" class="form-control" rows="8"></textarea>
</div>
<div class="form-group">
<button type="submit" name="submit" class="btn btn-primary btn-lg" required="required">Submit Message</button>
</div>
</div>
</form>
PHP
<?php
if (isset($_POST["submit"])){
$EmailFrom = "$Email";
$EmailTo = "ben.afunsho#gmail.com";
$Subject = "New Message from Your Message";
$Name = trim(stripslashes($_POST['Name']));
$Phone = trim(stripslashes($_POST['Phone']));
$Email = trim(stripslashes($_POST['Email']));
$Subject = trim(stripslashes($_POST['Subject']));
$Message = trim(stripslashes($_POST['Message']));
// validation
$validationOK = true;
if (!$validationOK) {
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
exit;
}
// prepare email body text
$Body = "You have received a new email from";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "Phone: ";
$Body .= $Phone;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";
$Body .= "Subject: ";
$Body .= "$Subject: ";
$Body .= "\n";
$Body .= "Message: ";
$Body .= $Message; $Body .= "\n";
// send email $success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
// redirect to success page
if ($success) {
print "<meta http-equiv=\"refresh\" content=\"0;URL=contactthanks.php\">";
}else {
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
}
}
?>
I only get an undefined error on the html nothing else

There were some case sensitivity issues in your post and I corrected it and commented at corresponding lines.
Check it
<?php
if (isset($_POST["submit"]))
{ $EmailFrom = "$Email";
$EmailTo = "ben.afunsho#gmail.com";
$Subject = "New Message from Your Message";
$Name = trim(stripslashes($_POST['Name']));
$Phone = trim(stripslashes($_POST['Phone']));
$Email = trim(stripslashes($_POST['Email']));
$Subject = trim(stripslashes($_POST['subject'])); //s should be small
$Message = trim(stripslashes($_POST['message'])); // m should be small
// validation
$validationOK = true;
if (!$validationOK) {
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">"; exit;
}
// prepare email body text
$Body = "You have received a new email from";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "Phone: ";
$Body .= $Phone;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";
$Body .= "Subject: ";
$Body .= $Subject; // copy paste is very bad
$Body .= "\n";
$Body .= "Message: ";
$Body .= $Message;
$Body .= "\n";
// send email $success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
// redirect to success page i
if ($success) { print "<meta http-equiv=\"refresh\" content=\"0;URL=contactthanks.php\">"; }
else { print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">"; }}?>
<form id="main-contact-form" class="contact-form" name="contact-form" method="post" action="contactengine.php">
<div class="col-sm-5 col-sm-offset-1">
<div class="form-group">
<label>Name *</label>
<input type="text" name="Name" class="form-control" required="required">
</div>
<div class="form-group">
<label>Email *</label>
<input type="email" name="Email" class="form-control" required="required">
</div>
<div class="form-group">
<label>Phone *</label>
<input type="number" name="Phone" class="form-control">
</div>
<div class="form-group">
<label>Company Name</label>
<input type="text" class="form-control">
</div>
</div>
<div class="col-sm-5">
<div class="form-group">
<label>Subject *</label>
<input type="text" name="subject" class="form-control" required="required">
</div>
<div class="form-group">
<label>Message *</label>
<textarea name="message" id="Message" required="required" class="form-control" rows="8"></textarea>
</div>
<div class="form-group">
<input type="submit" name="submit" class="btn btn-primary btn-lg" value="Submit" />
</div>
</div>
</form>

Related

How to validate a form before sending a data using jquery and php?

I am using php. I want to know how to validate the form before it sends data. Currently the code works, but it must validate before a user sends data, it just send data without validating it and store it to the db record list and needs some help around and don't know if it safe to do on the back end than client side using jquery.
Front end code / HTML
<div class="row">
<div class="col-12">
<h2 class="contact-title">Get in Touch</h2>
</div>
<div class="col-lg-8">
<form action="contact_process.php" method="post">
<form class="form-contact contact_form">
<div class="row">
<div class="col-12">
<div class="form-group">
<textarea class="form-control w-100" name="message" id="message" cols="30" rows="9" onfocus="this.placeholder = ''" onblur="this.placeholder = 'Enter Message'" placeholder=" Enter Message"></textarea>
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<input class="form-control valid" name="name" id="name" type="text" onfocus="this.placeholder = ''" onblur="this.placeholder = 'Enter your name'" placeholder="Enter your name">
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<input class="form-control valid" name="email" id="email" type="email" onfocus="this.placeholder = ''" onblur="this.placeholder = 'Enter email address'" placeholder="Email">
</div>
</div>
<div class="col-12">
<div class="form-group">
<input class="form-control" name="subject" id="subject" type="text" onfocus="this.placeholder = ''" onblur="this.placeholder = 'Enter Subject'" placeholder="Enter Subject">
</div>
</div>
</div>
<div class="form-group mt-3">
<input type="submit" value="Submit">
</div>
</form>
</div>
Back end / PHP
<?php
$to = "info#acifinance.co.za";
$from = $_REQUEST['email'];
$name = $_REQUEST['name'];
$subject = $_REQUEST['subject'];
$number = $_REQUEST['number'];
$message = $_REQUEST['message'];
$headers = "From: $from";
$headers = "From: " . $from . "\r\n";
$headers .= "Reply-To: ". $from . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$subject = "$subject";
$message = "$message";
$logo = 'img/logo.png';
$link = '#';
$body = "<!DOCTYPE html><html lang='en'><head><meta charset='UTF-8'><title>Express Mail</title></head><body>";
$body .= "<table style='width: 100%;'>";
$body .= "<thead style='text-align: center;'><tr><td style='border:none;' colspan='2'>";
$body .= "<a href='{$link}'><img src='{$logo}' alt=''></a><br><br>";
$body .= "</td></tr></thead><tbody><tr>";
$body .= "<td style='border:none;'><strong>Name:</strong> {$name}</td>";
$body .= "<td style='border:none;'><strong>Email:</strong> {$from}</td>";
$body .= "</tr>";
$body .= "<tr><td style='border:none;'><strong>Subject:</strong> {$subject}</td></tr>";
$body .= "<tr><td></td></tr>";
$body .= "<tr><td colspan='2' style='border:none;'><strong>Message:</strong> {$message}</td></tr>";
$body .= "</tbody></table>";
$body .= "</body></html>";
$send = mail($to, $subject, $body, $headers);
$message = $_POST['message'];
$name =$_POST['name'];
$email =$_POST['email'];
$subject =$_POST['subject'];
$send = mail($to, $subject);
// It's time to connect with mysql database
$conn = new mysqli('localhost','acifinan_acifinan','Finance_01','acifinan_acifinanc');
If ($conn->connect_error)
{die('Connection Failed:'.$conn-> connect_error); }
else
{
$stmt= $conn->prepare("insert into form(message, name, email, subject) values (? , ? , ? , ?)");
$stmt-> bind_param("ssss", $message,$name,$email,$subject);
$stmt->execute();
echo "Submitted Successfully";
$stmt->close();
$conn->close();
}
?>

Check-box Value to be emailed

I have a form that allows people to request a free eBook. The form details will be sent to an admin, upon return they will email the eBook to the user. I need to be able to show in this "system" email whether the user gave consent to be manually subscribed to the newsletter or not.
The checkbox is there, checked as the default state.
Here is the system mail I receive after submitting the form:
--
Name: Aniska
Surname: Berry
Email: nissberry#gmail.com
Residential: Ballito
Newsletter:
--
^As you can see the "Newsletter" value is empty - I need to be able to see here whether the user gave consent or not. (Whether the box was checked or not)
Here is my form:
<form method="POST" id="contact-form" class="form-inline" action="contactengine.php" onSubmit="alert('Thank you for your feedback!');">
<div class="form-group white-text">
<label>Name</label>
<input type="text" name="Name" id="Name" class="form-control wow fadeInUp" placeholder="Name" required/>
</div>
<div class="form-group white-text">
<label>Surname</label>
<input type="text" name="Surname" id="Surname" class="form-control wow fadeInUp" placeholder="Surname" required/>
</div>
<div class="form-group white-text m-t">
<label>Email address</label>
<input type="text" name="Email" id="Email" class="form-control wow fadeInUp" placeholder="Email" required/>
</div>
<div class="form-group white-text m-t">
<label>Residential Area</label>
<input type="text" name="Residential" id="Residential" class="form-control wow fadeInUp" placeholder="Residential Area" required/>
</div>
<div class="form-group checkbox white-text">
<label>
<input name="newsletter" type="checkbox" class="checked" checked>Consent
</label>
</div>
<div class="form-group m-t m-b">
<input type="submit" name="submit" value="Please send me my EBook!" class="btn-primary wow fadeInUp" />
</div>
</form>
And here is my php
<?php
$EmailFrom = "admin#webmaster.com";
$EmailTo = "nissberry#gmail.com";
$Subject = "eBook request from User";
$Name = Trim(stripslashes($_POST['Name']));
$Surname = Trim(stripslashes($_POST['Surname']));
$Email = Trim(stripslashes($_POST['Email']));
$Residential = Trim(stripslashes($_POST['Residential']));
$Newsletter = Trim(stripslashes($_POST['Newsletter']));
// validation
$validationOK=true;
if (!$validationOK) {
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
exit;
}
// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "Surname: ";
$Body .= $Surname;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";
$Body .= "Residential: ";
$Body .= $Residential;
$Body .= "\n";
$Body .= "Newsletter: ";
$Body .= $Newsletter;
$Body .= "\n";
// send email
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
// redirect to success page
if ($success){
print "<meta http-equiv=\"refresh\" content=\"0;URL=http://www.remax-panache.co.za\">";
}
else{
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
}
?>
You cannot use check boxes that way. Use this way:
$Newsletter = isset($_POST['Newsletter']) ? true : false;
$Newsletter = isset($_POST['Newsletter']) ? "Yes" : "No";
Either of the above should work. When you POST a check box, it posts using this way:
Checked: name=on
Unchecked: (empty) Doesn't post anything.

Message not showing on PHP form when email received

I tested to receive to my own email, for some reason I get everything but its not showing the content of what I type. It doesnt show the actual message when I receive it. Can anyone help? Thanks!
<div class="contact-heading">
</div>
<form role="form" class="contact-form" action="contact-us.php" method="post">
<div class="form-group">
<label class="" for="exampleInputPassword1">Name</label>
<input type="text" name="name" class="form-control" id="exampleInputPassword1" placeholder="Name:" required >
</div>
<div class="form-group">
<label class="" for="exampleInputEmail2">Email address</label>
<input type="email" name="email" class="form-control" id="exampleInputEmail2" placeholder="E-Mail:" required >
</div>
<div class="form-group">
<label class="" for="exampleInputEmail3">Company</label>
<input type="text" name="company" class="form-control" id="exampleInputEmail3" placeholder="Company:" required >
</div>
<div class="form-group">
<label class="" for="exampleInputEmail4">Message</label>
<textarea class="form-control textarea" name="message" rows="8" id="exampleInputEmail4" placeholder="Message:"></textarea>
</div>
<button type="submit" class="btn btn-submit">Submit</button>
</form>
</div>
<?php
$to = 'emailsample#yahoo.com';
$subject = "Contact us";
$name = stripslashes($_REQUEST['name']);
$email = stripslashes($_REQUEST['email']);
$company = stripslashes($_REQUEST['company']);
$message = stripslashes($_REQUEST['message']);
$msg .= "Name: ".$name."\r\n";
$msg .= "E-mail: ".$email."\r\n";
$msg .= "Company: ".$company."\r\n";
$message .= "Topicjk: ".$message."\r\n";
$msg .= "Subject: ".$subject."\r\n\n";
$msg .= "---Message--- \r\n";
$msg .= "\r\n\n";
$mail = #mail($to, $subject, $msg, "From:".$email);
if($mail) {
header("Location:contact-us.html");
} else {
echo 'Message could not be sent! Please try again.';
}
?>
Because you are not passing $message (which has content) to mail in this line.
Change from
$mail = #mail($to, $subject, $msg, "From:".$email);
To
$mail = #mail($to, $subject, $message, "From:".$email);
or
Change from
$message .= "Topicjk: ".$message."\r\n";
To
$msg .= "Topicjk: ".$message."\r\n";

Troubleshooting php form submition blank fields in emails

I know this question has been asked before, I have researched but can't seem to get to the route of the problem. I'm hoping someone can help. Ok so form seems to be working in as much as the form has to be completed before it allows to be sent, the form sends and an email arrives. Unfortunately the email fields are blank.
So here's the php:
<?php
$Body = 'MIME-Version: 1.0' . "\r\n";
$Body .= 'Content-type: text/html; ch***t=utf-8' . "\r\n";
$Body .= 'From: '. $EmailFrom . "\r\n" . 'X-Mailer: PHP/' . phpversion();
$EmailFrom = "info#mywebsite.com";
$EmailTo = "info#mywebsite.com";
$Subject = "Contact Form";
$Name = trim(stripslashes($_POST['Name']));
$Email = trim(stripslashes($_POST['Email']));
$Message = trim(stripslashes($_POST['Message']));
// validation
$validationOK=true;
if (!$validationOK) {
echo "please check your details";
header("Location: http://mywebsite.com/formredirect.html");
exit;
}
// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $Message;
$Body .= "\n";
// send email
$success = mail($EmailTo, $Subject, $Body, "From: $EmailFrom" . "\r\n");
// redirect to success page
if ($success){
print "<meta http-equiv=\"refresh\" content=\"1;URL=formredirect.htm\">";
}
else{
print "<meta http-equiv=\"refresh\" content=\"1;URL=error.html\">";
}
?>
My HTML:
<form method="post" action="contact.php" name="contact" id="contact">
<div class="row 50%">
<div class="6u 12u(mobile)">
<input type="text" name="name" id="name" placeholder="Name" required />
</div>
<div class="6u 12u(mobile)">
<input type="text" name="email" id="email" placeholder="Email" required />
</div>
</div>
<div class="row 50%">
<div class="12u">
<textarea name="message" id="message" placeholder="Message" required></textarea>
</div>
</div>
<div class="row 50%">
<div class="12u">
<input type="submit" class="button" id="submit" value="submit"></a>
</div>
</div>
</form>
I appreciate it's probably me being really dense. I'm new to this so am fumbling my way around. Any help would be greatly appreciated. Many thanks!
The $_POST keys are CaSe SenSitIve
$_POST['Name'] is different from $_POST['name']
Either change the form name attribute or the POST keys so they match, i.e.:
FORM
<input type="text" name="name" id="name" placeholder="Name" required />
...
<input type="text" name="email" id="email" placeholder="Email" required />
...
<textarea name="message" id="message" placeholder="Message" required></textarea>
...
PHP
...
$Name = trim(stripslashes($_POST['name']));
$Email = trim(stripslashes($_POST['email']));
$Message = trim(stripslashes($_POST['message']));
...

My PHP/HTML form doesn't work

I have a contact form in my website and it doesn't work properly. I get the email but the email it's empty, I don't get the information from the form.
the part of the html in my index.html
<form role="form" form name="contactform" method="post" action="send_form_email.php">
<div class="form-group">
<label>Nombre</label> <input type="text" name="nombre" class="form-control" placeholder="Introduce tu nombre" >
</div>
<div class="form-group">
<label>Apellidos</label> <input type="text" name="apellidos" class="form-control" placeholder="Introduce tus apellidos" >
</div>
<div class="form-group">
<label>Email</label> <input type="emal" name="email" class="form-control" placeholder="Introduce tu email" >
</div>
<div class="form-group">
<label>Telefono</label> <input type="text" name="telefono" class="form-control" placeholder="Introduce tu telefono" >
</div>
<div class="form-group">
<label>Mensaje</label> <textarea name="mensaje" class="form-control" rows="7"></textarea>
</div>
<button type="submit" class="btn btn-default" value="Submit"> Enviar
</form>
</div>
the part of the php file called "send_form_email.php"
<?php
$nombre = $_POST['nombre'];
$apellidos = $_POST['apellidos'];
$telefono = $_POST['telefono'];
$email = $_POST['email'];
$mensaje = $_POST['mensaje'];
$formcontent=" $nombre $apellidos $email $telefono $mensaje";
$recipient = "deniz946#gmail.com";
$subject = "Subject from $nombre";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You! We will get back to you as soon as possible!" . " -" . "<a href='./index.php'> Back to site</a>";
?>
<input type="emal" name="email" class="form-control" placeholder="Introduce tu email" > change to
<input type="email" name="email" class="form-control" placeholder="Introduce tu email" >
you have typo in type attribute.
Try this
<?php
$nombre = $_POST['nombre'];
$apellidos = $_POST['apellidos'];
$telefono = $_POST['telefono'];
$email = $_POST['email'];
$mensaje = $_POST['mensaje'];
$formcontent = "{$nombre} {$apellidos} {$email} {$telefono} {$mensaje}";
$recipient = "deniz946#gmail.com";
$subject = "Subject from $nombre";
if(PHP_OS == "Linux" || PHP_OS == "Darwin") $new_line = "\n"; /* if Linux or Mac */
elseif(PHP_OS == "WINNT") $new_line = "\r\n"; /* if Windows */
$mailheader = "MIME-Version: 1.1" . $new_line;
$mailheader .= "Content-type: text/html; charset=utf-8" . $new_line;
$mailheader .= "From: ".$recipient.$new_line;
$mailheader .= "Return-Path: " . $recipient . $new_line;
$mailheader .= "Reply-To: " . $recipient . $new_line;
mail($recipient, $subject, $formcontent, $mailheader, "-r" . $recipient) or die("Error!");
echo "Thank You! We will get back to you as soon as possible!" . " -" . "<a href='./index.php'> Back to site</a>";
?>
Perhaps I can help you with an example I used myself:
The HTML contact Form:
<div class="row">
<!-- Content -->
<div id="content" class="8u skel-cell-mainContent">
<section class="12u">
<header>
<h2>Contact</h2>
</header>
<form method="post" action="mail.php">
<div class="row half">
<div class="6u">
<input name="subject" placeholder="Name" type="text" class="text" />
</div>
<div class="6u">
<input name='email' placeholder="Email" type="text" class="text" />
</div>
</div>
<div class="row half">
<div class="12u">
<textarea name="message" placeholder="Message"></textarea>
</div>
</div>
<div class="row half">
<div class="12u">
<!--a href="mail.php" class="button">Submit</a-->
<input type="submit" class="button" value="Submit">
</div>
</div>
</form>
</section>
</div>
The PHP processing of the form:
<?php
//mail(to,subject,message,headers,parameters)
// Check if the "from" input field is filled out
//if (isset($_POST['from']))
//{}
$to = 'mail#xxxxxx.com'; // my email address
$subject = $_POST['subject']; //name client
$feedback = 'thank you, we will reply soon.';
$header = $_POST['email']; //Email client
$message = $_POST['message'];
$message = wordwrap($message, 70);
$message = <<<EMAIL
Dear (YOUR WEBSITE NAME HERE),
My name is: $subject
$message
Email Client: $header
EMAIL;
// send mail
if($_POST){
mail($to, $subject, $message, $header);
echo $feedback;
}
?>
Remember very carefully, the part from Dear... to EMAIL has to be against the left side of the screen, without any tabs or spaces. Else it won't work!
Hopefully you can use this to your success :)

Categories