<?php
function goback()
{
header("refresh:1; url=index.php");
exit;
}
if(isset($_POST['submit'])){
$name = $_POST['name'];
$comment = $_POST['comment'];
$mob = $_POST['mob'];
$email = $_POST['email'];
$to ='xyz#gmail.com';
$subject= 'Contact form pharma';
$message ="Name: ".$name."\n".
"Comment: ".$comment."\n".
"Mobile: ".$mob."\n".
"E-mail: ".$email;
if(mail($to, $subject, $message)){
echo "<script type='text/javascript'>alert('message successfully sent');window.close();</script>";
goback();
}
else
{
echo "something went wrong";
}
}
?>
Index.php on this page we have a form request acallback for which we use a html form and mail Function
<form>
<fieldset>
<legend>
<center>Request a call back</center>
</legend>
<div class="form-group">
<label class="control-label" for="requestid">Your Name</label>
<div>
<input id="requestid" name="name" placeholder="Name please" class="form-control input-md" required="" type="text">
</div>
</div>
<div class="form-group">
<label class="control-label" for="dis">Your Comment</label>
<div>
<textarea class="form-control" placeholder="Your Comment" id="comment" name="comment"></textarea>
</div>
</div>
<div class="form-group">
<label class="control-label" for="dis">Mobile No</label>
<div>
<input placeholder="1234-456-7890" class="form-control input-md" id="phonenum" name="mob" type="tel" required >
</div>
</div>
<div class="form-group">
<label class="control-label" for="dis">E-mail</label>
<div>
<input placeholder="xyz#example.com" class="form-control input-md" id="email" name="email" type="email" required >
</div>
</div>
<div class="form-group">
<div class="text-right">
<button id="submit" name="submit" class="btn btn-info">Submit Message</button>
</div>
</div>
</fieldset>
</form>
I want to display this alert box on the same page. It get redirect to blank page then shows alert box . If goback() removed then it redirects to blank page and does no return back to previous page.
how to solve this?
You are missing something. How it can be possible to display a popup related to another page without sending any request to it? First I have set a success session in mail.php page which is set if mail is sent successfully.
<?php
session_start();
function goback()
{
$_SESSION['success'] = 'true';
header("location:index.php");
}
if(isset($_POST['submit'])){
$name = $_POST['name'];
$comment = $_POST['comment'];
$mob = $_POST['mob'];
$email = $_POST['email'];
$to ='xyz#gmail.com';
$subject= 'Contact form pharma';
$message ="Name: ".$name."\n".
"Comment: ".$comment."\n".
"Mobile: ".$mob."\n".
"E-mail: ".$email;
if(mail($to, $subject, $message)){
goback();
}
else
{
echo "something went wrong";
}
}
?>
Then in index.php page we just have to call this session. if its true then we can show pupop
<?php
if(isset($_SESSION['success']) && $_SESSION['success']=='true')
{
//show pupop
$_SESSION['success'] = 'false';
}
?>
Related
I'm writing to create a contact form on my website and then get the information sent to my inbox, but it is not working. Please take a look at my code below (PHP is not my thing) and let me know where I've gone wrong.
here is my html form
<form action="" method="post">
<div class="row">
<div class="col-md-6">
<input type="text" name="name" placeholder="Name"class="form-control" required>
</div>
<div class="col-md-6">
<input type="text" name="number" class="form-control" placeholder="Phone"required>
</div>
</div>
<input type="email" class="form-control" placeholder="Email" name="email" required>
<textarea name="message" class="form-control"style="resize:none;" placeholder="Message" rows="8" cols="30" required></textarea>
<input type="submit" name="submit" value="contact us">
</form>
This is my php code
<?php
$message ='';
if(isset($_POST['submit'])){
$name = $_POST['name'];
$email = $_POST['email'];
$comments = $_POST['message'];
$number = $_POST['number'];
$msg = $name ."<br>".$email."<br>".$comments."<br>".$number;
$to = "karunagoyal7#gmail.com";
$subject = "Email from Contact Us from";
if(mail( $to, $subject, $msg)){
$message = "<div class='alert alert-success'> your message has been send to saisoftlinktechnologies </div>";
}else{
$message = "<div class='alert alert-primary'> your message has not been send to saisoftlinktechnologies </div>";
}
}
?>
Both html from and php script is in same file.After submitting the form it is not even displaying the message of success or failure.
This is the code for the form.
<?php
if ($_POST) {
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$email = $_POST['email'];
$company = $_POST['company'];
$phone = $_POST['phone'];
$checkbox = '';
if (isset($_POST['checkbox'])) {
$checkbox = 'Yes';} else{
$checkbox = 'No' ;}
$message = $_POST['message'];
$from = 'Demo Contact Form';
$to = 'imvael#gmail.com, vnikolic1#cps.edu';
$subject = 'Message from Contact Demo ';
$body ="From: $name\n E-Mail: $email\n Company: $company\n Phone: $phone\n Opt In?: $checkbox\n Message:\n $message";
$headers = 'From: webmaster#bradfordsystems.com' . "\r\n" .
'Reply-To: ' .$email . "\r\n" .
'X-Mailer: PHP/' . phpversion();
// Check if name has been entered
if (!$_POST['name']) {
$errName = 'Please enter your name';
}
// Check if email has been entered and is valid
if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$errEmail = 'Please enter a valid email address';
}
// Check if name has been entered
if (!$_POST['phone']) {
$errName = 'Please enter your phone number';
}
//Check if message has been entered
if (!$_POST['message']) {
$errMessage = 'Please enter your message';
}
// then after the posting of the form data
// validation
// If there are no errors, send the email
if (!$errName && !$errEmail && !$errMessage && isset($_POST['url']) && $_POST['url'] == '') {
if (mail ($to, $subject, $body, $headers)) {
header("Location: thankyou.php"); /* Redirect browser */
exit();
} else {
$result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later.</div>';
}
}
}
header("Location: " . $_SERVER['REQUEST_URI']);
exit();
}
This is the form itself
<div class="wrapper">
<div class="row">
<div class="col _66">
<?php echo $result; ?>
<form role="form" name="contactForm" id="contactForm" method="post" action="contact.php#contactForm">
<p>
We welcome your feedback and inquiries. Please use the form below to get in touch.
</p>
<div class="row">
<div class="col">
<input type="text" id="name" name="name" placeholder="Name" value="<?php echo htmlspecialchars($_POST['name']); ?>" required>
<?php echo "<p class='text-danger'>$errName</p>";?>
</div>
<div class="col">
<input type="email" id="email" name="email" placeholder="Company Email" value="<?php echo htmlspecialchars($_POST['email']); ?>" required>
<?php echo "<p class='text-danger'>$errEmail</p>";?>
</div>
</div>
<div class="row">
<div class="col">
<input type="text" id="company" name="company" placeholder="Company" value="<?php echo htmlspecialchars($_POST['company']); ?>" required>
</div>
<div class="col">
<input type="tel" id="phone" name="phone" <?php echo htmlspecialchars($_POST['phone']); ?> placeholder="Phone" required>
<?php echo "<p class='text-danger'>$errPhone</p>";?>
</div>
</div>
<div class="row">
<div class="col">
<input type="number" id="zipcode" name="zipcode" placeholder="Zip Code" value="<?php echo htmlspecialchars($_POST['zipcode']); ?>">
</div>
<div class="col">
<input id="checkBox" type="checkbox"> <span id="optInText">YES, I want a Free Workspace Evaluation!</span>
</div>
</div>
<div class="row">
<div class="col">
<p class="antispam">Leave this empty: <input type="text" name="url" /></p>
</div>
</div>
<div class="row">
<div class="col submit-col">
<p>Questions or Comments?</p>
<textarea id="message" name="message" placeholder="Enter your questions or comments here" style="height:200px"> <?php echo htmlspecialchars($_POST['message']); ?></textarea>
<?php echo "<p class='text-danger'>$errMessage</p>";?>
<button class="btn btn-dark hvr-underline-from-left" name="submit" type="submit" value="Send">Submit Request</button>
</div>
</div>
</form>
</div>
</div>
</section>
I am writing a contact form script from scratch The code is working correctly, but I am unable to prevent resubmissions if they user refreshed the page or press the submit buton more then once.
Also is it a bad practice to use self submitting contact forms?
Hey guys so I have an HTML document with a contact form I created and it is not working. I have the PHP in a seperate PHP file like so:
HTML:
<form class="form-horizontal" action="form_process.php" method="post" name="contact_form">
<div class="form-group">
<label class="col-sm-2 control-label white-color">Email</label>
<div class="col-sm-10">
<input type="email" class="form-control" name="email" placeholder="Email" required>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label white-color">Name</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="name" placeholder="First Name" required>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label white-color">Message</label>
<div class="col-sm-10">
<textarea class="form-control" rows="5" placeholder="Type your message here!" name="message" required></textarea>
<button type="submit" class="btn btn-default btn-lg text-center" id="send-btn" name="submit">Send</button>
</div>
</div>
</form>
And here is the PHP:
<?php
if (isset($POST['name']) && isset($_POST['email'])) {
$email = $_POST['email'];
$name = $_POST['name'];
$message = $_POST['message'];
$to = 'j_goris#live.com';
$subject = 'JorgeGoris.com Form Submission';
$text = "Name: ".$name."\n"."Email: ".$email."\n". "Wrote the following: "."\n\n".$message;
if(mail($to, $subject, $text, "From: ".$name)){
echo '<h1>Thanks! I will get back to you shortly.</h1>';
}
else {
echo 'Sorry there was an error! Please try again.';
}
}?>
This is my first time tackling PHP contact forms. I uploaded all my files to my server and still no dice. Can you guys see whats wrong?
Replace:
$POST['name']
to:
$_POST['name']
Replace:
if(mail($to, $subject, $text, "From: ".$name)){
To:
if(mail($to, $subject, $text, "From: ".$email)){
Full Code:
<?php
if (isset($_POST['name']) && isset($_POST['email'])) {
$email = $_POST['email'];
$name = $_POST['name'];
$message = $_POST['message'];
$to = 'j_goris#live.com';
$subject = 'JorgeGoris.com Form Submission';
$text = "Name: ".$name."\n"."Email: ".$email."\n". "Wrote the following: "."\n\n".$message;
if(mail($to, $subject, $text, "From: ".$email)){
echo '<h1>Thanks! I will get back to you shortly.</h1>';
}
else {
echo 'Sorry there was an error! Please try again.';
}
}
?>
I am trying to create a two page form- the first page is optional. Its a prompt to get started. It was working perfectly until I started adding in PHPMailer. Now when I click submit on the first, optional form on the first page it submits it through the mailer and all its validations/errors then emails correctly it if I comment out the error if statements.
Here is the first form that is optional. It should take you to the estimate page with those values stored in the form already so you don't have to do it over again.
<form class="form-horizontal first-form" action="estimate.php" method="post">
<div class="form-group">
<label for="inputMoveDate" class="col-xs-4 text-left">Move Date</label>
<div class="col-xs-8">
<input type="text" name="date" class="form-control" id="datepicker">
</div><!--col-xs-7-->
</div><!--form-group-->
<div class="form-group">
<label for="pickUpZip" class="col-xs-4">Pick Up Zip</label>
<div class="col-xs-8">
<input type="text" name="pickUpZip" class="form-control" id="pickUpZip" placeholder="">
</div><!--col-xs-7-->
</div><!--form-group-->
<div class="form-group">
<label for="dropOffZip" class="col-xs-4">Drop Off Zip</label>
<div class="col-xs-8">
<input type="text" name="dropOffZip" class="form-control" id="dropOffZip" placeholder="">
</div><!--col-xs-7-->
</div><!--form-group-->
<div class="form-group">
<div class="col-xs-12">
<button type="submit" class="btn btn-default center-block">Continue</button>
</div><!--col-sm-offset-2-->
</div><!--form-group-->
</form>
Estimate.php second page. The PHP at the top of the document.
session_start();
if ($_SERVER["REQUEST_METHOD"]=="POST") {
$date = $_POST['date'];
$pickUpZip = $_POST['pickUpZip'];
$dropOffZip = $_POST['dropOffZip'];
$dwellingType = $_POST['dwellingType'];
$salutation = $_POST['salutation'];
$firstName = trim(filter_input(INPUT_POST, "first-name", FILTER_SANITIZE_STRING));
$lastName = trim(filter_input(INPUT_POST, "last-name", FILTER_SANITIZE_STRING));
$email = trim(filter_input(INPUT_POST, "email", FILTER_SANITIZE_SPECIAL_CHARS));
$phone = $_POST['phone'];
$pickUpAddress = $_POST['pickUpAddress'];
$pickUpCity = $_POST['pickUpCity'];
$pickUpState = $_POST['pickUpState'];
$comments = $_POST['comments'];
$emailBody ="From: $salutation $firstName $lastName\n
Phone Number: $phone\n
E-Mail: $email\n
Message:\n $comments
\n \n
Move Date: $date\n
Pick Up Address: $pickUpAddress\n
Pick Up City: $pickUpCity\n
Pick Up State: $pickUpState\n
Pick Up Zip: $pickUpZip\n
\n
Drop Off Zip: $dropOffZip\n
Dwelling Type: $dwellingType\n";
if($firstName == "" || $email == "" || $lastName == "") {
echo "Please fill in the required forms: first name, last name, and email.";
exit;
}
if ($_POST["address"] != "") {
echo "Bad form input";
exit;
}
require('phpmailer/class.phpmailer.php');
$mail = new PHPMailer;
if (!$mail->ValidateAddress($email)) {
echo 'Invalid email. Try again.';
exit;
}
$mail->setFrom($email, $firstName);
$mail->addAddress('xxxx#xxx.com', 'name here');
$mail->isHTML(false);
$mail->Subject = 'Estimate Request Inquiry From ' . $firstName;
$mail->Body = $emailBody;
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
exit;
}
header("location:estimate.php?status=thanks");
}
Here is the starting and ending of the form HTML, the highlights.
<?PHP
if (isset($_GET["status"]) && $_GET["status"] == "thanks") {
echo "Thank you for your estimate request. We will return your inquiry soon!
</div>";
} else {
?>
<form class="form-horizontal first-form" action="estimate.php" method="post" style="margin: 0 45px;">
<label for="date" class="col-xs-4 text-left">Move Date</label>
<div class="col-xs-8">
<input type="text" class="form-control" id="datepicker" name="date" value="<?php echo $_POST["date"]; ?>">
</div><!--col-xs-7-->
</div><!--form-group-->
<div class="form-group">
<label for="pickUpZip" class="col-xs-4">Pick Up Zip</label>
<div class="col-xs-8">
<input type="text" name="pickUpZip" class="form-control" id="pickUpZip" value="<?php echo $_POST["pickUpZip"]; ?>">
</div><!--col-xs-7-->
</div><!--form-group-->
<div class="form-group">
<label for="dropOffZip" class="col-xs-4">Drop Off Zip</label>
<div class="col-xs-8">
<input type="text" name="dropOffZip" class="form-control" id="dropOffZip" value="<?php echo $_POST["dropOffZip"]; ?>">
</div><!--col-xs-7-->
</div><!--form-group-->
<div class="col-xs-12">
<button type="submit" id="submit" name="submit" class="btn btn-default center-block">Submit</button>
</div><!--col-sm-offset-2-->
</div><!--form-group-->
</form>
<?PHP
}
?>
This line attempts to send the email every time:
if(!$mail->send()) {
and your error messages are echoed if and when it fails.
You should nest this entire block in an if statement that checks if the form was submitted from page 2. You can give the submit button a different value and check that (i.e. 'submit1', 'submit2').
I have successfully set up a php script for my contact form on my website but I have recently found out that my server provider does not accept php. Instead I have use SMTP.
Can anyone help me as this is new to me. I've attempted using other scripts but I cannot implement it.
This is my php code:
<?php
/* Set e-mail recipient */
$myemail = "mail#louisreed.co.uk";
/* Check all form inputs using check_input function */
$name = check_input($_POST['name'], "Your Name");
$email = check_input($_POST['email'], "Your E-mail Address");
$subject = check_input($_POST['subject'], "Message Subject");
$message = check_input($_POST['message'], "Your Message");
/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\#[\w\-]+\.[\w\-]+)/", $email))
{
show_error("Invalid e-mail address");
}
/* Let's prepare the message for the e-mail */
$subject = "Someone has sent you a message";
$message = "
Someone has sent you a message using your contact form:
Name: $name
Email: $email
Subject: $subject
Message:
$message
";
/* Send the message using mail() function */
mail($myemail, $subject, $message);
/* Redirect visitor to the thank you page */
header('Location: http://louisreed.co.uk');
exit();
/* Functions we used */
function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
show_error($problem);
}
return $data;
}
function show_error($myError)
{
?>
<html>
<body>
<p>Please correct the following error:</p>
<strong><?php echo $myError; ?></strong>
<p>Hit the back button and try again</p>
</body>
</html>
<?php
exit();
}
?>
My HTML form:
<div class="row">
<div class="col-lg-8 col-lg-offset-2">
<form role="form" action="http://www.louisreed.co.uk/includes/mailer.php" method="post">
<div class="row">
<div class="form-group col-xs-12 floating-label-form-group">
<label for="name">Name</label>
<input class="form-control" type="text" id="name" name="name" placeholder="Name">
</div>
</div>
<div class="row">
<div class="form-group col-xs-12 floating-label-form-group">
<label for="email">Email Address</label>
<input class="form-control" type="email" id="email" name="email" placeholder="Email Address">
</div>
</div>
<div class="row">
<div class="form-group col-xs-12 floating-label-form-group">
<label for="subject">Subject</label>
<textarea placeholder="Subject" class="form-control" id="subject" name="subject" rows="1"></textarea>
</div>
</div>
<div class="row">
<div class="form-group col-xs-12 floating-label-form-group">
<label for="message">Message</label>
<textarea placeholder="Message" class="form-control" id="message" name="message" rows="5"></textarea>
</div>
</div>
<br>
<div class="row">
<div class="form-group col-xs-12">
<button type="submit" class="btn btn-lg btn-primary">Send</button>
</div>
</div>
</form>
</div>
</div>
Any suggestions welcome!
Thanks! :)
Louis
Oke I checked a few thinks. I guess you could atleast try the following. I know your host doesn't support php which find hard to believe tough. So maybe something else is wrong. What is the host you are using?
<?php
if($_POST) {
//Strip user input
function sanitize($value) {
//Escape the user input and then strip all html tags
//mysql_* is not recommended but I guess you don't store data in a database
$value = mysql_real_escape_string(strip_tags($value));
//Return the sanitized user input
return $value;
}
//To display all errors
function errors($error) {
echo '<ul class="error">';
foreach($error as $fail) {
echo '<li>'.$fail.'</li>';
}
echo '</ul>';
}
//All the input fields
$name = sanitize($_POST['name']);
$email = sanitize($_POST['email']);
$subject = sanitize($_POST['subject']);
$message = sanitize($_POST['message']);
//Check if there are no empty fields
if(!empty($name), !empty($email) && !empty($subject) && !empty($message)) {
if(strlen($name) <= 3) {
$error[] = 'Name '.$name.' is too short';
}
if(strlen($email) <= 3) {
$error[] = 'Email '.$email.' is too short';
}
if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$error[] = 'Email '.$email.' is not a valid email';
}
if(strlen($subject) <= 3) {
$error[] = 'Subject '.$subject.' is too short';
}
if(strlen($message) <= 8) {
$error[] = 'Message is too short';
}
//If there are no errors
if(empty($error)) {
$to = "mail#louisreed.co.uk"; //The email you want to send it to
//Subject already set
$headers = "FROM: My site"; //Sets the headers
//The message for the email
$message = "Someone has sent you a message using your contact form:\r\n\r\nName: ".$name."\r\nEmail: ".$email."\r\nSubject: ".$subject."\r\n\r\nMessage: ".$message;
$mail = mail($to, $subject, $message, $headers);
//If the mail has been send
if($mail) {
header('Location: http://louisreed.co.uk/index.php?success');
exit();
}
}
} else {
$error[] = 'There are empty fields';
}
//If there are errors show them
if(!empty($error)) {
echo errors($error);
}
}
//If issset success (from the header()) display a message
if(isset($_GET['success'])) {
echo '<p>Thank you for your message</p>';
}
?>
<form role="form" action="" method="post">
<div class="row">
<div class="form-group col-xs-12 floating-label-form-group">
<label for="name">Name</label>
<input class="form-control" type="text" id="name" name="name" placeholder="Name">
</div>
</div>
<div class="row">
<div class="form-group col-xs-12 floating-label-form-group">
<label for="email">Email Address</label>
<input class="form-control" type="email" id="email" name="email" placeholder="Email Address">
</div>
</div>
<div class="row">
<div class="form-group col-xs-12 floating-label-form-group">
<label for="subject">Subject</label>
<textarea placeholder="Subject" class="form-control" id="subject" name="subject" rows="1"></textarea>
</div>
</div>
<div class="row">
<div class="form-group col-xs-12 floating-label-form-group">
<label for="message">Message</label>
<textarea placeholder="Message" class="form-control" id="message" name="message" rows="5"></textarea>
</div>
</div>
<br>
<div class="row">
<div class="form-group col-xs-12">
<button type="submit" class="btn btn-lg btn-primary">Send</button>
</div>
</div>
</form>
So try the exact code I gave you above just copy and past it over the form code you have now. You can leave the rest of your code as is. After that save the complete file as index.php. After that remove the index.html file from your FTP. If you have done that you can upload the index.php file now check what you get. If there are still some errors please let me know