I want to echo message when form is submitted - php

I'm not very good at PHP (more of a front-end developer) and I'm doing a website for a client. I got everything working up to now except for one thing. When the user fills out the contact form and submits it successfully i want to display a message that tells them the email was sent. I already have the div set up I just don't know who to execute it with PHP.
<div class="sent">Email has been sent.</div>
<form action="" method="POST" class="contact-form">
<img class="close" src="images/close-icon.png">
<div class="top-title">Contact Us</div>
<input type="hidden" name="action" value="submit">
<div class="input-title">Name*</div>
<input class="input" name="name" type="text" value="" size="30" required><br>
<div class="input-title">Phone Number*</div>
<input class="input" name="phone" type="text" value="" size="30" required><br>
<div class="input-title">Email Address*</div>
<input class="input" name="email" type="text" value="" size="30" required><br>
<div class="input-title">How did you hear about us?</div>
<input class="input" name="company" type="text" value="" size="30"/><br>
<div class="input-title">Let us know how we can help you</div>
<textarea class="textarea" name="message" rows="7" cols="30"></textarea><br>
<div class="required">*Fill is required</div>
<input class="submit-button" type="submit" value="Send Email"/>
</form>
<?php
{
$name=$_POST['name'];
$email=$_POST['email'];
$phone=$_POST['phone'];
$company=$_POST['company'];
$message= "\r\nName: " . $_POST['name'] ."\r\nPhone Number: ". $_POST['phone']."\r\nEmail: ". $_POST['email'] ."\r\nHow you know the company: ".$_POST['company'] ."\r\nReason for message: ".$_POST['message'];
$from="From: $name<$email>\r\nReturn-path: $email";
$subject="GS Website Message";
mail("name#email.com", $subject, $message, $from);
}
?>

Change: (and using a conditional statement)
mail("name#email.com", $subject, $message, $from);
to:
if(mail("name#email.com", $subject, $message, $from)){
$sent = "Message sent";
}
Then modify:
<div class="sent">Email has been sent.</div>
to read as:
<div class="sent"><?php if(!empty($sent)) { echo $sent; } ?></div>
Seeing you're new to PHP/forms, it's best to also check if your inputs are empty or not, otherwise you risk in either not getting mail, or receiving empty data.
http://php.net/manual/en/function.empty.php
Set inside a conditional statement also.
I.e.: using NOT empty: ! is the NOT operator in PHP.
if(!empty($_POST['var'])){...}
Edit:
The logic needs to be reversed.
First, place your PHP above your HTML form.
I also added a submit name attribute to your submit button, with a conditional statement and a ternary operator in the div.
<?php
if(isset($_POST['submit'])){
$name=$_POST['name'];
$email=$_POST['email'];
$phone=$_POST['phone'];
$company=$_POST['company'];
$message= "\r\nName: " . $_POST['name'] ."\r\nPhone Number: ". $_POST['phone']."\r\nEmail: ". $_POST['email'] ."\r\nHow you know the company: ".$_POST['company'] ."\r\nReason for message: ".$_POST['message'];
$from="From: $name<$email>\r\nReturn-path: $email";
$subject="GS Website Message";
if(mail("email#example.com", $subject, $message, $from)){
$sent = "Message sent";
}
}
?>
<div class="sent"><?php echo isset($sent) ? $sent : ''; ?></div>
<form action="" method="POST" class="contact-form">
<img class="close" src="images/close-icon.png">
<div class="top-title">Contact Us</div>
<input type="hidden" name="action" value="submit">
<div class="input-title">Name*</div>
<input class="input" name="name" type="text" value="" size="30" required><br>
<div class="input-title">Phone Number*</div>
<input class="input" name="phone" type="text" value="" size="30" required><br>
<div class="input-title">Email Address*</div>
<input class="input" name="email" type="text" value="" size="30" required><br>
<div class="input-title">How did you hear about us?</div>
<input class="input" name="company" type="text" value="" size="30"/><br>
<div class="input-title">Let us know how we can help you</div>
<textarea class="textarea" name="message" rows="7" cols="30"></textarea><br>
<div class="required">*Fill is required</div>
<input class="submit-button" type="submit" value="Send Email" name="submit"/>
</form>

