PHP success message on same page / styling - php

I am new to PHP but found a tutorial for a php contact form. the form works well except the 'your email was sent successfully' text shows on a new page.
Is there a way to make it so the text shows on the same page?
I am also unsure how I would go about styling the information?
thanks
<?php
$to = 'zoeharrisondesign#gmail.com';
$subject = 'New Message Recieved!';
$from = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$name = $_POST['name'];
//check honeypot
if( !empty( $honeypot ) ) {
echo 'There was a problem';
}
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Create email headers
$headers .= 'From: '.$from."\r\n".
'Reply-To: '.$from."\r\n" .
'X-Mailer: PHP/' . phpversion();
// Compose a simple HTML email message
$message = "$message \r\n |
From $name \r\n |
Tel: $phone";
// Sending email
if(mail($to, $subject, $message, $headers)){
echo 'Your mail has been sent successfully.';
} else{
echo 'Unable to send email. Please try again.';
}
?>
HTML
<form role="form" action="action.php" method="post" class="contact-form">
<div class="form-row"><input type="text" id="name" name="name" placeholder="name" required="required"></div>
<div class="form-row"><input type="email" id="email" name="email" placeholder="email" required="required"> </div>
<div class="form-row"><input type="tel" id="phone" name="phone" placeholder="phone"></div>
<div class="form-row"><textarea id="message" name="message" placeholder="message" required="required" style="height:200px"></textarea></div>
<div class="form-row" style="display:none;">
<input type="hidden" name="url" placeholder="URL"></div>
<div class="form-row"><input class="submit" type="submit" value="Send"></div>
</form>

An easy way of doint that is using sessions,
you need to create a session php file like just this:
SESSION.PHP:
ini_set('session.use_only_cookies', 1);
session_set_cookie_params(0,'/','localhost',false,false); // IF YOU ARE HOSTING IT IN LOCAL
session_start();
session_regenerate_id(); // REGENERATE SESSIONE FOR SECURITY ISSUES
MAIL PHP FILE:
// add this on top of your mail.php file
include 'session.php'; // just insert the path of the file
// AND CHANGE THIS
if(mail($to, $subject, $message, $headers)){
$_SESSION['error'] = 'Your mail has been sent successfully.'; // setting your session and value
header("Location: index.php"); // path of the file where you want to show the text
exit();
} else{
// same thing
}
INDEX.PHP: (just add this where you want to show the text, example:)
if(isset($_SESSION['error'])) {
echo '<h1>'.$_SESSION['error'].'</h1>';
unset($_SESSION['error']); // to display it just once and then delete it
}
IMPORTANT: you need to add also <?php include 's-session.php'; ?> to the top of your index.php file

In top of php code start the session`session_start();`
Insert this piece of code
if(mail($to, $subject, $message, $headers)){
$_SESSION['message'] = "Submitted Successfully";
}
Paste this code in html where you want to show the message
<?php if (isset($_SESSION['message'])): ?>
<div class="msg text-success" id="success" >
<?php
echo $_SESSION['message'];
unset($_SESSION['message']);
?>
</div>
<?php endif ?>

Related

How can we pass FORM data from included file to another page?

I had included footer.php in index file and the footer contains FORM for Subscriber Register.
when i click to submit a new subscriber email from https://mywebsite.com/index.php the value is not being passed from index.php page the same code is perfectly apply when i use url https://mywebsite.com/footer.php
footer.php
<form id="newsletterForm" action="newsletter-subscribe.php" method="POST">
<div class="input-group input-group-rounded">
<input name="mailid" type="email" placeholder="Email Address">
<span>
<input type="submit">
</span>
</div>
</form>
newsletter-subscribe.php
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$email = $_POST["mailid"];
$recipient = "info#myemail.com";
$email_content = "
<html>
<body>
<h2>Hello You had a new Subscriber!</h2>
<h3>Email Id : ".$email." </h3>
</body>
</html>
";
$email_headers = "MIME-Version: 1.0" . "\r\n";
$email_headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$email_headers .= 'From: info#myemail.com' . "\r\n";
if (mail($recipient, $subject, $email_content, $email_headers)) {
$msg= "Thanks You! For Subscribing our service.";
echo "<script type='text/JavaScript'>;window.location.href='../index.php';</script>";
} else {
$error= "Faild To Send Your Order. Please Try Again.";
echo "<script type='text/JavaScript'>alert ('$error');window.location.href='index.php';</script>";
}
} else {
$error1= "There was a problem with your submission, please try again.";
echo "<script type='text/JavaScript'>alert ('$error1');window.location.href='index.php';</script>";
}
?>

