PHP: Interactive Contact form not working - php

Why is this form not working?
I'm creating a form that sends messages to users.
The user email comes from the database.
The query that searches the database is influenced by some data sent previously to the browser.
The problem at the moment is:
When one clicks to submit, nothing really happens.
Got a file called mini-contact.php with the following:
Script to proccess the form:
<?php
var_dump($_POST);
error_reporting(E_ALL);
$company=$_GET['company'];
$sql = "SELECT * FROM companies WHERE companies.companyID= '$company'";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
$row = mysqli_fetch_assoc($result);
$companyemail = $row['Email_Address'];
if(isset($_POST['submit'])){
$to = "tiagomartinsperes#gmail.com"; // this is your Email address
$from = $_POST['email']; // this is the sender's Email address
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$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'];
$header = "From:" . $from;
$header.= "MIME-Version: 1.0\r\n";
$header.= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$header.= "X-Priority: 1\r\n";
$header2 = "From:" . $to;
$header2.= "MIME-Version: 1.0\r\n";
$header2.= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$header2.= "X-Priority: 1\r\n";
$status1 = mail($to,$subject,$message,$header);
$status2 = mail($from,$subject2,$message2,$header2); // sends a copy of the message to the sender
if($status1 and $status2){
$first_name = $_POST['first_name'];
echo 'Mail Sent. Thank you' . $first_name . ', we will contact you shortly.';
} else {
echo '<p>Something went wrong, Please try again!</p>';
}
// You can also use header('Location: thank_you.php'); to redirect to another page.
}
}
?>
The actual form:
<form id="contact-form" method="post" action="">
<div class="messages"></div>
<div class="controls">
<div class="row">
<div class="col-md-6 col-sm-6 no-left-pad">
<div class="form-group">
<label>First Name *</label>
<input type="text" name="first_name" class="form-control" placeholder="Please enter your first name" required="required" data-error="First name is required.">
<div class="help-block with-errors"></div>
</div>
</div>
<div class="col-md-6 col-sm-6 no-left-pad">
<div class="form-group">
<label>Last Name *</label>
<input type="text" name="last_name" class="form-control" placeholder="Please enter your last name" required="required" data-error="Last name is required.">
<div class="help-block with-errors"></div>
</div>
</div>
<div class="col-md-6 col-sm-6 no-left-pad">
<div class="form-group">
<label>Email *</label>
<input type="email" name="email" class="form-control" placeholder="Please enter your email" required="required" data-error="Valid email is required.">
<div class="help-block with-errors"></div>
</div>
</div>
<div class="col-md-6 col-sm-6 no-left-pad">
<div class="form-group">
<label>Phone</label>
<input type="tel" name="phone" class="form-control" placeholder="Please enter your phone">
<div class="help-block with-errors"></div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12 no-left-pad">
<div class="form-group">
<label>Subject</label>
<input type="text" name="subject" class="form-control" placeholder="Example: Service Request ">
<div class="help-block with-errors"></div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12 no-left-pad">
<div class="form-group">
<label>Message *</label>
<textarea name="message" class="form-control" placeholder="Please enter your message" rows="4" required="required" data-error=" Please enter your message."></textarea>
<div class="help-block with-errors"></div>
</div>
</div>
<div class="col-md-12 no-left-pad">
<p class="text-muted"><strong>*</strong> These fields are required.</p>
</div>
<div class="col-md-12 no-left-pad">
<input type="submit" class="btn btn-default caps" name="submit" value="Submit">
</div>
</div>
</div>
</form>
What am I doing wrong?
Thank you for your time

One needs to have SMPT properly configure so that it works.

Related

Why isn't my PHP form working? I do receive the email but there is no content in it