$password=md5($_POST ['password']);
$password3 =md5($_POST ['password2']);
if($password != $password3)
{
$message = "Passwords do not match";
}
etc.
you need add
<form>
<div class="form-group">
<input type="password" name="password" id="password" class="form-control" placeholder="Password *" value="" required/>
</div>
<div class="form-group">
<div class="message"><?php if(!empty($message)) { echo $message; } ?></div>
<input type="password" name="password2" class="form-control" id="password2" placeholder="Confirm Password *" value="" required/>
</div>
</form>

Related

Question about contact form HTTP ERROR 405 crash

so I am trying to make a link to contact form but it seems to be crashing and I do not know why is this happening. I double checked the link and variable name, but seems like error 405 happens without stopping every time I click.
This is my html part
<h3>Contact Form</h3>
<div class="containerofcontact" style="text-align: left !important;">
<form method="POST" action="contactform.php">
<label for="name">Name</label>
<input type="text" id="Name" name="Name" placeholder="Your name..">
<label for="email">Email</label>
<input type="text" id="Email" name="Email" placeholder="Your Email">
<label for="subject">Subject</label>
<textarea id="Subject" name="Subject" placeholder="Place Detail of Concern" style="height:200px"></textarea>
<input type="submit" value="Send">
</form>
</div>
This is php part
<?php
$name = $_POST['Name'];
$email = $_POST['Email'];
$text = $_POST['Subject'];
$mailTo = "blankforshare";
$headers = "From: ".$email;
$txt = "You have received an e-mail from ".$name."\n\n".$text;
mail($mailTo, $headers, $txt) or die("Error!");
header("Location: index.html");
?>
Thank you for the help.

Email PHP form doesn't work [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 5 years ago.
I haven't written code in years, so I am going off old code from a project that is over 5 years old, so I am not surprised that it doesn't work; I would like some pointers on how to make it work, please.
Here is what I have in my HTML email form --
<form action="fydcontact.php" method="post">
<div class="form-group">
<!--<label for="contact_name">Name:</label>-->
<input type="text" id="contact_name" class="form-control" placeholder="Name" />
</div>
<div class="form-group">
<!--<label for="contact_email">Email:</label>-->
<input type="text" id="contact_email" class="form-control" placeholder="Email Address" />
</div>
<div class="form-group">
<!--<label for="contact_message">Message:</label>-->
<textarea id="contact_message" class="form-control" rows="9" placeholder="Write a message"></textarea>
</div>
<button type="submit" class="btn btn-primary">Send</button>
</form>
and here is what my PHP looks like --
<?php
if(isset($_POST['send'])) {
// Prepare the email
$to = ''foryourdayformals#gmail.com ';
$name = $_POST['contact_name'];
$mail_from = $_POST['contact_email'];
$subject = 'Message sent from website';
$message = $_POST['contact_message'];
$header = "From: $name <$mail_from>";
// Send it
$sent = mail($to, $subject, $message, $header);
if($sent) {
echo 'Your message has been sent successfully! Return to For Your Day, LLC Website';
} else {
echo 'Sorry, your message could not send. Please use direct email link on Contact Us page (below the map). Return to For Your Day, LLC Website';
}
}
?>
Any help is GREATLY appreciated! Thanks!
update
$to = ''foryourdayformals#gmail.com ';
to
$to = 'foryourdayformals#gmail.com';
and need to add name attribute for form . This update your form
<form action="fydcontact.php" method="post">
<div class="form-group">
<!--<label for="contact_name">Name:</label>-->
<input type="text" id="contact_name" name="contact_name" class="form-control" placeholder="Name" />
</div>
<div class="form-group">
<!--<label for="contact_email">Email:</label>-->
<input type="text" id="contact_email" name="contact_email" class="form-control" placeholder="Email Address" />
</div>
<div class="form-group">
<!--<label for="contact_message">Message:</label>-->
<textarea id="contact_message" name="contact_message" class="form-control" rows="9" placeholder="Write a message"></textarea>
</div>
<button type="submit" class="btn btn-primary" name ="send">Send</button>
</form>