display a message after submit and refresh

i'm trying to get a message to appear after my form has been submitted, i can get the form to submit and the page to then refresh but unsure how to add the message after the refresh.
i have no knowledge of PHP so sorry is its and obvious answer
thanks
my html & PHP if needed
<form action="action.php" method="POST" target="my_iframe" onsubmit="setTimeout(function(){window.location.reload();},10);">
<input type="text" id="name" name="name" placeholder="name" required="required">
<input type="email" id="email" name="email" placeholder="email" required="required">
<input type="tel" id="phone" name="phone" placeholder="phone">
<div class="form-row" style="display:none;"><input type="hidden" name="url" placeholder="URL"></div>
<textarea id="message" name="message" placeholder="message" style="height:200px" required="required"></textarea>
<input id="submit" type="submit" value="submit">
</form>
<iframe name="my_iframe" width="1" height="1" style="border:none"></iframe>
<?php
$to = 'zoeharrisondesign#gmail.com';
$subject = 'New Message Recieved!';
$from = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$name = $_POST['name'];
//check honeypot
if( !empty( $honeypot ) ) {
echo 'There was a problem';
}
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Create email headers
$headers .= 'From: '.$from."\r\n".
'Reply-To: '.$from."\r\n" .
'X-Mailer: PHP/' . phpversion();
// Compose a simple HTML email message
$message = "$message \r\n |
From $name \r\n |
Tel: $phone";
// Sending email
if(mail($to, $subject, $message, $headers)){
echo 'Your message has been sent successfully.';
} else{
echo 'Unable to send email. Please try again.';
}
?>
Update your php sending email to this
if (mail($myEmail, $subject, $message)){
$success = "Message sent successful";
}else{
$success = "Message Sending Failed.";
}
Then add this php codes in your html codes add this code to display a message. Put it on top of your form html tag.
<?php
if (isset($success)){ echo "<div>" . $success . "</div>";}
?>
You may even add some styling on the message if you prefer.

Upon submitting form, scroll to vertical position on page

I have created a simple html/php form where visitors on my site can write their name, email and message and then send the message to my email. Problem is that when they submit the email, my site then performs a full refresh (it looks like) and therefore just reloads to the top of my site. I would like for the user to remain at the same scroll position after submit, so that they can instantly see whether the submit was succesful or not. So either a solution that prevents the refresh or some other solution that automatically scrolls down vertical to the form.
Can you tell me if this is possible using php? Or do I have to use some jquery/ajax solution?
Below is the code I am using. I am a complete novice, so please be gentle.
<form action="" method="post" id="form">
<div class="contact-info-group">
<label for="name"><span>Your name</span>
<input type="text" id="name" name="name" autocomplete="off" value="<?php echo $name; ?>"></label>
<label for="email"><span>Your email</span>
<input type="email" id="email" name="email" autocomplete="off"></label>
</div>
<label for="message"><span>Your message</span>
<textarea id="message" name="message"></textarea></label>
<input id="button1" type="submit" class="button next" name="contact_submit" value="Send message">
<?php
// Check for header injections
function has_header_injection($str) {
return preg_match("/[\r\n]/", $str);
}
if (isset ($_POST['contact_submit'])) {
$name = trim($_POST['name']);
$email = trim($_POST['email']);
$msg = $_POST['message'];
// Check to see if $name or $email have header injections
if (has_header_injection($name) || has_header_injection($email)) {
die();
}
if (!$name || !$email || !$msg) {
echo '<div class="contact-warning"><h2>! Error - Please note that all of the above fields are required !</h2></div>';
exit;
}
// Add the recipient email to a variable
$to = "email#email.com";
// Create a subject
$subject = "Message via website.com - $name";
// Construct the message
$message = "Name: $name\r\n";
$message .= "Email: $email\r\n";
$message .= "Message: \r\n\r\n$msg";
// Clean up the message
$message = wordwrap($message, 72);
// Set the mail headers into a variable
$headers = "MIME-Version 1.0\r\n";
$headers .= "Content-type: text/plain; charset=iso-8859-1\r\n";
$headers .= "From: $name <$email> \r\n";
$headers .= "X-Priority: 1\r\n";
$headers .= "X-MSMail-Priority: High\r\n\r\n";
// Send the email
mail($to, $subject, $message, $headers);
echo '<div class="contact-warning"><h2>Thank you for your message. We will get back to you shortly.</h2></div>';
}
?>
</form>

