Using a small contact form, however when the form is submitted/sent, I'm getting a "No data" message.
<form method="post" action="mail.php">
<input type="text" placeholder="Name*" name="name" required>
<input type="email" placeholder="Email*" name="email" required>
<input type="text" placeholder="Subject" name="subject">
<textarea placeholder="Message" name="message" required></textarea>
<input type="submit" value="Send" name="submit">
</form>
The PHP:
<?php
// variable
$fromemail = 'any_site#my_site_com'; // from mail
$to = "marygsheehan#yahoo.ie"; // to mail
//
// check data
if (!isset($_POST["fields"])) {
die("No data");
}
$fields = $_POST["fields"];
if( empty($fields['name']) ) {
die("No name");
}
if( empty($fields['email']) ) {
die("No email");
}
if (!empty( $fields['code'] ) ) {
die("ok");
}
$subject = "Site mail: " . $fields['subject'];
// subject massege
$subject = '=?utf-8?Q?'."\"".urlencode($subject)."\"".'?=';
$subject= str_replace("%","=",$subject);
$subject = str_replace("+","_",$subject);
// content massage
$name = $name ? $name : 'unknown';
$from = 'Mail from'."<".$fromemail.">";
$mess = $mess ? $mess : 'unknown';
$message = "<b>Client name: </b> " . $fields['name'] . "<br>";
$message .= "<b>Client email: </b> " . $fields['email'] . "<br>";
/*$message .= "<b>Client phone: </b> ".$site."<br>";*/
$message .= "<b>Subject: </b> " . $fields['subject'] . "<br>";
$message .= "<b>Text:</b>\n" . $fields['text'] . "<br>";
$message .= "Sent: ".strftime("%a, %d %b %Y %H:%M:%S");
// end content massage
$headers = "Content-type: text/html; charset=utf-8 \r\n";
$headers .= "From: Site Mail <" . $fromemail . ">\r\n";
if(mail($to, $subject, $message, $headers)){
print 'ok';
} else {
print 'email not senta';
}
?>
I've been staring at it so long it's a blur, so it's probably a silly mistake. Any help appreciated?
There is no input field in the form with name fields
You need to access them directly like this
$_POST['your_input_name_declared_in_form']
Do it as follow
if(isset($_POST['submit']))
{
$fields = $_POST;
if( empty($fields['name']) ) {
die("No name");
}
if( empty($fields['email']) ) {
die("No email");
}
if (!empty( $fields['code'] ) ) {
die("ok");
}
}
Related
I have a simple form for a website in HTML and PHP query.
HTML form
<form name="contactForm" id="contactForm" method="post" action="" >
<fieldset>
<div class="group">
<input name="contactName" type="text" id="contactName" placeholder="Name" value="" minLength="2" required />
</div>
<div>
<input name="contactEmail" type="email" id="contactEmail" placeholder="Email" value="" required />
</div>
<div>
<input name="contactSubject" type="text" id="contactSubject" placeholder="Subject" value="" />
</div>
<div>
<textarea name="contactMessage" id="contactMessage" placeholder="message" rows="10" cols="50" required ></textarea>
</div>
<div>
<button class="submitform contactdetails1">Submit</button>
<div id="submit-loader">
<div class="text-loader contactdetails2">Sending...</div>
<div class="s-loader">
<div class="bounce1"></div>
<div class="bounce2"></div>
<div class="bounce3"></div>
</div>
</div>
</div>
After trying to submit the form, I have the error messages that POST 405 NOT ALLOWED. It is unclear from the documentation how to set up a server and fix POST request.
PHP query
<?php
// Replace this with your own email address
$siteOwnersEmail = 'jeremie.xxxxx#xxxxx.com';
if($_POST) {
$name = trim(stripslashes($_POST['contactName']));
$email = trim(stripslashes($_POST['contactEmail']));
$subject = trim(stripslashes($_POST['contactSubject']));
$contact_message = trim(stripslashes($_POST['contactMessage']));
// Check Name
if (strlen($name) < 2) {
$error['name'] = "Please enter your name.";
}
// Check Email
if (!preg_match('/^[a-z0-9&\'\.\-_\+]+#[a-z0-9\-]+\.([a-z0-9\-]+\.)*+[a-z]{2}/is', $email)) {
$error['email'] = "Please enter a valid email address.";
}
// Check Message
if (strlen($contact_message) < 15) {
$error['message'] = "Please enter your message. It should have at least 15 characters.";
}
// Subject
if ($subject == '') { $subject = "Contact Form Submission"; }
// Set Message
$message .= "Email from: " . $name . "<br />";
$message .= "Email address: " . $email . "<br />";
$message .= "Message: <br />";
$message .= $contact_message;
$message .= "<br /> ----- <br /> This email was sent from your site's contact form. <br />";
// Set From: header
$from = $name . " <" . $email . ">";
// Email Headers
$headers = "From: " . $from . "\r\n";
$headers .= "Reply-To: ". $email . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
if (!$error) {
ini_set("sendmail_from", $siteOwnersEmail); // for windows server
$mail = mail($siteOwnersEmail, $subject, $message, $headers);
if ($mail) { echo "OK"; }
else { echo "Something went wrong. Please try again."; }
} # end if - no validation error
else {
$response = (isset($error['name'])) ? $error['name'] . "<br /> \n" : null;
$response .= (isset($error['email'])) ? $error['email'] . "<br /> \n" : null;
$response .= (isset($error['message'])) ? $error['message'] . "<br />" : null;
echo $response;
} # end if - there was a validation error
}
?>
...................................................................
I have a problem that after I fill out the contact form on my HTML website I receive over 50 same E-mails. Its a HTML form connected to contact.php file which code is shown bellow. I have set everything but maybe there is a problem in my code or somewhere else.
My code is over here
<?php
if(!$_POST) exit;
// Email address verification, do not edit.
function isEmail($email) {
return(preg_match("/^[-_.[:alnum:]]+#((([[:alnum:]]|[[:alnum:]][[:alnum:]-]*[[:alnum:]])\.)+ (ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cs|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|in|info|int|io|iq|ir|is|it|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|me|mg|mh|mil|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nt|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)$|(([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5])\.){3}([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]))$/i",$email));
}
if (!defined("PHP_EOL")) define("PHP_EOL", "\r\n");
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$subject = $_POST['subject'];
$comments = $_POST['comments'];
$verify = $_POST['verify'];
if(trim($name) == '') {
echo '<div class="error_message">Vyplnte meno.</div>';
exit();
} else if(trim($email) == '') {
echo '<div class="error_message">Vyplnte email.</div>';
exit();
} else if(!isEmail($email)) {
echo '<div class="error_message">Zadali ste nesprávny e-mail, skúste to znovu.</div>';
exit();
}
if(trim($comments) == '') {
echo '<div class="error_message">Vyplnte text správy.</div>';
exit();
}
if(get_magic_quotes_gpc()) {
$comments = stripslashes($comments);
}
// Configuration option.
// Enter the email address that you want to emails to be sent to.
// Example $address = "joe.doe#yourdomain.com";
$address = "noreply#marcelaskolenia.sk";
$address = "lubosmasura#gmail.com";
$toCustomer = $email;
// Configuration option.
// i.e. The standard subject will appear as, "You've been contacted by John Doe."
// Example, $e_subject = '$name . ' has contacted you via Your Website.';
$e_subject = 'Mate novu spravu od ' . $name . '.';
// Configuration option.
// You can change this if you feel that you need to.
// Developers, you may wish to add more fields to the form, in which case you must be sure to add them here.
$e_body = "Mate novu spravu od $name." . PHP_EOL . PHP_EOL;
$e_content = "\"$subject\"" . "\"$comments\"" . PHP_EOL . PHP_EOL;
$e_reply = "Kontaktujte $name cez email, $email alebo cez mobil $phone";
$msg = wordwrap( $e_body . $e_content . $e_reply, 70 );
$headers .= 'To: Test <noreply#marcelaskolenia.sk>' . "\r\n";
$headers .= 'From: Testk <noreply#marcelaskolenia.sk>' . "\r\n";
$headers .= "MIME-Version: 1.0" . PHP_EOL;
$headers .= "Content-type: text/plain; charset=utf-8" . PHP_EOL;
$headers .= "Content-Transfer-Encoding: quoted-printable" . PHP_EOL;
if(mail($address, $e_subject, $msg, $headers)) {
// Email has sent successfully, echo a success page.
echo "<fieldset>";
echo "<div id='success_page'>";
echo "<h3 class'mark'>Sprava bola odoslana.</h3>";
echo "<p>Dakujeme <strong>$name</strong>, Vasa sprava nam bude dorucena.</p>";
echo "</div>";
echo "</fieldset>";
} else {
echo 'ERROR!';
}
HTML code
<div class="contact_form">
<div id="message"></div>
<form id="contactform" class="row" action="contact.php" name="contactform" method="post">
<div class="col-md-12">
<input type="text" name="name" id="name" class="form-control" placeholder="Meno">
<input type="text" name="email" id="email" class="form-control" placeholder="Email">
<input type="text" name="phone" id="phone" class="form-control" placeholder="Telefónne číslo">
<input type="text" name="subject" id="subject" class="form-control" placeholder="Predmet">
<textarea class="form-control" name="comments" id="comments" rows="6" placeholder="Text správy"></textarea>
<button type="submit" value="SEND" id="submit" class="btn btn-primary"> ODOSLAŤ</button>
</div>
</form>
</div>
</div><!-- end col -->
Any Ideas why is this happening?
Thank you.
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 4 years ago.
I am having trouble with getting the mail php script to work, I probably dont understand why because I already looked it up on the internet, but I do not find a correct solution ( kinda new to this )
If I run the script it is always entering to the last if then else with something went wrong please try again.
Here are the scripts:
<?php
$siteOwnersEmail = 'test#gmail.be';
if($_POST) {
$name = trim(stripslashes($_POST['contactName']));
$email = trim(stripslashes($_POST['contactEmail']));
$subject = trim(stripslashes($_POST['contactSubject']));
$contact_message = trim(stripslashes($_POST['contactMessage']));
// Check Name
if (strlen($name) < 2) {
$error['name'] = "Please enter your name.";
}
// Check Email
if (!preg_match('/^[a-z0-9&\'\.\-_\+]+#[a-z0-9\-]+\.([a-z0-9\-]+\.)*+[a-z]{2}/is', $email)) {
$error['email'] = "Please enter a valid email address.";
}
// Check Message
if (strlen($contact_message) < 15) {
$error['message'] = "Please enter your message. It should have at least 15 characters.";
}
// Subject
if ($subject == '') { $subject = "Contact Form Submission"; }
// Set Message
$message .= "Email from: " . $name . "<br />";
$message .= "Email address: " . $email . "<br />";
$message .= "Message: <br />";
$message .= $contact_message;
$message .= "<br /> ----- <br /> This email was sent from your site's contact form. <br />";
// Set From: header
$from = $name . " <" . $email . ">";
// Email Headers
$headers = "From: " . $from . "\r\n";
$headers .= "Reply-To: ". $email . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
if (isset($error)) {
$response = (isset($error['name'])) ? $error['name'] . "<br /> \n" : null;
$response .= (isset($error['email'])) ? $error['email'] . "<br /> \n" : null;
$response .= (isset($error['message'])) ? $error['message'] . "<br />" : null;
echo $response;
} # end if - there was a validation error
else {
ini_set("sendmail_from", $siteOwnersEmail); // for windows server
$mail = mail($siteOwnersEmail, $subject, $message, $headers);
if ($mail) { echo "OK"; }
else { echo "Something went wrong. Please try again."; }
} # end if - no validation error
}
}
?>
And last but not least the form for submitting
<form name="contactForm" id="contactForm" action="inc/sendEmail.php" method="post" >
<fieldset>
<div class="form-field">
<input name="contactName" type="text" id="contactName" placeholder="Naam" value="" minlength="2" required="" aria-required="true" class="full-width">
</div>
<div class="form-field">
<input name="contactEmail" type="email" id="contactEmail" placeholder="Email" value="" required="" aria-required="true" class="full-width">
</div>
<div class="form-field">
<input name="contactSubject" type="text" id="contactSubject" placeholder="Onderwerp" value="" class="full-width">
</div>
<div class="form-field">
<textarea name="contactMessage" id="contactMessage" placeholder="Uw vraag" rows="10" cols="50" required="" aria-required="true" class="full-width"></textarea>
</div>
<div class="form-field">
<button class="full-width btn--primary">Submit</button>
<div class="submit-loader">
<div class="text-loader">Sending...</div>
<div class="s-loader">
<div class="bounce1"></div>
<div class="bounce2"></div>
<div class="bounce3"></div>
</div>
</div>
</div>
</fieldset>
</form>
Use this code to send mail it working
<?php
$malil = 'example#example.com';
$to = $malil;
$subject = "Your order has been placed.";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=ISO-8859-1' . "\r\n";
$headers .= "From: example#example.com" . "\r\n" ;
/* $headers .= "Cc:reservations#bookingsmaker.com,bookingsmakerdotcom#gmail.com,bookingsmakerworld#gmail.com "."\r\n";
*/
$headers .="Cc:example#example.com,example#example.in";
////////////////***********Html format*****************//////////////////
$htmlContent = 'Content';
$result=mail($to,$subject,$htmlContent,$headers);
if($result){
echo 'mail Send';
}else{
echo 'Mail not send';
}
?>
So I've made myself a little contact form with php, css, and html. But when I try to add a email validation it still sends the email and doesn't change the style of the input to red (Like I would like it to). Another issue I'm having is the button redirecting to the top of the page (which I do not want it to do). Last I can I make the input keep the text rather than remove it once submitted
HTML:
<div id="contact">
<div class="container">
<form id="contact-form" method="post">
<h1>Contact Form</h1>
<fieldset>
<input placeholder="Your Name" type="text" name="name" required>
</fieldset>
<fieldset>
<input placeholder="Your Email Address" type="email" name="email" id="email-input" required>
</fieldset>
<fieldset>
<input placeholder="Your Phone Number (optional)" type="tel" name="phone" required>
</fieldset>
<fieldset>
<input placeholder="Your Web Site (optional)" type="url" name="site" required>
</fieldset>
<fieldset>
<textarea placeholder="Type your message here...." name="message" required></textarea>
</fieldset>
<fieldset>
<button type="submit" id="contact-submit" name="submit">Submit</button>
</fieldset>
</form>
</div>
</div>
PHP:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$visitors_site = $_POST['site'];
$message = $_POST['message'];
$email_from = 'mattmowen1#gmail.com';
$email_subject = 'New Contact Submission';
$to = 'mattmowen1#gmail.com';
$headers = "From:" . $email;
$headers = "Contact Submission From: " . $email;
$message1 = "Name: " . $name;
$message2 = "\n\nEmail: " . $email;
$message3 = "\n\nPhone: " . $phone;
$message4 = "\n\nTheir Site: " . $visitors_site;
$message5 = "\n\nMessage: " . $message;
$email_body = $message1 . $message2 . $message3 . $message4 . $message5;
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
mail($to, $email_subject, $email_body,$headers);
} else {
echo "<style>#email-input {color:red}</style";
}
?>
Try this for email validation in php
<?php
if (isset($_POST) && !empty($_POST)) {
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$visitors_site = $_POST['site'];
$message = $_POST['message'];
$email_from = 'mattmowen1#gmail.com';
$email_subject = 'New Contact Submission';
$to = 'mattmowen1#gmail.com';
$headers = "From:" . $email;
$headers = "Contact Submission From: " . $email;
$message1 = "Name: " . $name;
$message2 = "\n\nEmail: " . $email;
$message3 = "\n\nPhone: " . $phone;
$message4 = "\n\nTheir Site: " . $visitors_site;
$message5 = "\n\nMessage: " . $message;
$email_body = $message1 . $message2 . $message3 . $message4 . $message5;
if (!filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
mail($to, $email_subject, $email_body,$headers);
} else {
echo "<style>#email-input {color:red}</style>";
}
}
?>
As per our chat conversation. I am adding jquery ajax function according to your form requirement.
You need to create new file email.php and put your php code into this separate php file
<script>
var url = 'email.php';
$.ajax({
url : url,
type : "POST",
dataType : "JSON",
data : $('#contact-form').serialize(),
success : function(response) {
if (response.error == 0) { // success
$('#contact-form')[0].reset();
alert('Form submitted successfully. We will contact you asap.');
} else { // error
$('#email-input').css('color', 'red');//in case of email error
alert('ERROR MESSAGE');//form is invalid
}
}
})
</script>
To handle JSON request you need to send JSON object in response. So change you php code snippet like this:
if (!filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
mail($to, $email_subject, $email_body,$headers);
exit(json_encode(array('error' => 0)));
} else {
exit(json_encode(array('error' => 1)));
}
PHP has never been my strong suit so I need some assistance getting the contents of a form to send via email properly in PHP.
Here is the html form:
<form action="estimate.php" method="post" >
<fieldset>
<input type="text" class="required" name="name" value="FULL NAME*" onfocus="if (this.value=='FULL NAME*') this.value='';"/>
<input type="text" class="required" name="phone" value="PHONE NUMBER*" onfocus="if (this.value=='PHONE NUMBER*') this.value='';"/>
<input type="text" class="required" name="email" value="EMAIL*" onfocus="if (this.value=='EMAIL*') this.value='';"/>
<input type="text" class="required" name="date" value="MOVE DATE*" onfocus="if (this.value=='MOVE DATE*') this.value='';"/>
<input type="text" class="required" name="origin" value="ORIGINATING ADDRESS*" onfocus="if (this.value=='ORIGINATING ADDRESS*') this.value='';"/>
<input type="text" name="destination" value="DESTINATION ADDRESS" onfocus="if (this.value=='DESTINATION ADDRESS') this.value='';"/>
<select name="move-type">
<option value="" selected="selected">TYPE OF MOVE</option>
<option value="Private">Private</option>
<option value="Commercial">Commercial</option>
</select>
<input id="quoteSubmit"
type="image" src="_images/btn_submit.png" alt=""
onmouseover="javascript:this.src='_images/btn_submit-over.png'"
onmouseout="javascript:this.src='_images/btn_submit.png'"/>
</fieldset>
</form>
Here is the PHP I have thus far:
<?php
$emailFromName = $_POST['name'];
$emailFromPhone = $_POST['phone'];
$emailFrom = $_POST['email'];
$emailDate = $_POST['date'];
$emailOrigin = $_POST['origin'];
$emailDestination = $_POST['destination'];
$emailType = $_POST['move-type'];
if (empty($emailFromName)) {
echo 'Please enter your name.';
} elseif (!preg_match('/^([A-Z0-9\.\-_]+)#([A-Z0-9\.\-_]+)?([\.]{1})([A-Z]{2,6})$/i', $emailFrom) || empty($emailFrom)) {
echo 'The email address entered is invalid.';
} elseif (empty($emailDate)) {
echo 'You must enter a Move date.';
} elseif (empty($emailOrigin)) {
echo 'You must enter a message.';
} elseif (empty($emailDestination)) {
echo 'You must enter a message.';
} else {
$emailTo = "info#movingsimple.com";
if (!empty($emailFrom)) {
$emailHeaders = 'From: "' . $emailFromName . '" <' . $emailFrom . '>';
} else {
$emailHeaders = 'From: "The Boss" <noreply#movingsimple.com>';
}
/* Send Email */
if (mail($emailTo, $emailSubject, $emailDate, $emailHeaders)) {
echo 'Thank you! Your message has been sent.';
} else {
echo 'There was an internal error while sending your email.<br>';
echo 'Please try again later.';
}
}
?>
I am having trouble with the body of the message. I need to display all the values from the form:
From:
Email:
Phone:
Move date:
Origin:
Destination:
Move type:
but I am not exactly sure how to compile all those into the body and would appreciate some help.
thanks.
Update this part of the code
if (!empty($emailFrom)) {
$emailHeaders = 'From: "' . $emailFromName . '" <' . $emailFrom . '>';
} else {
$emailHeaders = 'From: "The Boss" <noreply#movingsimple.com>';
}
/* Send Email */
if (mail($emailTo, $emailSubject, $emailDate, $emailHeaders)) {
echo 'Thank you! Your message has been sent.';
to generate a string containing the body of the email:
if (!empty($emailFrom)) {
$emailHeaders = 'From: "' . $emailFromName . '" <' . $emailFrom . '>';
} else {
$emailHeaders = 'From: "The Boss" <noreply#movingsimple.com>';
}
// Create a string representing the body. "\n" creates a new line.
// The ".=" operator appends the string onto the existing string.
$body = "From: ".$emailFromName."\n";
$body .= "Email: ".$emailFrom."\n";
$body .= "Phone: ".$emailFromPhone."\n";
$body .= "Move Date: ".$emailDate."\n";
$body .= "Origin: ".$emailOrigin."\n";
$body .= "Destination: ".$emailDestination."\n";
$body .= "Move Type: ".$emailType."\n";
/* Send Email */
if (mail($emailTo, $emailSubject, $body, $emailHeaders)) {
echo 'Thank you! Your message has been sent.';
// ... rest of the code ...
The third parameter of the mail() function (http://us.php.net/manual/en/function.mail.php) is the message, so we create the message as a string ($body) and then pass it as a parameter to the mail() function.
Small Update: The PHP mail() help page says to use "\n" as a line ending rather than "\r\n"
The third attribute to the mail function is for body of the email not date like you are specifying $emailDate, you should specify the email body there.
Right now you're sending the $emailDate as the body of the message.. You just need to build a variable with the data you want. i.e.
$emailBody = "Move date: " . $emailDate;
I think your header variable needs to have a \r\n at the end, too.