my form is not sending after adding functions from php [duplicate] - php

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 1 year ago.
Soi tried creating a php form and it seems not to be working, it is not sending and no action is occuring. the code below is that if the HTML
<form id="form" class="vbottom-desktop grid default-form no-spacing lined-form mb-xl" action="php\mail.php" method="post">
<div class="col-2">
<input required type="text" placeholder="Name" name="name" class="form-control">
</div>
<div class="col-2">
<input required type="email" placeholder="Email address" name="email" class="form-control">
</div>
<div class="col-2">
<textarea required placeholder="Message" name="message" class="small form-control"></textarea>
</div>
<div class="col-2">
<input id="send" type="submit" value="Send" class="btn btn-primary">
</div>
</form>
and below this is that of the php
<?php
$subject = 'You have received this businessmail from';
$to = 'okonodestine99#gmail.com';
$headers = 'MIME-Version: 1.0' . "\r\n" .
'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$message = '';
if (!empty($_POST["name"])) {
$message .= 'Name: ' . $_POST['name'] . ' <br/>';
}
if (!empty($_POST["email"])) {
$message .= 'Email: ' . $_POST['email'] . ' <br/>';
}
if (!empty($_POST["phone"])) {
$message .= 'Phone: ' . $_POST['phone'] . ' <br/>';
}
if (!empty($_POST["website"])) {
$message .= 'Website: ' . $_POST['website'] . ' <br/>';
}
if (!empty($_POST["message"])) {
$message .= 'Message: ' . $_POST['message'] . ' <br/>';
}
if (#mail($to, $subject, $message, $headers))
{
echo 'sent';
}
else
{
echo 'failed';
}
?>
any possible solution to this would be highly appreciated

you should use slash not backslash in url!
try this:
<form id="form" class="vbottom-desktop grid default-form no-spacing lined-form mb-xl" action="php/mail.php" method="post">
<div class="col-2">
<input required type="text" placeholder="Name" name="name" class="form-control">
</div>
<div class="col-2">
<input required type="email" placeholder="Email address" name="email" class="form-control">
</div>
<div class="col-2">
<textarea required placeholder="Message" name="message" class="small form-control"></textarea>
</div>
<div class="col-2">
<input id="send" type="submit" value="Send" class="btn btn-primary">
</div>
</form>
and make sure you put files like this:
root
|-- yourform.html(or .php whatever is that you're using)
|-- php
|-- mail.php

Related

Page is not redirecting to the php page from Html, when send button click in the web form?

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."

Send mail from HTML form with PHP backend

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

PHP Contact form empty body (bootstrap)

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.'
));
}
?>

php form not working

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>

Can't get my php form working, sends mail but doesn't run code on redirect page