Contact Us form not Functioning HTML5/PHP Help needed

Good day,
Newbie here in PHP. I have been working on a website (free template) and got all the functions to work except the Contact Us part of the code. I don't get any errors it just does not send any email to the listed email or send back a response to the sender.
Here is the HTML Side of the code:
<form id="contact-form" action="php/mail.php">
<div class="control-group">
<div class="controls">
<input class="span6" type="text" id="name" name="name" placeholder="* Your name..."/>
<div class="error center" id="err-name">Please enter your name.</div>
</div></div>
<div class="control-group">
<div class="controls">
<input class="span6" type="email" name="email" id="email" placeholder="* Your email..."/>
<div class="error center" id="err-email">Please enter a valid email adress.</div></div></div>
<div class="control-group">
<div class="controls">
<textarea class="span6" name="comment" id="comment" placeholder="* Comments..."></textarea>
<div class="error center" id="err-comment">Please enter your comment.</div>
</div></div>
<div class="control-group">
<div class="controls">
<button id="send-mail" class="message-btn">Send message</button>
</div></div></form>
and this is the mail.php code used:
include 'functions.php';
if (!empty($_POST)) {
$data['success'] = true;
$_POST = multiDimensionalArrayMap('cleanEvilTags', $_POST);
$_POST = multiDimensionalArrayMap('cleanData', $_POST);
//your email adress
$emailTo = "myemail#gmail.com"; //"myemail#gmail.com";
//from email adress
$emailFrom = "myemail#gmail.com"; //"myemail#gmail.com";
//email subject
$emailSubject = "Mail from MyEmail";
$name = $_POST["name"];
$email = $_POST["email"];
$comment = $_POST["comment"];
if ($name == "")
$data['success'] = false;
if (!preg_match("/^[_\.0-9a-zA-Z-]+#([0-9a-zA-Z][0-9a-zA-Z-]+\.)+[a-zA-Z]{2,6}$/i", $email))
$data['success'] = false;
if ($comment == "")
$data['success'] = false;
if ($data['success'] == true) {
$message = "NAME: $name<br>
EMAIL: $email<br>
COMMENT: $comment";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html; charset=utf-8" . "\r\n";
$headers .= "From: <$emailFrom>" . "\r\n";
mail($emailTo, $emailSubject, $message, $headers);
$data['success'] = true;
echo json_encode($data);
}
}
I am really stuck at this point and this is the only issue I have left hope someone can help point out what I am doing wrong.
Regards,
Rafael
in order for a form to send POST requests you need to specify it by adding the method attribute:
<form id="contact-form" action="php/mail.php" method="post">
Is your php mail script placed in the cgi directory? This is my mail script:
<?php $name = trim(strip_tags($_POST['name']));
$email = trim(strip_tags($_POST['email']));
$message = htmlentities($_POST['message']);
$subject = "Some subject";
$to = 'info#xxx.de';
$body = <<<HTML
$message
HTML;
$headers = "From: $email\r\n";
$headers .= "Content-type: text/html\r\n";
// send the email
mail($to, $subject, $body, $headers);
// redirect afterwords, if needed
header('Location: ../contact/thx.html');?>
And it is placed in cgi directory.
After much research and help from CodeGoodie I think the blame lies with the Hosting. I had asked how I could check Server logs to detect the error and they just replied I need to upgrade to be able to use that feature (although it is supposed to be part of the feature already). Thanks for all the help guys. I did learn a lot :)

