I have a contact us page in html including a form where a user can send message with that form. I want to send the message with PHP mailer function. But the problem is after i sending the message I want to redirect to the page with message. But instead of loading the it's showing the response in a new page. How do i send the user to the same page again?
Here is my Contact.html
<?php
if (isset($arrResult)) {
if($arrResult['response'] == 'success') {
?>
<div class="alert alert-success" id="contactSuccess">
<strong>Success!</strong> Your message has been sent to us.
</div>
<?php
} else if($arrResult['response'] == 'error') {
?>
<div class="alert alert-danger" id="contactError">
<strong>Error!</strong> There was an error sending your message. (<?php echo $arrResult['error'];?>)
</div>
<?php
}
}
?>
<h2 class="mb-sm mt-sm"><strong>Contact</strong> Us</h2>
<form id="contactForm" action="php/contact-form.php" method="POST">
<div class="row">
<div class="form-group">
<div class="col-md-6">
<label>Your name *</label>
<input type="text" value="" data-msg-required="Please enter your name." maxlength="100" class="form-control" name="name" id="name" required>
</div>
<div class="col-md-6">
<label>Your email address *</label>
<input type="email" value="" data-msg-required="Please enter your email address." data-msg-email="Please enter a valid email address." maxlength="100" class="form-control" name="email" id="email" required>
</div>
</div>
</div>
<div class="row">
<div class="form-group">
<div class="col-md-12">
<label>Subject</label>
<input type="text" value="" data-msg-required="Please enter the subject." maxlength="100" class="form-control" name="subject" id="subject" required>
</div>
</div>
</div>
<div class="row">
<div class="form-group">
<div class="col-md-12">
<label>Message *</label>
<textarea maxlength="5000" data-msg-required="Please enter your message." rows="10" class="form-control" name="message" id="message" required></textarea>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<input type="submit" value="Send Message" onclick="myFunction()"class="btn btn-primary btn-lg mb-xlg" data-loading-text="Loading...">
</div>
</div>
</form>
</div>
Here is the contact-form.php
<?php
session_cache_limiter('nocache');
header('Expires: ' . gmdate('r', 0));
header('Content-type: application/json');
require_once('php-mailer/PHPMailerAutoload.php');
$email = 'myemail#yahoo.co';
$subject = $_POST['subject'];
$fields = array(
0 => array(
'text' => 'Name',
'val' => $_POST['name']
),
1 => array(
'text' => 'Email address',
'val' => $_POST['email']
),
2 => array(
'text' => 'Message',
'val' => $_POST['message']
)
);
$message = '';
foreach($fields as $field) {
$message .= $field['text'].": " . htmlspecialchars($field['val'], ENT_QUOTES) . "<br>\n";
}
$mail = new PHPMailer(true);
try {
$mail->SMTPDebug = $debug;
$mail->AddAddress($email);
$mail->SetFrom($email, $_POST['name']);
$mail->AddReplyTo($_POST['email'], $_POST['name']);
$mail->IsHTML(true); // Set email format to HTML
$mail->CharSet = 'UTF-8';
$mail->Subject = $subject;
$mail->Body = $message;
$mail->Send();
$arrResult = array ('response'=>'success');
} catch (phpmailerException $e) {
$arrResult = array ('response'=>'error','errorMessage'=>$e->errorMessage());
} catch (Exception $e) {
$arrResult = array ('response'=>'error','errorMessage'=>$e->getMessage());
}
if ($debug == 0) {
echo json_encode($arrResult);
}
Use header
header('Location: http://www.example.com/')
Header doc
Related
I use this contact form by Boostrapious and works well, but I can't find solution to add some checkbox inputs and send values by email. I added HTML inputs, but I need help with php code. Can anyone help me?
This is a link to this tutorial and code:
https://bootstrapious.com/p/how-to-build-a-working-bootstrap-contact-form
<form id="contact-form" method="post" action="contact.php" role="form">
<div class="messages"> </div>
<div class="controls">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label for="form_name">Firstname *</label>
<input id="form_name" type="text" name="name" class="form-control" placeholder="Please enter your firstname *" required="required" data-error="Firstname is required.">
<div class="help-block with-errors"></div>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label for="form_lastname">Lastname *</label>
<input id="form_lastname" type="text" name="surname" class="form-control" placeholder="Please enter your lastname *" required="required" data-error="Lastname is required.">
<div class="help-block with-errors"></div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label for="form_email">Email *</label>
<input id="form_email" type="email" name="email" class="form-control" placeholder="Please enter your email *" required="required" data-error="Valid email is required.">
<div class="help-block with-errors"></div>
</div>
</div>
<div class="col-md-6">
<div class="form-group checkbox">
<label for="checkboxoptions1">
<input type="checkbox" name="checkboxoptions[]" id="checkboxoptions1" value="checkboxoptions1">
Option1
</label>
<label for="checkboxoptions2">
<input type="checkbox" name="checkboxoptions[]" id="checkboxoptions2" value="checkboxoptions2">
Option2
</label>
<label for="checkboxoptions3">
<input type="checkbox" name="checkboxoptions[]" id="checkboxoptions3" value="checkboxoptions3">
Option3
</label>
<div class="help-block with-errors"></div>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="form-group">
<label for="form_message">Message *</label>
<textarea id="form_message" name="message" class="form-control" placeholder="Message for me *" rows="4" required="required" data-error="Please, leave us a message."></textarea>
<div class="help-block with-errors"></div>
</div>
</div>
<div class="col-md-12">
<input type="submit" class="btn btn-success btn-send" value="Send message">
</div>
</div>
<div class="row">
<div class="col-md-12">
<p class="text-muted">
<strong>*</strong> These fields are required. Contact form template by
Bootstrapious.</p>
</div>
</div>
</div>
</form>
and php code here
<?php
/*
* CONFIGURE EVERYTHING HERE
*/
// an email address that will be in the From field of the email.
$from = 'Demo contact form <demo#domain.com>';
// an email address that will receive the email with the output of the form
$sendTo = 'Demo contact form <demo#domain.com>';
// subject of the email
$subject = 'New message from contact form';
// form field names and their translations.
// array variable name => Text to appear in the email
$fields = array('checkboxoptions' => 'My options','name' => 'Name', 'surname' => 'Surname', 'phone' => 'Phone', 'email' => 'Email', 'message' => 'Message');
// message that will be displayed when everything is OK :)
$okMessage = 'Contact form successfully submitted. Thank you, I will get back to you soon!';
// If something goes wrong, we will display this message.
$errorMessage = 'There was an error while submitting the form. Please try again later';
/*
* LET'S DO THE SENDING
*/
// if you are not debugging and don't need error reporting, turn this off by error_reporting(0);
error_reporting(E_ALL & ~E_NOTICE);
try
{
if(count($_POST) == 0) throw new \Exception('Form is empty');
$emailText = "You have a new message from your contact form\n=============================\n";
foreach ($_POST as $key => $value) {
// If the field exists in the $fields array, include it in the email
if (isset($fields[$key])) {
$emailText .= "$fields[$key]: $value\n";
}
}
// All the necessary headers for the email.
$headers = array('Content-Type: text/plain; charset="UTF-8";',
'From: ' . $from,
'Reply-To: ' . $_POST['email'],
'Return-Path: ' . $from,
);
// Send email
mail($sendTo, $subject, $emailText, implode("\n", $headers));
$responseArray = array('type' => 'success', 'message' => $okMessage);
}
catch (\Exception $e)
{
$responseArray = array('type' => 'danger', 'message' => $errorMessage);
}
// if requested by AJAX request return JSON response
if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
$encoded = json_encode($responseArray);
header('Content-Type: application/json');
echo $encoded;
}
// else just display the message
else {
echo $responseArray['message'];
so I am building a contact for mon my website, but for some reasons, I am getting this error
[![error image][1]][1] (https://prnt.sc/vw49gd - link to image as the one uploaded doesn't seem to get it)
My HTML form is fairly simple:
<form class="nk-form-submit" action="form/contact.php" method="post">
<div class="row">
<div class="col-sm-6">
<div class="field-item animated" data-animate="fadeInUp" data-delay="0.8">
<label class="field-label textSubTittleColors ttu">Your Name</label>
<div class="field-wrap">
<input name="contact-name" placeholder="First & Last Name" type="text" class="input-bordered contactUsTextfields required">
</div>
</div>
</div>
<div class="col-sm-6">
<div class="field-item animated" data-animate="fadeInUp" data-delay="0.9">
<label class="field-label textSubTittleColors ttu">Your Email</label>
<div class="field-wrap">
<input name="contact-email" placeholder="example#gmail.com" type="email" class="input-bordered contactUsTextfields required email">
</div>
</div>
</div>
</div>
<div class="field-item animated" data-animate="fadeInUp" data-delay="1.0">
<label class="field-label textSubTittleColors ttu">Your Message</label>
<div class="field-wrap ">
<textarea name="contact-message" placeholder="Leave your question or comment here" class="input-bordered contactUsTextfields input-textarea required"></textarea>
</div>
</div>
<input type="text" class="d-none" name="form-anti-honeypot" value="">
<div class="row">
<div class="col-sm-5 text-right animated" data-animate="fadeInUp" data-delay="1.1">
<button type="submit" name="submit" class="btn sendButton" >SEND</button>
</div>
<div class="col-sm-7 order-sm-first">
<div class="form-results"></div>
</div>
</div>
</form>
But the issue relies on the contact.php code:
<?php
header('Content-type: application/json');
require_once('php-mailer/PHPMailerAutoload.php'); // Include PHPMailer
$mail = new PHPMailer();
$emailTO = $emailBCC = $emailCC = array(); $formEmail = '';
### Enter Your Sitename
$sitename = 'Organization';
### Enter your email addresses: #required
$emailTO[] = array( 'email' => 'myname#microsoftMail.org', 'name' => 'name' );
### Enable bellow parameters & update your BCC email if require.
//$emailBCC[] = array( 'email' => 'email#yoursite.com', 'name' => 'Your Name' );
### Enable bellow parameters & update your CC email if require.
//$emailCC[] = array( 'email' => 'email#yoursite.com', 'name' => 'Your Name' );
### Enter Email Subject
$subject = "Contact Us " . ' - ' . $sitename;
### If your did not recive email after submit form please enable below line and must change to your correct domain name. eg. noreply#example.com
//$formEmail = 'noreply#yoursite.com';
### Success Messages
$msg_success = "We have <strong>successfully</strong> received your message. We'll get back to you soon.";
if( $_SERVER['REQUEST_METHOD'] == 'POST') {
if (isset($_POST["contact-email"]) && $_POST["contact-email"] != '' && isset($_POST["contact-name"]) && $_POST["contact-name"] != '') {
### Form Fields
$cf_email = $_POST["contact-email"];
$cf_name = $_POST["contact-name"];
$cf_message = isset($_POST["contact-message"]) ? $_POST["contact-message"] : '';
$honeypot = isset($_POST["form-anti-honeypot"]) ? $_POST["form-anti-honeypot"] : 'bot';
$bodymsg = '';
if ($honeypot == '' && !(empty($emailTO))) {
### If you want use SMTP
// $mail->isSMTP();
// $mail->SMTPDebug = 0;
// $mail->Host = 'smtp_host';
// $mail->Port = 587;
// $mail->SMTPAuth = true;
// $mail->Username = 'smtp_username';
// $mail->Password = 'smtp_password';
### Regular email configure
$mail->IsHTML(true);
$mail->CharSet = 'UTF-8';
$mail->From = ($formEmail !='') ? $formEmail : $cf_email;
$mail->FromName = $cf_name . ' - ' . $sitename;
$mail->AddReplyTo($cf_email, $cf_name);
$mail->Subject = $subject;
foreach( $emailTO as $to ) {
$mail->AddAddress( $to['email'] , $to['name'] );
}
### if CC found
if (!empty($emailCC)) {
foreach( $emailCC as $cc ) {
$mail->AddCC( $cc['email'] , $cc['name'] );
}
}
### if BCC found
if (!empty($emailBCC)) {
foreach( $emailBCC as $bcc ) {
$mail->AddBCC( $bcc['email'] , $bcc['name'] );
}
}
### Include Form Fields into Body Message
$bodymsg .= isset($cf_name) ? "Contact Name: $cf_name<br><br>" : '';
$bodymsg .= isset($cf_email) ? "Contact Email: $cf_email<br><br>" : '';
$bodymsg .= isset($cf_message) ? "Message: $cf_message<br><br>" : '';
$bodymsg .= $_SERVER['HTTP_REFERER'] ? '<br>---<br><br>This email was sent from: ' . $_SERVER['HTTP_REFERER'] : '';
// Mailing
$mail->MsgHTML( $bodymsg );
$is_emailed = $mail->Send();
if( $is_emailed === true ) {
$response = array ('result' => "success", 'message' => $msg_success);
} else {
$response = array ('result' => "error", 'message' => $mail->ErrorInfo);
}
echo json_encode($response);
} else {
echo json_encode(array ('result' => "error", 'message' => "Bot <strong>Detected</strong>.! Clean yourself Botster.!"));
}
} else {
echo json_encode(array ('result' => "error", 'message' => "Please <strong>Fill up</strong> all required fields and try again."));
}
}
I have uploaded this on the hostgator domain, but every time I click the send button I keep getting the error mentioned above.
I also tried several different codes from either other posts here in stackoverflow or from youtube videos I have seen, but its all the same result.
[1]: https://i.stack.imgur.com/MQQxS.png
You may have used the wrong HTTP request type. Try the method attribute as shown below.
method="get"
As used in:
Change your HTML to:
<form class="nk-form-submit" action="form/contact.php" method="get">
<div class="row">
<div class="col-sm-6">
<div class="field-item animated" data-animate="fadeInUp" data-delay="0.8">
<label class="field-label textSubTittleColors ttu">Your Name</label>
<div class="field-wrap">
<input name="contact-name" placeholder="First & Last Name" type="text" class="input-bordered contactUsTextfields required">
</div>
</div>
</div>
<div class="col-sm-6">
<div class="field-item animated" data-animate="fadeInUp" data-delay="0.9">
<label class="field-label textSubTittleColors ttu">Your Email</label>
<div class="field-wrap">
<input name="contact-email" placeholder="example#gmail.com" type="email" class="input-bordered contactUsTextfields required email">
</div>
</div>
</div>
</div>
<div class="field-item animated" data-animate="fadeInUp" data-delay="1.0">
<label class="field-label textSubTittleColors ttu">Your Message</label>
<div class="field-wrap ">
<textarea name="contact-message" placeholder="Leave your question or comment here" class="input-bordered contactUsTextfields input-textarea required"></textarea>
</div>
</div>
<input type="text" class="d-none" name="form-anti-honeypot" value="">
<div class="row">
<div class="col-sm-5 text-right animated" data-animate="fadeInUp" data-delay="1.1">
<button type="submit" name="submit" class="btn sendButton" >SEND</button>
</div>
<div class="col-sm-7 order-sm-first">
<div class="form-results"></div>
</div>
</div>
</form>
i have this problem. I made a bootstrap form with a php included and instead of displaying a message if the form is filled and sent successfuly i need it to redirect to a different page (.html).
This is the PHP page for the form:
<?php
/*
* CONFIGURE EVERYTHING HERE
*/
// an email address that will be in the From field of the email.
$from = 'email#email.com';
// an email address that will receive the email with the output of the form
$sendTo = 'email#mail.cz';
// subject of the email
$subject = 'Nová zpráva z Vašeho webu';
// form field names and their translations.
// array variable name => Text to appear in the email
$fields = array('name' => 'Name', 'surname' => 'Surname', 'phone' =>
'Phone', 'email' => 'Email', 'message' => 'Message');
// message that will be displayed when everything is OK :)
$okMessage = 'Děkuji! Vaše zpráva byla úspěšně odeslána. ';
// If something goes wrong, we will display this message.
$errorMessage = 'Omlouvám se, ale došlo k nečekaně chybě. Zkuste to prosím
později.';
/*
* LET'S DO THE SENDING
*/
// if you are not debugging and don't need error reporting, turn this off by
error_reporting(0);
error_reporting(E_ALL & ~E_NOTICE);
try
{
if(count($_POST) == 0) throw new \Exception('Form is empty');
$emailText = "You have a new message from your contact form\n=============================\n";
foreach ($_POST as $key => $value) {
// If the field exists in the $fields array, include it in the email
if (isset($fields[$key])) {
$emailText .= "$fields[$key]: $value\n";
}
}
// All the neccessary headers for the email.
$headers = array('Content-Type: text/plain; charset="UTF-8";',
'From: ' . $from,
'Reply-To: ' . $from,
'Return-Path: ' . $from,
);
// Send email
mail($sendTo, $subject, $emailText, implode("\n", $headers));
$responseArray = array('type' => 'success', 'message' => $okMessage);
}
catch (\Exception $e)
{
$responseArray = array('type' => 'danger', 'message' => $errorMessage);
}
// if requested by AJAX request return JSON response
if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) &&
strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
$encoded = json_encode($responseArray);
header('Content-Type: application/json');
echo $encoded;
}
// else just display the message
else {
echo $responseArray['message'];
}
And here is the HTML for the form:
<form id="contact-form" method="post" action="contact.php">
<div class="messages"></div>
<div class="controls">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label for="form_name">Jméno *</label>
<input id="form_name" type="text" name="name" class="form-control" placeholder="Vaše křestní jméno *" required="required" data-error="Firstname is required.">
<div class="help-block with-errors"></div>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label for="form_lastname">Příjmení *</label>
<input id="form_lastname" type="text" name="surname" class="form-control" placeholder="Vaše příjmení *" required="required" data-error="Lastname is required.">
<div class="help-block with-errors"></div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label for="form_email">Email *</label>
<input id="form_email" type="email" name="email" class="form-control" placeholder="Váš platný email *" required="required" data-error="Valid email is required.">
<div class="help-block with-errors"></div>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label for="form_phone">Telefon</label>
<input id="form_phone" type="tel" name="phone" class="form-control" placeholder="Váš telefon">
<div class="help-block with-errors"></div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="form-group">
<label for="form_message">Vaše zpráva *</label>
<textarea id="form_message" name="message" class="form-control" placeholder="Vaše zpráva *" rows="4" required="required" data-error="Please,leave us a message."></textarea>
<div class="help-block with-errors"></div>
</div>
<br>
</div>
<div class="col-md-12">
<input type="submit" class="odeslat" value="Odeslat">
</div>
</div>
<div class="row">
<div class="col-md-12">
<br><p class="text-muted"><strong>*</strong> Tyto pole jsou povinná.</p>
</div>
</div>
</div>
</form>
can u pls help me fix it so it doesn't display only the message?
Thank you
You can write in the end a if sentence that shows this echo in the true case and doesn't show in a false case, some like that ->
if(!$ejecutar)
{
echo "whatever you want";
}
else
{
echo "Whatever you want<br><a href='index.php'>Volver</a>";
}
I have a form in my website, and it works well. It was part of a oakler template and so far I received subject,email,message information back.
I added a telephone field trying to follow the structure of the form but am no php expert and don't know what is not working.
In the email sent I receive the field 'telephone' but it is empty.
Please help.
The website is this: http://ruidematos.co.uk/contact-psychologist-hypnotherapist-london-harley-street
The form html is this:
<form id="contactForm" action="php/contact-form.php" method="POST">
<div class="row">
<div class="form-group">
<div class="col-md-12">
<input type="text" placeholder="Subject" value="" data-msg-required="Please enter the subject." maxlength="100" class="form-control input-lg" name="subject" id="subject" required>
</div>
</div>
</div>
<div class="row">
<div class="form-group">
<div class="col-md-12">
<textarea maxlength="5000" placeholder="Message" data-msg-required="Please enter your message." rows="10" class="form-control input-lg" name="message" id="message" required></textarea>
</div>
</div>
</div>
<div class="row">
<div class="form-group">
<div class="col-md-12">
<input type="text" placeholder="Your Name" value="" data-msg-required="Please enter your name." maxlength="100" class="form-control input-lg" name="name" id="name" required>
</div>
</div>
</div>
<div class="row">
<div class="form-group">
<div class="col-md-12">
<input type="email" placeholder="Your E-mail" value="" data-msg-required="Please enter your email address." data-msg-email="Please enter a valid email address." maxlength="100" class="form-control input-lg" name="email" id="email" required>
</div>
</div>
</div>
<div class="row">
<div class="form-group">
<div class="col-md-12">
<input type="text" placeholder="Your Telephone" value="" data-msg-required="Please enter your telephone." data-msg-email="Please enter a valid number." maxlength="30" class="form-control input-lg" name="phone" id="phone" required>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<input type="submit" value="Send Message" class="btn btn-primary btn-lg mb-xs" data-loading-text="Loading...">
</div>
</div>
</form>
The PHP code is this:
session_cache_limiter('nocache');
header('Expires: ' . gmdate('r', 0));
header('Content-type: application/json');
require_once('php-mailer/PHPMailerAutoload.php');
// Step 1 - Enter your email address below.
$email = 'info#ruidematos.co.uk';
// If the e-mail is not working, change the debug option to 2 | $debug = 2;
$debug = 0;
$subject = $_POST['subject'];
$fields = array(
0 => array(
'text' => 'Name',
'val' => $_POST['name']
),
1 => array(
'text' => 'Email address',
'val' => $_POST['email']
),
2 => array(
'text' => 'Message',
'val' => $_POST['message']
),
3 => array(
'text' => 'Telephone',
'val' => $_POST['phone']
)
);
$message = '';
foreach($fields as $field) {
$message .= $field['text'].": " . htmlspecialchars($field['val'], ENT_QUOTES) . "<br>\n";
}
$mail = new PHPMailer(true);
try {
$mail->SMTPDebug = $debug; // Debug Mode
// Step 2 (Optional) - If you don't receive the email, try to configure the parameters below:
//$mail->IsSMTP(); // Set mailer to use SMTP
//$mail->Host = 'mail.yourserver.com'; // Specify main and backup server
//$mail->SMTPAuth = true; // Enable SMTP authentication
//$mail->Username = 'user#example.com'; // SMTP username
//$mail->Password = 'secret'; // SMTP password
//$mail->SMTPSecure = 'tls'; // Enable encryption, 'ssl' also accepted
//$mail->Port = 587; // TCP port to connect to
$mail->AddAddress($email); // Add another recipient
//$mail->AddAddress('person2#domain.com', 'Person 2'); // Add a secondary recipient
//$mail->AddCC('person3#domain.com', 'Person 3'); // Add a "Cc" address.
//$mail->AddBCC('person4#domain.com', 'Person 4'); // Add a "Bcc" address.
$mail->SetFrom($email, $_POST['name']);
$mail->AddReplyTo($_POST['email'], $_POST['name']);
$mail->IsHTML(true); // Set email format to HTML
$mail->CharSet = 'UTF-8';
$mail->Subject = $subject;
$mail->Body = $message;
$mail->Send();
$arrResult = array ('response'=>'success');
} catch (phpmailerException $e) {
$arrResult = array ('response'=>'error','errorMessage'=>$e->errorMessage());
} catch (Exception $e) {
$arrResult = array ('response'=>'error','errorMessage'=>$e->getMessage());
}
if ($debug == 0) {
echo json_encode($arrResult);
}
You have added the HTML element for phone correctly, but it seems the Javascript handler that submits the form has a specified list of elements it fetches, and not the entire form.
Look in http://ruidematos.co.uk/js/views/view.contact.js , within the function $('#contactForm').validate( on line 26 the ajax call is performed and submits via POST the 4 elements name, email, subject, ,message:
$.ajax({
type: 'POST',
url: $form.attr('action'),
data: {
name: $form.find('#name').val(),
email: $form.find('#email').val(),
subject: $form.find('#subject').val(),
message: $form.find('#message').val()
}
})
After the subject line add one for phone too and it should work.
$.ajax({
type: 'POST',
url: $form.attr('action'),
data: {
name: $form.find('#name').val(),
email: $form.find('#email').val(),
subject: $form.find('#subject').val(),
phone: $form.find('#phone').val(),
message: $form.find('#message').val()
}
})
Your rename message to phone.
name: $form.find('#name').val(),
email: $form.find('#email').val(),
subject: $form.find('#subject').val(),
phone: $form.find('#phone').val(),
message: $form.find('#message').val()
I am having difficulty with my php code. From my tired eyes, the code is correct, and I've had multiple others look at the code. No one can figure out why it's not working. It must be something quite simple, but I cannot get the contact form to send.
PHP script:
<?php
$from = 'email#example.com';
$sendTo = 'email#example.com';
$subject = 'New message from contact form';
$fields = array('name' => 'Name', 'surname' => 'Surname', 'phone' => 'Phone', 'email' => 'Email', 'message' => 'Message');
$htmlHeader = '';
$htmlFooter = '';
$okMessage = 'Contact form succesfully submitted. Thank you, We will get back to you soon!';
$htmlContent = '<h1>New message from contact form</h1>';
use Nette\Mail\Message,
Nette\Mail\SendmailMailer;
require 'php/Nette/nette.phar';
$configurator = new Nette\Configurator;
$configurator->setTempDirectory(__DIR__ . '/php/temp');
$container = $configurator->createContainer();
$httpRequest = $container->getService('httpRequest');
$httpResponse = $container->getService('httpResponse');
$post = $httpRequest->getPost();
if ($httpRequest->isAjax()) {
$htmlContent .= '<table>';
foreach ($post as $key => $value) {
if (isset($fields[$key])) {
$htmlContent .= "<tr><th>$fields[$key]</th><td>$value</td></tr>";
}
}
$htmlContent .= '</table>';
$htmlBody = $htmlHeader . $htmlContent . $htmlFooter;
$mail = new Message;
$mail->setFrom($from)
->addTo($sendTo)
->setSubject($subject)
->setHtmlBody($htmlBody, FALSE);
$mailer = new SendmailMailer;
$mailer->send($mail);
$responseArray = array('type' => 'success', 'message' => $okMessage);
$httpResponse->setCode(200);
$response = new \Nette\Application\Responses\JsonResponse($responseArray);
$response->send($httpRequest, $httpResponse);
}
Contact Form HTML:
<div class="section contact soepa" id="contact" data-animate="bounceIn">
<div class="container">
<div class="col-md-12">
<h2 class="title"><span style="color: #f46b01;">Connect With Us</span></h2>
<div class="row">
<div class="col-md-8 col-md-offset-2">
<form id="contact-form" method="post" action="contact.php">
<div class="messages">
</div>
<div class="controls">
<div class="row">
<div class="col-md-6">
<input type="text" name="name" class="form-control" placeholder="Your firstname *" required="required">
</div>
<div class="col-md-6">
<input type="text" name="surname" class="form-control" placeholder="Your lastname *" required="required">
</div>
<div class="col-md-6">
<input type="text" name="email" class="form-control" placeholder="Your email *" required="required">
</div>
<div class="col-md-6">
<input type="text" name="phone" class="form-control" placeholder="Your phone *" required="required">
</div>
<div class="col-md-12">
<textarea name="message" class="form-control" placeholder="Message *" rows="4" required="required"></textarea>
</div>
<div class="col-md-12 text-center">
<input type="submit" class="btn btn-primary btn-lg" value="Send message">
</div>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>