This question gets asked all the time, but I can't figure out why mine isn't working. I have a form that redirects to itself. If PHP decides it is submitted, there is a success/failure message and it displays the user input as the default value and disables the fields: using phpinfo I can see that the form is being submitted, but this first conditional doesn't work. I've tried a couple of versions, but no luck. It's weird because it sends the email
Specifically, the result and disable functions don't display their code after the form has been sent.
<?php
function clean($data) {
$data = trim(stripslashes(strip_tags($data)));
return $data;
}
function result(){
if($sent) echo $result;
}
function disable($field){
if($sent){
if($field != null){
$ret .= $field . '", disabled, placeholder!="';
}
$ret .= '", disabled, placeholder!="';
echo $ret;
}
}
function option($item){
$ret = "<option>";
if($sent){
if($eventType == $item){
$ret = "<option selected>";
}
}
$ret .= $item . "</option>";
echo $ret;
}
if(isset($_POST['name'])){
$sent = TRUE;
$result = null;
$name = $_POST['name'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$eventDate = $_POST['eventDate'];
$eventTime = $_POST['eventTime'];
$eventLength = $_POST['eventLength'];
$eventLocation = $_POST['eventLocation'];
$eventType = $_POST['eventType'];
$message = $_POST['message'];
$recipient = "";
$subject = " Form Submission";
$mailheader = "From: \r\n";
$formcontents = "You received this e-mail message through your website: \n\n";
$formcontents .= "Name: " . clean($name) . "\r\n";
$formcontents .= "Phone: " . clean($phone) . "\r\n";
$formcontents .= "Email: " . clean($email) . "\r\n";
$formcontents .= "Event Date: " . clean($eventDate) . "\r\n";
$formcontents .= "Event Time: " . clean($eventTime) . "\r\n";
$formcontents .= "Event Length: " . clean($eventLength) . "\r\n";
$formcontents .= "Event Location: " . clean($eventLocation) . "\r\n";
$formcontents .= "Event Type: " . clean($eventType) . "\r\n";
$formcontents .= "Message: " . clean($message) . "\r\n";
$formcontents .= "\r\n";
$formcontents .= 'IP: '.$_SERVER['REMOTE_ADDR']."\r\n";
$formcontents .= 'Browser: '.$_SERVER['HTTP_USER_AGENT']."\r\n";
// Send mail
if(mail($recipient, $subject, $formcontents, $mailheader)){;
$result = '<h3 class="alert alert-success"> Thank you, your form was successfully sent and I will contact you shortly.</h3>';
} else {
$result = '<h3 class="alert alert-error"> Your mail could not be sent at this time.</h3>';
}
}
?>
<form action="contact.php" method="POST" class="form-horizontal span4">
<fieldset>
<legend>
<h2>Or send me a message. </h2>
</legend>
<p class="help-block">None of the fields are required, but the more information I have about your event, the more detailed I can be in my response.</p>
<legend class="help-block">Your Details</legend>
<div class="control-group">
<label for="name" class="control-label">Your Name</label>
<div class="controls">
<input id="name" type="text" name="name" placeholder="<?php disable($name); ?>" class="input-xlarge"/>
</div>
</div>
<div class="control-group">
<label for="phone" class="control-label">Your Contact Number</label>
<div class="controls">
<input id="phone" type="tel" name="phone" placeholder="<?php disable($phone); ?>" class="input-xlarge"/>
</div>
</div>
<div class="control-group">
<label for="email" class="control-label">Your Email</label>
<div class="controls">
<input id="email" type="email" name="email" placeholder="<?php disable($email); ?>" class="input-xlarge"/>
</div>
</div>
<legend class="help-block">Your Event </legend>
<div class="control-group">
<label for="eventDate" class="control-label">Your Event's Date</label>
<div class="controls">
<input id="eventDate" type="date" name="eventDate" placeholder="<?php disable($eventDate); ?>" class="input-xlarge"/>
</div>
</div>
<div class="control-group">
<label for="eventTime" class="control-label">Your Event's Start Time</label>
<div class="controls">
<input id="eventTime" type="time" name="eventTime" placeholder="<?php disable($eventTime); ?>" class="input-xlarge"/>
</div>
</div>
<div class="control-group">
<label for="eventLength" class="control-label">Your Event's Length</label>
<div class="controls">
<input id="eventLength" type="text" name="eventLength" placeholder="<?php disable($eventLength); ?>" class="input-xlarge"/>
</div>
</div>
<div class="control-group">
<label for="eventLocation" class="control-label">Your Event's Location</label>
<div class="controls">
<input id="eventLocation" type="text" name="eventLocation" placeholder="<?php disable($eventLocation); ?>" class="input-xlarge"/>
</div>
</div>
<div class="control-group">
<label for="eventType" class="control-label">What Kind of Event</label>
<div class="controls">
<select id="eventType" name="eventType" placeholder="<?php disable($eventType); ?>"><?php option("Charity Event"); option("Expo/Trade Show"); option("Personal Event"); option("Other"); ?></select>
</div>
</div>
<div class="control-group">
<label for="message" class="control-label">Other comments or the best time to reach you.</label>
<div class="controls">
<textarea id="message" name="message" rows="10" placeholder="<?php disable($message); ?>" class="input-xxlarge"></textarea>
</div>
</div>
<div class="form-actions">
<button type="submit" name="submit" placeholder="<?php disable(null); ?>" class="btn btn-primary">Send Message</button>
</div>
</fieldset>
</form>
You have to import your global variables into function scope, like:
function result(){
global $sent, $result;
if($sent) echo $result;
}
..in functions disable() and option(), too.
if(isset($_POST['name'])){
should be
if(isset($_POST['submit'])){

Categories