post to form to same page

Basically I am trying to re work a plugin so what I need to do is post the form data on the Cart.php form on the same page. Below is the set up I have but the $_POST info is not returning anything when email is sent:
Cart.php
<form id='SimpleEcommCartCartForm' action="" method="post">
<div id="emailForm">
<p>Please fill out the form below and one of our associates will contact you with more information.</p>
<div class="col-xs-4">
Name: <input type="text" name="name" id="name">
</div>
<div class="col-xs-4">
E-mail: <input type="email" name="email" id="email">
</div>
<div class="col-xs-4">
Phone: <input type="tel" name="phone" id="phone">
</div>
</div>
</form>
<?php
//Send the email
$to = "test#gmail.com";
$name = $_POST['name'] ;
$from = $_POST['email'] ;
$phone = $_POST['phone'] ;
$headers = "MIME-Version: 1.0\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\n";
$headers .= "From: $from";
$subject = "Pump Part Inquiry";
$emailBody = "
<html>
<head>
<style>
</style>
</head>
<body>
<h1> Pump Inquiry</h1>
<h3>From:".$name."<h3>
<h3>Phone:".$phone."<h3>
<p>Minetuff Parts".$mine."</p>
<p>Flygt Parts".$flygt."</p>
</body>
</html>";
$send = mail($to, $subject, $emailBody, $headers);
?>
It sends the mail with nothing in it (or missing information) if you load it right away, and it's because of a specific reason.
As it stands, your code will send you an Email as soon as you load the page, so it's best to wrap your (PHP) code inside an isset() to work in conjunction with a (named) submit button, which seems to be missing in your originally posted question/code.
Plus, you have two undefined variables:
$mine and $flygt, so you'll need to define those to fit your needs.
I.e.: if(isset($_POST['submit'])) and <input type="submit" name="submit" value="Send">
Sidenote: It's best to check for empty fields, but that's another topic; see my footnotes.
Tested and working, and receiving all info and I've replaced your present mail function with if(mail($to, $subject, $emailBody, $headers)){...}
<form id='SimpleEcommCartCartForm' action="" method="post">
<div id="emailForm">
<p>Please fill out the form below and one of our associates will contact you with more information.</p>
<div class="col-xs-4">
Name: <input type="text" name="name" id="name">
</div>
<div class="col-xs-4">
E-mail: <input type="email" name="email" id="email">
</div>
<div class="col-xs-4">
Phone: <input type="tel" name="phone" id="phone">
<input type="submit" name="submit" value="Send">
</div>
</div>
</form>
<?php
//Send the email
if(isset($_POST['submit'])){
$to = "test#gmail.com";
$name = $_POST['name'] ;
$from = $_POST['email'] ;
$phone = $_POST['phone'] ;
$headers = "MIME-Version: 1.0\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\n";
$headers .= "From: $from";
$subject = "Pump Part Inquiry";
// $mine = " Mine variable"; // replace this
// $flygt = " Flygt variable"; // replace this
$emailBody = "
<html>
<head>
<style>
</style>
</head>
<body>
<h1> Pump Inquiry</h1>
<h3>From:".$name."<h3>
<h3>Phone:".$phone."<h3>
<p>Minetuff Parts".$mine."</p>
<p>Flygt Parts".$flygt."</p>
</body>
</html>";
// $send = mail($to, $subject, $emailBody, $headers);
if(mail($to, $subject, $emailBody, $headers)){
echo "Mail sent.";
}
else{
echo "Sorry, something went wrong.";
}
} // brace for if(isset($_POST['submit']))
?>
If you're still having problems:
Add error reporting to the top of your file(s) right after your opening <?php tag, which will help during production testing.
error_reporting(E_ALL);
ini_set('display_errors', 1);
Footnotes:
You are open to XSS attacks (Cross-site scripting).
Use the following (PHP) filter function: FILTER_SANITIZE_FULL_SPECIAL_CHARS
$name = filter_var($_POST['name'], FILTER_SANITIZE_FULL_SPECIAL_CHARS);
Equivalent to calling htmlspecialchars() with ENT_QUOTES set. Encoding quotes can be disabled by setting.
To check for empty fields, you can add this below if(isset($_POST['submit'])){
if(empty($_POST['name']) || empty($_POST['email']) || empty($_POST['phone'])){
echo "Fill out all the fields";
exit;
}
|| means "OR", which could very well be replaced by OR if you want, however || has precedence over OR.
This is a very basic method; there are other ways of accomplishing this, but you get the gist of it.
The issue here is that the mail is sent before the submit since you didn't test of the existence of $_POST variables, try something like this:
<form id='SimpleEcommCartCartForm' action="" method="post">
<input type="hidden" name="action" value="mailing"/>
<div id="emailForm">
<p>Please fill out the form below and one of our associates will contact you with more information.</p>
<div class="col-xs-4">
Name: <input type="text" name="name" id="name">
</div>
<div class="col-xs-4">
E-mail: <input type="email" name="email" id="email">
</div>
<div class="col-xs-4">
Phone: <input type="tel" name="phone" id="phone">
</div>
<input name="submit" type="submit"/>
</div>
</form>
<?php
if(isset($_POST['mailing'])){
// Send the email
$to = "test#gmail.com";
$name = $_POST['name'] ;
$from = $_POST['email'] ;
$phone = $_POST['phone'] ;
$headers = "MIME-Version: 1.0\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\n";
$headers .= "From: $from";
$subject = "Pump Part Inquiry";
$emailBody = "
<html>
<head>
<style>
</style>
</head>
<body>
<h1> Pump Inquiry</h1>
<h3>From:".$name."<h3>
<h3>Phone:".$phone."<h3>
<p>Minetuff Parts".$mine."</p>
<p>Flygt Parts".$flygt."</p>
</body>
</html>";
$send = mail($to, $subject, $emailBody, $headers);
}
?>
Once the email is sent, you haven't told PHP to output anything to the page.
You could add something like echo 'Mail sent'; to the end of the PHP so if the script is being executed correctly then you'll know about it.
If the problem lies within the email not being sent at all, this may be a problem with your server and you should look for a tutorial on how to set up mail correctly. If you're using shared hosting, there are some companies that do not allow the use of the PHP mail() function so I would check with them first.
Amongst this I would recommend that you use something like PHPMailer, a full email library for PHP. Then you could configure for your emails to be sent from anywhere that supports SMTP (not sure if others are supported but there most likely is). PHPMailer will work with shared hosting.
Updated - Code that you can try
Try this out in replacement of $emailBody:
$emailBody = <<<EOT
<html>
<head>
<style type="text/css">
</style>
</head>
<body>
<h1>Pump Inquiry</h1>
<h3>From: $name<h3>
<h3>Phone: $phone<h3>
<p>Minetuff Parts $mine</p>
<p>Flygt Parts $flygt</p>
</body>
</html>
EOT;
NB: Some email clients only allow the use of inline CSS, so if you did decide to add anything to your <style> tag in the <head>, don't expect it to work very well or at all in some cases.
As someone has mentioned before, you'll also need to protect against cross-site scripting.

Categories