My php email form is not returning the data entered by the user

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

if isset Not Writing To Database

I have an if isset statement that fires when my site receives approval from my merchant account api after a form is submitted. Everything seems to work except for the order details from the form getting written to my database. Here is the code for my form:
<form id="input-form" class="form-inline" action="" method="post">
<?php
if($error <> '') {
echo '<div class="alert alert-danger">',$error,'</div>';
}
?>
<div id="makepayment">
<div class="form-group">
<label for="order_fname">First Name</label>
<input class="form-control" style="width: 273px;" type="text" name="order_fname" id="order_fname" value="<?php echo $user['user_fname']; ?>" placeholder="First Name" />
</div>
<div class="form-group">
<label for="order_lname">Last Name</label>
<input class="form-control" style="width: 273px;" type="text" name="order_lname" id="order_lname" value="<?php echo $user['user_lname']; ?>" placeholder="Last Name" />
</div>
<div class="form-group">
<label for="order_phone">Primary Phone</label>
<input class="form-control" style="width: 273px;" type="text" name="order_phone" id="order_phone" value="<?php echo $user['user_phone']; ?>" placeholder="Phone Number" />
</div>
<div class="form-group">
<label for="order_cell">Cell Phone</label>
<input class="form-control" style="width: 273px;" type="text" name="order_cell" id="order_cell" value="<?php echo $user['user_cell']; ?>" placeholder="Cell Phone" />
</div>
<div class="form-group">
<label for="order_email">Email</label>
<input class="form-control" style="width: 273px;" type="text" name="order_email" id="order_email" value="<?php echo $user['user_email']; ?>" placeholder="Email Address" />
</div><br/>
<div class="clear"></div>
<div class="form-group" style="width:100% !important">
<label for="company_id">Property Information</label><br/>
<br/>
<input id="company_id" type="hidden" id="company_id" name="company_id" value="<?php echo $company['company_id']; ?>" />
<input id="company_name" type="hidden" id="company_name" name="company_name" value="<?php echo $company['company_name']; ?>" />
<div class="property">
<strong><?php echo $company['company_name']; ?></strong><br/>
<?php echo $company['company_street']; ?><br/>
<?php echo $company['company_city']; ?>, <?php echo $company['company_state']; ?> <?php echo $company['company_zip']; ?>
</div>
<div class="clear"></div>
</div><br/>
<div class="clear"></div>
<div class="form-group">
<label for="order_cc">Card Number</label>
<input class="form-control" type="text" name="ssl_card_number" id="ssl_card_number" style="width:100% !important" />
</div><br/>
<div class="clear"></div>
<div class="form-group">
<label for="order_code">Security Code</label>
<input class="form-control" style="width: 75px;" type="text" name="order_code" id="order_code" placeholder="ex:123" />
</div>
<div class="form-group">
<label for="order_customernumber">Customer Number</label>
<input class="form-control" type="text" style="width: 75px;" name="order_customernumber" id="order_customernumber" />
</div>
<div class="form-group">
<label for="order_invoicenumber">Invoice Number</label>
<input class="form-control" type="text" style="width: 75px;" name="order_invoicenumber" id="order_invoicenumber" />
</div>
<div class="form-group">
<label for="order_amount">Payment Amount</label>
$ <input class="form-control" type="text" style="width: 100px" name="ssl_amount" id="ssl_amount" />
</div>
<div class="form-group" style="width:100% !important">
<input class="btn btn-primary" type="submit" name="submit" value="Submit" />
</div>
</div>
</form>
Here is my if isset statement that fires if the api sends back an approval code. I know that part works because it does this action when I put in my card info and throws the proper error when I put in fake info.
if(isset($response['ssl_result_message']) && $response['ssl_result_message'] == "APPROVAL") {
//If transaction successful write to database
$userid = $session['user_id'];
$addOrderSQL = "
INSERT INTO orders (
user_id,
company_id,
order_fname,
order_lname,
order_phone,
order_cell,
order_email,
company_name,
order_customernumber,
order_invoicenumber,
order_amount
)
VALUES (
'$userid',
'$_POST[company_id]',
'$_POST[order_fname]',
'$_POST[order_lname]',
'$_POST[order_phone]',
'$_POST[order_cell]',
'$_POST[order_email]',
'$_POST[company_name]',
'$_POST[order_customernumber]',
'$_POST[order_invoicenumber]',
'$_POST[ssl_amount]'
)";
$firstname= $_POST['order_fname'];
$lastname = $_POST['order_lname'];
$invoice = $_POST['order_invoicenumber'];
$customernum = $_POST['order_customernumber'];
$property = $_POST['company_name'];
$ordertotal = $_POST['ssl_amount'];
/* -------------------------------------------------
Send Email to Administrator
------------------------------------------------- */
$to = "web#abcprintingink.com";
$subject = "Payment for Customer # ". $customernum;
$headers = "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "From: webmaster#turfmastersct.com" . "\r\n" .
"Reply-To: webmaster#example.com" . "\r\n" .
"X-Mailer: PHP/" . phpversion();
$message = "<p>A customer has completed a payment. Here are the payment details:</p>
Customer Name: ". $firstname ." ". $lastname ."<br/>
Customer #: ". $customernum ."<br/>
Invoice #: ". $invoice ."<br/>
Property: ". $property ."<br/>
Payment Amount: $". $ordertotal;
// Send it.
mail($to, $subject, $message, $headers);
//once executed queries redirect user
header('Location: /payments/orders.php');
exit;
}
elseif(isset($response['errorCode'])) {
$error = 'Your credit card has been declined';
}
else {
$error = 'Failed to process your payment';
}
The form is communicating with the api, I get a response from it and I can see the payment details post to my demo account. The isset statement fires, I get the email that it sends and it redirects me to the orders.php page like it's supposed to. It just doesn't write to the database. You can see my form and you can see the fields I am writing to my database. All those fields exist in the database itself. Am I missing something here?
You never run the $addOrderSQL query.
See mysqli_query() to do this.