PHP form on my website isn't responding well. It would be appreciated if someone could help me.
This is the HTML Code of the form
<form action="contactform.php" method="post" name="form" class="p-5 bg-white">
<div class="row form-group">
<div class="col-md-6 mb-3 mb-md-0">
<label class="text-black" for="fname">First Name</label>
<input type="text" id="fname" class="form-control">
</div>
<div class="col-md-6">
<label class="text-black" for="lname">Last Name</label>
<input type="text" id="lname" class="form-control">
</div>
</div>
<div class="row form-group">
<div class="col-md-12">
<label class="text-black" for="email">Email</label>
<input type="email" id="email" class="form-control">
</div>
</div>
<div class="row form-group">
<div class="col-md-12">
<label class="text-black" for="subject">Subject</label>
<input type="subject" id="subject" class="form-control">
</div>
</div>
<div class="row form-group">
<div class="col-md-12">
<label class="text-black" for="mssg">Message</label>
<textarea name="mssg" id="mssg" cols="30" rows="7" class="form-control" placeholder="Write your notes or questions here..."></textarea>
</div>
</div>
<div class="row form-group">
<div class="col-md-12">
<input type="submit" value="Send Message" class="btn btn-primary py-2 px-4 text-white">
</div>
</div>
</form>
This is the PHP Code
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$to = "alyyashar#gmail.com";
$subject = "New email from your site!";
$fname = $_POST['fname'];
$email = $_POST['email'];
$message = $_POST['message'];
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";
$title = '<h3>Hello! You have received a new mail from your website!</h3>';
$body = "$title
<br/>
<b>From:</b> $fname
<br/>
<b>E-Mail:</b> $email
<br/>
<b>Message:</b>\n$message
<br/>
<br/>";
if (mail($to, $subject, $body, $headers)){
echo "<h1>Sent Successfully! Thank you"." ".$fname.", We will contact you shortly!</h1>";
} else {
echo "Something went wrong!";
}
}
?>
<br>
Back to Homepage
This is the email I receive
Screenshot of the email
When I enter information into the form and click send message, I do receive the email but there is no content in it.
The form elements have no name attributes, which is what the browser uses to send their name/value pairs to the server. So while it's posting the form, none of the values are being included.
For example, this:
<input type="text" id="fname" class="form-control">
Should be this:
<input type="text" id="fname" class="form-control" name="fname">
The same fix would need to be repeated for the remaining form elements.

PHP: Contact us form does not echo success text

