I am having trouble with my contact form (from bootstrap). the php code as well as the html code are as seen below. Whenever I try the contact form, the body will be empty. Am I missing anything?
This is the html code:
<form id="main-contact-form" class="contact-form" name="contact-form" method="post" action="sendemail.php">
<div class="row-fluid">
<div class="span5">
<label>First Name</label>
<input type="text" class="input-block-level" required="required" placeholder="Your First Name">
<label>Last Name</label>
<input type="text" class="input-block-level" required="required" placeholder="Your Last Name">
<label>Email Address</label>
<input type="text" class="input-block-level" required="required" placeholder="Your email address">
</div>
<div class="span7">
<label>Message</label>
<textarea name="message" id="message" required="required" class="input-block-level" rows="8"></textarea>
</div>
</div>
<button type="submit" class="btn btn-primary btn-large pull-right">Send Message</button>
<p> </p>
</form>
This is the PHP code:
<?php
header('Content-type: application/json');
$status = array(
'type'=>'success',
'message'=>'Email sent!'
);
$name = #trim(stripslashes($_POST['name']));
$email = #trim(stripslashes($_POST['email']));
$subject = #trim(stripslashes($_POST['subject']));
$message = #trim(stripslashes($_POST['message']));
$email_from = $email;
$email_to = 'mail#luckystarmaids.com';
$body = 'Name: ' . $name . "\n\n" . 'Email: ' . $email . "\n\n" . 'Subject: ' . $subject . "\n\n" . 'Message: ' . $message;
$success = #mail($email_to, $subject, $body, 'From: <'.$email_from.'>');
echo json_encode($status);
die;
Your name and email form elements don't have name attributes. Without them, they won't be posted to the form's action1, 2.
Add names to your inputs:
<input type="text" name="first_name" class="input-block-level" required="required" placeholder="Your Last Name">
Also you will need to handle first and last name in your PHP code, at the moment you're only looking for name.
<form id="main-contact-form" class="contact-form" name="contact-form" method="post" action="">
<div class="row-fluid">
<div class="span5">
<label>First Name</label>
<input name=firstname type="text" class="input-block-level" required="required" placeholder="Your First Name">
<label>Last Name</label>
<input name=lastname type="text" class="input-block-level" required="required" placeholder="Your Last Name">
<label>Email Address</label>
<input name=email type="text" class="input-block-level" required="required" placeholder="Your email address">
<label>Subject</label>
<input name=subject type="text" class="input-block-level" required="required" placeholder="Subject">
</div>
<div class="span7">
<label>Message</label>
<textarea name="message" id="message" required="required" class="input-block-level" rows="8"></textarea>
</div>
</div>
<button type="submit" class="btn btn-primary btn-large pull-right">Send Message</button>
<p> </p>
</form>
Try also to handle the data on the server. Check at least if the values are send:
<?php
if ( isset( $_POST['firstname'] )
&& isset( $_POST['email'] )
&& isset( $_POST['subject'] )
&& isset( $_POST['message'] ) )
{
$name = #trim(stripslashes($_POST['name']));
$email = #trim(stripslashes($_POST['email']));
$subject = #trim(stripslashes($_POST['subject']));
$message = #trim(stripslashes($_POST['message']));
$email_from = $email;
$email_to = 'mail#luckystarmaids.com';
$body = 'Name: ' . $name . "\n\n" . 'Email: ' . $email . "\n\n" . 'Subject: ' . $s
ubject . "\n\n" . 'Message: ' . $message;
$success = #mail($email_to, $subject, $body, 'From: <'.$email_from.'>');
if ($success)
{
echo json_encode(array(
'success'=> true,
'message'=>'Email sent, you recieve an email at ' . $email
));
}
}
else
{
echo json_encode(array(
'success' => false,
'message' => 'An error has occured, please try again.'
));
}
?>
Related
When I try to click sent button, the content from the webpage is not redirecting to the .php page.I am using recaptcha in the form .Can you please help me to solve this issue..
my HTML code is:
<form action="sendform.php" id="contact-form" class="form-horizontal"
method="post">
<fieldset>
<div class="form-group">
<label class="col-sm-4 control-label" for="name">Your Name</label>
<div class="col-sm-8">
<input type="text" placeholder="Your Name" class="form-control" name="name" id="name">
</div>
</div>
<div class="form-group">
<label class="col-sm-4 control-label" for="email">Email Address</label>
<div class="col-sm-8">
<input type="text" placeholder="Enter Your Email Address" class="form-control" name="email" id="email">
</div>
</div>
<div class="form-group">
<label class="col-sm-4 control-label" for="subject">Subject</label>
<div class="col-sm-8">
<input type="text" placeholder="Subject" class="form-control" name="subject" id="subject" list="exampleList">
<datalist id="exampleList" >
<option value="a">A</option>
<option value="b">B Combo</option>
</datalist>
</div>
</div>
<div class="form-group">
<label class="col-sm-4 control-label" for="message">Your Message</label>
<div class="col-sm-8">
<textarea placeholder="Please Type Your Message" class="form-control" name="message" id="message" rows="3"></textarea>
</div>
</div>
<div class="col-sm-8" class="form-group" class="g-recaptcha" data-sitekey="xxxxxxyyyyyyy"></div>
<div class="col-sm-offset-4 col-sm-8">
<button type="submit" value="Send" id="submit" name="submit" class="submit_btn float_l">Submit</button>
<button type="reset" class="btn btn-primary">Cancel</button>
</div>
</fieldset>
</form>
And my PHP Code sendform.php
<?php
if (isset($_POST['submit']) && !empty($_POST['submit'])):
if (isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response'])):
//your site secret key
$secret = 'xxxxxxxxxxxxx';
//get verify response data
$verifyResponse = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret=' . $secret . '&response=' . $_POST['g-recaptcha-response']);
$responseData = json_decode($verifyResponse);
if ($responseData->success):
$to = "aaa#abc.com"; // this is your Email address
$from = !empty($_POST['email']) ? $_POST['email'] : ''; // this is the sender's Email address
$name = !empty($_POST['name']) ? $_POST['name'] : '';
$subject = !empty($_POST['subject']) ? $_POST['subject'] : '';
$subject2 = "Copy of your form submission";
$message = $first_name . " " . $last_name . " wrote the following:" . "\n\n" . $_POST['message'];
$message2 = "Here is a copy of your message " . $first_name . "\n\n" . $_POST['message'];
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers = "From:" . $from;
$headers .= 'From:' . $name . ' <' . $from . '>' . "\r\n";
$headers2 = "From:" . $to;
mail($to, $subject, $message, $headers);
$succMsg = 'Your request have submitted successfully.';
else:
$errMsg = 'Robot verification failed, please try again.';
endif;
else:
$errMsg = 'Please click on the reCAPTCHA box.';
endif;
else:
$errMsg = '';
$succMsg = '';
endif;
?>
I have tested your code and it works.
Please make sure that you have placed your html and php files in same directory and also your files should be served via a local running server.
So your url should look like this http://localhost/testing/index.html
Although, your sendform.php gives me captcha error ofcourse.
"Please click on the reCAPTCHA box."
As stated in the title, my webform is not returning the user information in the email, although the field labels are being returned.
I think this is an issue where the HTML is not structured to relate back to the php field name, but I am not sure how to format that.
Can anyone set me on the path?
This is the HTML:
<form id="main-contact-form" class="contact-form" name="contact-form" method="post" action="sendemail.php">
<div class="row-fluid">
<div class="span5">
<label>First Name</label>
<input type="text" class="input-block-level" required="required" placeholder="Your First Name">
<label>Last Name</label>
<input type="text" class="input-block-level" required="required" placeholder="Your Last Name">
<label>Email Address</label>
<input type="text" class="input-block-level" required="required" placeholder="Your email address">
</div>
<div class="span7">
<label>Message</label>
<textarea name="message" id="message" required class="input-block-level" rows="8"></textarea>
</div>
</div>
<button type="submit" class="btn btn-primary btn-large pull-right">Send Message</button>
</form>
And this is the php:
<?php
header('Content-type: application/json');
$status = array(
'type'=>'success',
'message'=>'Email sent!'
);
$name = #trim(stripslashes($_POST['name']));
$email = #trim(stripslashes($_POST['email']));
$subject = #trim(stripslashes($_POST['subject']));
$message = #trim(stripslashes($_POST['message']));
$email_from = $email;
$email_to = 'Example#email.com';
$body = 'Name: ' . $name . "\n\n" . 'Email: ' . $email . "\n\n" . 'Subject: ' . $subject . "\n\n" . 'Message: ' . $message;
$success = #mail($email_to, $subject, $body, 'From: <'.$email_from.'>');
echo json_encode($status);
die;
Firstly, your inputs does not have name attributes:
<input type="text" class="input-block-level" required="required" name="first_name" placeholder="Your First Name">
<input type="text" class="input-block-level" required="required" name="last_name" placeholder="Your Last Name">
<input type="text" class="input-block-level" required="required" name="email" placeholder="Your email address">
Secondly, you need to use <input> instead of <button>. Otherwise, your form will not be submitted.
<input type="submit" class="btn btn-primary btn-large pull-right">Send Message</input>
Thirdly, you need to check if the form is submitted by using isset() function:
// will return true if the form is submitted
if(isset($_POST['submit']){
header('Content-type: application/json');
// other parts of your code
}
Lastly, you shouldn't suppress the error messages by removing the # as they are very useful in helping you to debug your errors, especially when you have a syntax error.
You need to give your inputs a name attribute, such as:
<input type="text" name="name" class="input-block-level" required="required" placeholder="Your First Name" />
Then you will be able to access the $_POST data as you currently are. Check out the name attribute docs
I am working on my personal website at the moment, and I have done everything, except when a message comes through from a user via the contact form, and email is successfully sent to me, but the content contained within said email is blank.
My code is below:
PHP (sendmail.php)
<?php
header('Content-type: application/json');
$status = array(
'type'=>'success',
'message'=>'Your email has been succesfully sent.'
);
$name = #trim(stripslashes($_POST['name']));
$email = #trim(stripslashes($_POST['email']));
$subject = #trim(stripslashes($_POST['subject']));
$message = #trim(stripslashes($_POST['message']));
$email_from = $email;
$email_to = 'me#joshuaquinlan.co.uk';
$body = 'Name: ' . $name . "\n\n" . 'Email: ' . $email . "\n\n" . 'Subject: ' . $subject . "\n\n" . 'Message: ' . $message;
$success = #mail($email_to, $subject, $body, 'From: <'.$email_from.'>');
echo json_encode($status);
die;
HTML (contact-us.html):
<form id="main-contact-form" class="contact-form" name="contact-form" method="post" action="sendmail.php" role="form">
<div class="row">
<div class="col-sm-5">
<div class="form-group">
<input type="text" name="name" class="form-control" required="required" placeholder="Full Name">
</div>
<div class="form-group">
<input type="text" name="email" class="form-control" required="required" placeholder="Email address">
</div>
<div class="form-group">
<input type="text" name="subject" class="form-control" required="required" placeholder="Subject">
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary btn-lg">Send Message</button>
</div>
</div>
<div class="col-sm-7">
<textarea name="message" id="message" required="required" class="form-control" rows="8" placeholder="Message"></textarea>
</div>
</div>
When the email is actually sent, however, it actually shows up like
this
Please, if someone can help me, then thank you!
Your issue is you are not sending anything from your form:
$.post($(this).attr('action'),function(data){$this.prev().text(data.message) // ...
You forgot to add the data:
$.post($(this).attr('action'),$(this).serialize(), function(data){$this.prev().text(data.message) // ...
Beautified Code
// You forgot to add data here v----
$.post($(this).attr('action'), function(data) {
$this.prev().text(data.message).fadeIn().delay(3000).fadeOut();
}, 'json');
So correct it to:
// added data using serialize() vvvvvvvvvvvvvvvvvv
$.post($(this).attr('action'), $(this).serialize(), function(data) {
$this.prev().text(data.message).fadeIn().delay(3000).fadeOut();
}, 'json');
Find and Replace
Press Ctrl + H (Replace).
Paste this in Find For: $.post($(this).attr('action'),func.
Paste this in Replace With: $.post($(this).attr('action'),$(this).serialize(),func.
Press Replace.
See below screenshot for more details.
Screenshot
I am working on a simple contact form however when the email comes through the fields are empty. The form sends fine but it doesn't get any of the values. My form HTML looks like this.
<form id="main-contact-form" class="contact-form" name="contact-form" method="post" action="sendemail.php">
<input type="text" name="namefirst" id="namefirst" class="input-block-level" required="required" placeholder="Your First Name">
<input type="text" name="namefirst" id="namefirst" class="input-block-level" required="required" placeholder="Your First Name">
<input type="text" name="email" id="email" class="input-block-level" required="required" placeholder="Your email address">
<textarea name="message" id="message" required="required" class="input-block-level" rows="8"></textarea>
<button type="submit" class="btn btn-primary btn-large pull-right">Send Message</button>
The PHP side of things is as follows
header('Content-type: application/json');
$status = array(
'type'=>'success',
'message'=>'Email sent!'
);
$nameFirst = $_POST['namefirst'];
$nameLast = $_POST['namelast'];
$email = $_POST['email'];
$message = $_POST['message'];
$subject = $message;
$email_from = $email;
$email_to = 'jdwman14#gmail.com';
$body = 'Name: ' . $nameFirst .' '. $nameLast. "\n\n" . 'Email: ' . $email . "\n\n" . 'Subject: ' . $subject . "\n\n" . 'Message: ' . $message;
$success = #mail($email_to, $subject, $body, 'From: <'.$email_from.'>');
echo json_encode($status);
die;
When the email comes through it has this format but has none of the variables.
Name:
Email:
Subject:
Message:
There is issue in Form,
There are two time namefirst input field in your form
<input type="text" name="namefirst" id="namefirst" class="input-block-level" required="required" placeholder="Your First Name">
replace namefirst with lastname in name & id attribute value in second input as following,
<input type="text" name="namelast" id="namelast" class="input-block-level" required="required" placeholder="Your Last Name">
I hope this solve your issue...
I cant get this to work properly. I know its something stupid but I cant find it.
<?php
$mailto = "stormygurl73#yahoo.com";
$name = ucwords($_POST['name']);
$subject = $_POST['Contact form'];
$email = $_POST['email'];
$message = $_POST['message'];
if(strlen($_POST['name']) < 1 ){
echo 'email_error';
}
else if(strlen($email) < 1 ) {
echo 'email_error';
}
else if (!eregi("^[A-Z0-9._%-]+#[A-Z0-9._%-]+\.[A-Z]{2,4}$", $email)) {
echo 'email_error';
}
else if(strlen($message) < 1 ){
echo 'email_error';
} else {
// NOW SEND THE ENQUIRY
$email_message="\n\n" .
"Name : " .
ucwords($name) .
"\n" .
"Email : " .
$email .
"\n\n" .
"Message : " .
"\n" .
$message .
"\n" .
"\n\n" ;
$email_message = trim(stripslashes($email_message));
mail($mailto, $subject, $email_message, "From: \"$vname\" <".$email.">\nReply-To: \"".ucwords($name)."\" <".$email.">\nX-Mailer: PHP/" . phpversion() );
}
?>
Html
<div class="wrapper">
<div id="main" style="padding:50px 0 0 0;">
<form id="contact-form" action="sendemail.php" method="post">
<h3>Get in touch</h3>
<h4>Fill in the form below, and we'll get back to you within 24 hours.</h4>
<div>
<label>
<span>Name: (required)</span>
<input placeholder="Please enter your name" type="text" tabindex="1" required autofocus>
</label>
</div>
<div>
<label>
<span>Email: (required)</span>
<input placeholder="Please enter your email address" type="email" tabindex="2" required>
</label>
</div>
<div>
<label>
<span>Message: (required)</span>
<textarea placeholder="Include all the details you can" tabindex="5" required></textarea>
</label>
</div>
<div>
<button name="submit" type="submit" id="contact-submit">Send Email</button>
</div>
</form>
<!-- /Form -->
Any help would be awesome!
You must have the name attribute on your input-elements. The first one should have
name="name"
and so on. You can read more about it here: Variables From External Sources : HTML Forms (GET and POST)
It looks like you've missed the attribute name name='somename' everywhere in your form fields. e.g.
This is what you have
<input placeholder="Please enter your name" type="text" tabindex="1" required autofocus>
This is what it should have been
<input name="name" placeholder="Please enter your name" type="text" tabindex="1" required autofocus>