PHP mail is not sending email to my email address

I've been looking around everywhere and cannot seem to find how to make this work - it is simply not sending an email to my address. Here is my HTML form:
<form id="contactMe" name="contact" method="post" novalidate="novalidate">
<fieldset>
<label for="name" id="name">Name<span class="required">*</span></label>
<input type="text" name="name" id="name" size="30" value="" required="">
<label for="email" id="email">Email<span class="required">*</span></label>
<input type="text" name="email" id="email" size="30" value="" required="">
<label for="phone" id="phone">Phone</label>
<input type="text" name="phone" id="phone" size="30" value="">
<label for="Message" id="message">Message<span class="required">*</span></label>
<textarea name="message" id="message" required=""></textarea>
<label for="Answer" id="answer">Name the house pet that says “<i>woof</i>“<span class="required">*</span></label>
<input type="text" name="answer" value="" required=""></br>
<input id="submit" type="submit" name="submit" value="Send">
</fieldset>
<div id="success">
<span class="green textcenter">
<p>Your message was sent successfully! I will be in touch as soon as I can.</p>
</span>
</div> <!-- closes success div -->
<div id="error">
<span><p>Something went wrong, try refreshing and submitting the form again.</p></span>
</div> <!--close error div-->
</form>
and here is my PHP saved as mailer.php:
<?php
$to = "someone#gmail.com";
$from = $_REQUEST['email'];
$name = $_REQUEST['name'];
$headers = "From: $from";
$subject = "You have a message from your.";
$fields = array();
$fields{"name"} = "name";
$fields{"email"} = "email";
$fields{"phone"} = "phone";
$fields{"message"} = "message";
$body = "Here is what was sent:\n\n"; foreach($fields as $a => $b){ $body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]); }
mail("maytee.kneitz#gmail.com",$subject,$message,"From: $from\n");
echo 'Mail sent';
?>
This is my first shot at working on a mailer / contact form, so sorry if it's a blatant problem. I just can't seem to find it. Any guidance would be appreciated.
I do have validation in my scripts (not posted here).
You don't have a form action defined. Try this:
<form id="contactMe" name="contact" method="post" action="mailer.php" novalidate="novalidate">
By default, the form will be submitted to its current location unless otherwise specified. In your case, point it to wherever your mail script is located

Categories