I have two html forms that both send emails successfully using PHP. However, one form echo successful submission text, but the other one does not.
Here is the form that is part of the index.html page, which does not echo any text after submission. Please note, the email still sends:
<form class="booking-form" id="myForm" action="#">
<div class="row">
<div class="col-lg-12 d-flex flex-column">
<input name="name" placeholder="Your name" onfocus="this.placeholder = ''" onblur="this.placeholder = 'Your Name'" class="form-control mt-20" required="" type="text">
</div>
<div class="col-lg-6 d-flex flex-column">
<input name="subject" placeholder="subject" onfocus="this.placeholder = ''" onblur="this.placeholder = 'subject'" class="form-control mt-20" required="" type="text">
</div>
<div class="col-lg-6 d-flex flex-column">
<input name="email" placeholder="Email" onfocus="this.placeholder = ''" onblur="this.placeholder = 'Email'" class="form-control mt-20" required="" type="email">
</div>
<div class="col-lg-12 flex-column">
<textarea rows="5" class="form-control mt-20" name="message" placeholder="Messege" onfocus="this.placeholder = ''" onblur="this.placeholder = 'Messege'" required=""></textarea>
</div>
<div class="col-lg-12 d-flex justify-content-end send-btn">
<button type="submit" class="genric-btn primary mt-20 text-uppercase ">Send</button>
</div>
</div>
</form>
The following is the PHP mailer code:
<?php
$to = 'myEmail#gmail.com';
$firstname = $_POST["name"];
$email= $_POST["email"];
$text= $_POST["message"];
$subject= $_POST["subject"];
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= "From: Email coming from Brave Towing " . $email . "\r\n"; // Sender's E-mail
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$message = "
<html>
<head>
</head>
<body style=\"background-color:#fafafa;\">
<div style=\"padding:20px;\">
Customer Email: <span style=\"color:#888\">$email</span>
<br>
Contact Subject: <span style=\"color:#888\">$subject</span>
<br>
Contact Message: <div style=\"color:#888\">$text</div>
</div>
</body>
</html>
";
if (#mail($to, $email, $message, $headers))
{
echo '<h1>The message has been sent</h1>';
}else{
echo 'failed';
}
?>

Bootstrap 4 form not sedning any messages

My bootstrap 4 form is not sending any messages, I've uploaded the index.php and contact.php files on the server and I'm not having any response. Would you be able to help me find the problem?
Landing page with simple form
<form id="#contacts" method="POST" class="form" role="form">
<div class="row">
<div class="col-md-6 form-group">
<input class="form-control" id="name" name="name"
placeholder="Podaj swoje Imię i nazwisko" type="text" required/>
</div>
<div class="col-md-6 form-group">
<input class="form-control" id="website" name="website"
placeholder="Adres strony www" type="text" required/>
</div>
<div class="col-md-6 form-group">
<input class="form-control" id="telephone" name="telephone"
placeholder="Podaj swój numer telefonu" type="text" required/>
</div>
<div class="col-md-6 form-group">
<input class="form-control" id="email" name="email"
placeholder="Podaj swój email" type="email" required/>
</div>
</div>
<textarea class="form-control" id="message" name="message" placeholder="Twoja wiadomość"
rows="4"></textarea>
<br>
<div class="row">
<div class="col-md-12 form-group">
<button class="btn btn-primary" type="submit">Wyślij</button>
</div>
</div>
</form>
contact.php
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$website = $_POST['website'];
$telephone = $_POST['telephone'];
$email_from = 'SEO';
$email_subject = 'Wiadomość kontaktowa z... '
$email_body = "Name: $name.\n".
"Email: $email.\n".
"Message: $message.\n";
"Website: $website.\n";
"Telephone: $telephone.\n";
$to = "biuro#of.pl";
$headers = "From: $email_from r\n";
$headers .= "Reply-To: $email r\n";
mail($to,$email-$email_subject,$email_body,$headers);
header("location: index.php");
?>
Your form is missing Action
<form id="#contacts" method="POST" class="form" role="form" action="contact.php">

PHP Mailer script not receiving POST variables

when a contact me script is run on my website, the script doesn't seem to be grabbing the POST variables from the HTML. The HTML is as follows:
<section id="contact">
<div class="container">
<div class="row">
<div class="col-lg-12 text-center">
<h2 class="section-heading">Contact Us</h2>
<h3 class="section-subheading text-muted">Fill out a form below to make an appointment, and our top masseur will be with you as soon as possible!</h3>
</div>
</div>
<!-- To Do: Change pictures, add FA's, finish "about",and remove portfolio-->
<div class="row">
<div class="col-lg-12">
<form name="sentMessage" id="contactForm" formaction="mail/contact_me.php" method="POST" novalidate>
<div class="row">
<div id="center><div class="col-md-6">
<center><div class="form-group">
<input type="text" class="form-control" placeholder="Your Name *" id="name" name="name" required data-validation-required-message="Please enter your name.">
<p class="help-block text-danger"></p>
</div>
<div class="form-group">
<input type="email" class="form-control" placeholder="Your Email *" id="email" required data-validation-required-message="Please enter your email address.">
<p class="help-block text-danger"></p>
</div>
<div class="form-group">
<input type="tel" class="form-control" placeholder="Your Phone *" id="phone" required data-validation-required-message="Please enter your phone number.">
<p class="help-block text-danger"></p>
</div>
<div class="form-group">
<input type="tel" class="form-control" placeholder="Massage Type *" id="type" required data-validation-required-message="Please enter your type of massage.">
<p class="help-block text-danger"></p>
</div>
<div class="form-group">
<input type="tel" class="form-control" placeholder="Date of desired appointment *" id="message" required data-validation-required-message="Please enter the date">
<p class="help-block text-danger"></p>
</div>
</div>
<!--<div class="col-md-6">
<div class="form-group">
<textarea class="form-control" placeholder="Please include date, and type of massage *" id="message" required data-validation-required-message="Please enter a message."></textarea>
<p class="help-block text-danger"></p>
</div>
</div> -->
<div class="clearfix"></div>
<div class="col-lg-12 text-center">
<button type="submit" class="btn btn-xl" formaction="mail/contact_me.php">Set Appointment!</button>
</div>
</div>
</form>
</div>
</div>
</div>
</section>
The PHP Script (contact_me.php) is:
<?php
if(isset($_POST['submit'])){
$to = "*****"; // this is your Email address
$from = $_POST['email']; // this is the sender's Email address
$first_name = $_POST['name'];
$subject = "New Appointment!";
$subject2 = "Copy of your Appointment credentials.";
$message = $first_name . "'s Appointment. Phone: " . $phone . " Email: " . $_POST['email'] . " Type of Massage:" . "\n\n" . $_POST['type'] . " On:" . $_POST['message'];
$message2 = "Here is a copy of your message " . $first_name . "\n\n" . $_POST['message'];
$headers = "From:" . $from;
$headers2 = "From:" . $to;
mail($to,$subject,$message,$headers);
mail($from,$subject2,$message2,$headers2); // sends a copy of the message to the sender
echo "Mail Sent. Thank you " . $first_name . ", we will contact you shortly.";
// You can also use header('Location: thank_you.php'); to redirect to another page.
}
?>
I tried removing the isset (Or rather changing it to !isset), and the email sent. However, the credentials and the fields that were provided at the website were completely missing, and only the given text in the script was sent.
The attribute formaction is not valid. Use action instead.
In your case:
<form name="sentMessage" id="contactForm" action="mail/contact_me.php" method="POST" novalidate>
You're also missing a name attribute, as #anant kumar singh stated.

Contact form sends email but without user inputs

This bootstrap using html/php contact form sends me an email just fine, but without any of the users input information!
The email I received looks empty like this:
Name:
Email:
Subject:
Message:
The html for the form is:
<section id="partner">
<div class="container">
<div class="center wow fadeInDown">
<h2><br>Contact</h2>
<p class="lead">Send a general enquiry here or order your service here.</p>
</div>
<div class="row contact-wrap">
<div class="status alert alert-success" style="display: none"></div>
<form id="main-contact-form" class="contact-form" name="contact-form" method="post" action="sendemail.php">
<div class="col-sm-5 col-sm-offset-1">
<div class="form-group">
<label>Name *</label>
<input type="text" name="name" class="form-control" required="required">
</div>
<div class="form-group">
<label>Email *</label>
<input type="email" name="email" class="form-control" required="required">
</div>
<div class="form-group">
<label>Phone</label>
<input type="number" class="form-control">
</div>
<div class="form-group">
<label>Company Name</label>
<input type="text" class="form-control">
</div>
</div>
<div class="col-sm-5">
<div class="form-group">
<label>Subject *</label>
<input type="text" name="subject" class="form-control" required="required">
</div>
<div class="form-group">
<label>Message *</label>
<textarea name="message" id="message" required="required" class="form-control" rows="8"></textarea>
</div>
<div class="form-group">
<button type="submit" name="submit" class="btn btn-primary btn-lg" required="required">Submit Message</button>
</div>
</div>
</form>
</div><!--/.row-->
</div><!--/.container-->
</section><!--/#contact-page-->
The PHP for the form looks like this:
<?php
header('Content-type: application/json');
$status = array(
'type'=>'success',
'message'=>'Thank you for contact us. As early as possible we will contact you '
);
$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 = 'heerschapnikki#gmail.com';//replace with your email
$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;
Why isn't it working properly? The message "thank you for contacting us" pops up fine and the email sends through, but non of the users information comes through.
Try to print the values you are sending. So use this:
var_dump( $_POST['name'] );
var_dump( $_POST['email'] );
etc...
Doing this, you will be able to see wether your fields are even posted correctly.
the fields have no id param. in the post you get the "id" nor the "name"
<label>Name *</label>
<input type="text" id="name" class="form-control" required="required">
</div>
<div class="form-group">
<label>Email *</label>
<input type="email" id="email" class="form-control" required="required">

Categories