I am trying to get a simple contact form to work with my wordpress site but cannot get it to work. I dont want to use any plugin, I just want to do it using PHP. So I put this file in root dir of my wordpress installation on my hostgator hosted site
www.example.com/sendmail.php
<?php
if(isset($_POST['submit'])){
$to = "myemail#yahoo.com";
$from= $_POST['email'];
$fname= $_POST['fname'];
$lname= $_POST['lname'];
$message= $_POST['message'];
$subject = "Request email";
$headers = "From:" .$from;
mail($to,$subject,$message,$headers);
}
?>
This is the contact-us form on www.example.com/contact-us
<form action="http://www.example.com/sendmail.php" method="post">
<fieldset>
<div class="form-group">
<div class="col-md-12">
<input id="fname" name="name" type="text" placeholder="First Name" class="form-control">
</div>
</div>
<div class="form-group">
<div class="col-md-12">
<input id="lname" name="name" type="text" placeholder="Last Name" class="form-control">
</div>
</div>
<div class="form-group">
<div class="col-md-12">
<input id="email" name="email" type="text" placeholder="Email Address" class="form-control">
</div>
</div>
<div class="form-group">
<div class="col-md-12">
<input id="phone" name="phone" type="text" placeholder="Phone" class="form-control">
</div>
</div>
<div class="form-group">
<div class="col-md-12">
<textarea class="form-control" id="message" name="message" placeholder="Enter your massage for us here. We will get back to you within 2 business days." rows="7"></textarea>
</div>
</div>
<div class="form-group">
<div class="col-md-12 text-center">
<!-- <button type="submit" class="btn btn-primary btn-lg">Submit</button>-->
<input type="submit" name="submit" class="btn btn-primary btn-lg" value="Submit">
</div>
</div>
</fieldset>
</form>
If i try to visit this page www.example.com/sendmail.php, it gives 200 success ok message. However if I try to fill the form and then it sends the email, i am not able to recieve it.
Is there anything I am missing or I need to check ?
You are getting these variables $fname= $_POST['fname']; $lname= $_POST['lname']; on sendmail.php
But your html form input fields does not have name for fname and lname .
So use the below one :
<div class="form-group">
<div class="col-md-12">
<input id="fname" name="fname" type="text" placeholder="First Name" class="form-control">
</div>
</div>
<div class="form-group">
<div class="col-md-12">
<input id="lname" name="lname" type="text" placeholder="Last Name" class="form-control">
</div>
</div>
Instead of :
<div class="form-group">
<div class="col-md-12">
<input id="fname" name="name" type="text" placeholder="First Name" class="form-control">
</div>
</div>
<div class="form-group">
<div class="col-md-12">
<input id="lname" name="name" type="text" placeholder="Last Name" class="form-control">
</div>
</div>
Hope it helps you.
Related
The email I receive only shows the message, not the email address or phone number or even the name.
It also doesn't show from which email it has been sent.
I should receive whole information from the form, otherwise it wouldn't show me the message, I guess.
Maybe someone has more simple code than this one?
I tried but I'm stuck.
Can someone help me out?
Below is the HTML and PHP code
HTML Code:
<form name="mail" action="mail.php" method="post" >
<div class="form-group ">
<input id="name" class="form-control" placeholder="Name" type="text" required>
<label for="name" class="sr-only">Name</label>
</div>
<div class="form-group ">
<label for="email" class="sr-only">Email</label>
<input id="email" class="form-control" placeholder="Email" type="email" required>
</div>
<div class="form-group ">
<label for="phone" class="sr-only">Phone</label>
<input id="phone" class="form-control" placeholder="Phone" type="text" required>
</div>
<div class="form-group ">
<label for="message" class="sr-only">Message</label>
<textarea name="message" id="message" cols="30" rows="5" class="form-control" placeholder="Message" required></textarea>
</div>
<div class="form-group ">
<input class="btn btn-primary btn-lg" value="Send Message" name="submit" type="submit">
</div>
</div>
</form>
PHP code:
<?php
if(!isset($_POST['submit']))
{
//This page should not be accessed directly. Need to submit the form.
echo "error; you need to submit the form!";
}
$name = $_POST['name'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$message = $_POST['message'];
$email_from = "$email";//<== update the email address
$email_subject = "New Form submission";
$email_phone = $phone;
$email_body = "You have received a new message from the user ".$name.
"\nHere is the message:\n ".$message.
"\nThe phone number:\n ".$phone.
"The email:\n ".$email.
$to = "info#czwebdesign.be";//<== update the email address
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $email \r\n";
//Send the email!
mail($to,$email_subject,$email_body,$headers);
//done. redirect to thank-you page.
header('Location: index.html');
?>
You also need to use name attribute for other fields as well same like message field, something like:
<input name="email" class="form-control" placeholder="Email" required>
<input name="name" class="form-control" placeholder="NAme" required>
<input name="phone" class="form-control" placeholder="Phone" required>
Otherwise you will get the Undefined Index warning in your script.
It's better to use php error_reporting() in your development mode, this will help to find out all errors and warnings in your code.
// Same as error_reporting(E_ALL);
ini_set('error_reporting', E_ALL);
// Report all PHP errors (see changelog)
error_reporting(E_ALL);
Try this
<form name="mail" action="mail.php" method="post" >
<div class="form-group ">
<input id="name" class="form-control" placeholder="Name" type="text" name="name" required>
<label for="name" class="sr-only">Name</label>
</div>
<div class="form-group ">
<label for="email" class="sr-only">Email</label>
<input id="email" name="email" class="form-control" placeholder="Email" type="email" required>
</div>
<div class="form-group ">
<label for="phone" class="sr-only">Phone</label>
<input id="phone" name="phone" class="form-control" placeholder="Phone" type="text" required>
</div>
<div class="form-group ">
<label for="message" class="sr-only">Message</label>
<textarea name="message" id="message" cols="30" rows="5" class="form-control" placeholder="Message" required></textarea>
</div>
<div class="form-group ">
<input class="btn btn-primary btn-lg" value="Send Message" name="submit" type="submit">
</div>
</div>
</form>
Look here at the missing name="" attribute in your <input>
Use this code instead:
<form name="mail" action="mail.php" method="post" >
<div class="form-group ">
<input name="name" id="name" class="form-control" placeholder="Name" type="text" required>
<label for="name" class="sr-only">Name</label>
</div>
<div class="form-group ">
<label for="email" class="sr-only">Email</label>
<input name="email" id="email" class="form-control" placeholder="Email" type="email" required>
</div>
<div class="form-group ">
<label for="phone" class="sr-only">Phone</label>
<input name="phone" id="phone" class="form-control" placeholder="Phone" type="text" required>
</div>
<div class="form-group ">
<label for="message" class="sr-only">Message</label>
<textarea name="message" id="message" cols="30" rows="5" class="form-control" placeholder="Message" required></textarea>
</div>
<div class="form-group ">
<input class="btn btn-primary btn-lg" value="Send Message" name="submit" type="submit">
</div>
</div>
</form>
I am recently getting into php and am running it locally on Xampp. I have created the following simple form followed by a few lines of php. It seems that no data is being passed through from the html form to the php page.
<form method="post" action="emailform.php" enctype="text/plain" class="form-horizontal">
<div class="row input">
<div class="col-md-1"></div>
<div class="col-md-10">
<div class="form-group">
<input type="text" name="subject" id="subject" class="form-control" placeholder="subject">
</div>
</div>
</div>
<div class="row input">
<div class="col-md-1"></div>
<div class="col-md-4">
<div class="form-group">
<input type="text" name="name" id="name" class="form-control" placeholder="full name">
</div>
<div class="form-group">
<input type="email" name="subject" id="email" class="form-control" placeholder="email">
</div>
</div>
<div class="col-md-1"></div>
<div class="col-md-5 textarea">
<textarea class="form-control" name="message" id="message" rows="4" placeholder="message"></textarea>
</div>
</div>
<br>
<div class="row">
<div class="col-md-1"></div>
<div class="col-md-10">
<button type="submit" class="btn" id="submit">Send</button>
</div>
</div>
</form>
This is the simple php code I am using to test:
<?php
$subject = 'No subject was set';
if (isset($_POST['subject'])) {
$subject = ($_POST['subject']);
}
echo "This is the subject:$subject";
?>
I appreciate any help as I have been struggling with this simple code for the past week now.
Two subject names!
<input type="email" name="subject" id="email" class="form-control" placeholder="email">
^
and
<input type="text" name="subject" id="subject" class="form-control" placeholder="subject">
^
Also remove enctype="text/plain" from the form
It's because PHP doesn't handle it
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have a problem that might be a syntax problem but I can't seem to figure out what I am doing wrong.
I have created a form and when I click on submit, the data in the form is not sent to my mysql database.
Here is my html code
<div class="content-wrapper">
<div class="container">
<div class="row">
<div class="col-md-10">
<h1 class="page-head-line">Forms </h1>
</div>
</div>
<div class="row">
<div class="col-md-10">
<div class="panel panel-default">
<div class="panel-heading">
BASIC FORM ELEMENTS
</div>
<div class="panel-body">
<form method="post" action="insert.php" >
<div class="form-group">
<label for="name">Name</label>
<input name="name' type="text" class="form-control" id="name" placeholder="Enter your name" required/>
</div>
<div class="form-group">
<label for="project_num">OIT-GIS Project Number</label>
<input name="project_num' type="text" class="form-control" id="project_num" placeholder="OIT-GIS Project Number" />
</div>
<div class="form-group">
<label for="project_name">Project Name</label>
<input name="name' type="text" class="form-control" id="project_name" placeholder="Project Name" required/>
</div>
<div class="form-group">
<label for="easyvista">EasyVista Ticket Number</label>
<input name="easyvista' type="text" class="form-control" id="easyvista" placeholder="EasyVista Ticket Number" />
</div>
<div class="form-group">
<label for="agency">Requestor/Agency</label>
<input name="agency' type="text" class="form-control" id="agency" placeholder="Requestor or Agency" />
</div>
<div class="form-group">
<label for="description">Description of Work:</label>
<input name="description' type="text" class="form-control" id="agency" placeholder="Description" />
</div>
<div class="form-group">
<label for="input-date">Enter Today Date</label>
<input name="input-date' type="date" value="">
<span class="result"></span>
</div>
<div class="form-group">
<div class="col-md-10">
<input id="submit" name="submit" type="submit" class="btn btn-primary">
</div>
</div>
</form>
</div>
</div>
and here is my php
<?php
echo $POST;
error_reporting(E_ALL);
ini_set('display_errors', 1);
include("../includes/config.php");
if (isset($_POST['submit'])) {
echo $_POST['submit'];
$name = $_POST['name'];
$projectnum = $_POST['project_num'];
$projectname = $_POST['project_name'];
$easyvista = $_POST['easyvista'];
$agency = $_POST['agency'];
$description = $_POST['description'];
$startDate = $_POST['input-date'];
$sql="INSERT INTO statusreport(name, project_num, project_name, easyvista, agency, description)
VALUES
('$name','$projectnum', '$projectname', '$easyvista', '$agency', '$description')";
if (!mysqli_query($conn, $sql))
{
die('Error: ' . mysqli_connect_error($conn));
}
echo "Entry is recored <br/>";
echo "Name:", $name, "<br/>";
echo "test..................<br/>", $name;
/*header("location: http://10.1.7.129//gisadmin/admin/forms.php");*/
//echo "<script>setTimeout(\"location.href = 'http://10.1.7.129//gisadmin/admin/forms.php';\",700);</script>";
mysqli_query($conn, $sql);
}
else {
echo "No data";
}
?>
Any help would be greatly appreciated.
Thanks
You have a mixing of single and double quotes here, the name attributes are opening the value with double quotes and closing with single quotes, should be as follows:
<form method="post" action="insert.php" >
<div class="form-group">
<label for="name">Name</label>
<input name="name" type="text" class="form-control" id="name" placeholder="Enter your name" required/>
</div>
<div class="form-group">
<label for="project_num">OIT-GIS Project Number</label>
<input name="project_num" type="text" class="form-control" id="project_num" placeholder="OIT-GIS Project Number" />
</div>
<div class="form-group">
<label for="project_name">Project Name</label>
<input name="project_name" type="text" class="form-control" id="project_name" placeholder="Project Name" required/>
</div>
<div class="form-group">
<label for="easyvista">EasyVista Ticket Number</label>
<input name="easyvista" type="text" class="form-control" id="easyvista" placeholder="EasyVista Ticket Number" />
</div>
<div class="form-group">
<label for="agency">Requestor/Agency</label>
<input name="agency" type="text" class="form-control" id="agency" placeholder="Requestor or Agency" />
</div>
<div class="form-group">
<label for="description">Description of Work:</label>
<input name="description" type="text" class="form-control" id="agency" placeholder="Description" />
</div>
<div class="form-group">
<label for="input-date">Enter Today Date</label>
<input name="input-date" type="date" value="">
<span class="result"></span>
</div>
<div class="form-group">
<div class="col-md-10">
<input id="submit" name="submit" type="submit" class="btn btn-primary">
</div>
</div>
</form>
And then, as #Fred -ii stated in his comment, the php script is wrong:
echo $POST;
That line is wrong, there is no variable with name $POST before that code.
Are you getting success message but database is not getting updated OR Getting Total Error...???
1) Check for double quotes and single quotes..
<div class="form-group">
<label for="name">Name</label>
<input name="name" type="text" class="form-control" id="name" placeholder="Enter your name" required/>
</div>
2) Also check for path... for
include("../includes/config.php");
Is it correct or not...?
3)
<label for="project_name">Project Name</label>
<input name="name' type="text"
SHOULD BE
<label for="project_name">Project Name</label>
<input name="project_name" type="text"
I've made a contact form for my website, included the html and the php, it does send an email which does show "name: email: message:" but after that it's empty, so it doesn't show what you type in the form, please help!
Here is the html:
<form action="thankyou.php" method="post">
<div class="col-md-5" style="margin-top:15px;">
<div class="form-group">
<label for="contact-name" class="col-sm-2 control-label">Naam</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="contact-name" placeholder="First & Last name" />
</div>
</div>
<div class="form-group">
<label for="contact-email" class="col-sm-2 control-label" style="margin-top:15px;">Email</label>
<div class="col-sm-10" style="margin-top:15px;">
<input type="text" class="form-control" id="contact-name" placeholder="example#domain.com" />
</div>
</div>
<div class="form-group">
<label for="contact-message" class="col-sm-2 control-label" style="margin-top:15px;">Message</label>
<div class="col-sm-10" style="margin-top:15px;">
<textarea class="form-control" rows="4"></textarea>
</div>
</div>
<button style="btn-align:center; margin-left:88px; margin-top:15px;" type="submit" class="btn btn-primary">Verstuur</button>
</div>
</div>
</div>
</form>
Here is the php:
<?
$name = $_POST['contact-name'];
$email = $_POST['contact-email'];
$message = $_POST['contact-message'];
$email_message = "
Name:".$name."
Email:".$email."
Message:".$message."
";
mail ( "email#example.com" , "New inquiry", $email_message);
?>
Your form fields are all missing the name attribute. Without it that data is not sent.
<input type="text" class="form-control" name="contact-name" id="contact-name" placeholder="First & Last name" />
^^^^
This is missing for all form fields
I Have two forms on my site. One works fine and the other sends email with email_from: etc but doesn't capture any of the form data.
Wondering what it may be. I can post the form that is working along with it's html too if that would help debug. Very much a novice and built form using stack overflow/other sites.
Coffee
<div class="form-group">
<label class="col-sm-3 control-label">Quantity</label>
<div class="col-sm-4">
<input type="text" name="quantity" class="form-control" placeholder="Quantity : " required>
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">Name</label>
<div class="col-sm-6">
<input type="text" name="name" class="form-control" placeholder="Name : " required>
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">Email address</label>
<div class="col-sm-6">
<input type="email" name="email" class="form-control" placeholder="Email address : " required>
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">Shipping Address</label>
<div class="col-sm-6">
<textarea class="form-control" name="shipping_address" rows="8" placeholder="Shipping Address : " required></textarea>
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">Payment Method</label>
<div class="col-sm-6">
<select class="form-control" required>
<option value="Paypal">Paypal</option>
</select>
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">Notes</label>
<div class="col-sm-6">
<textarea class="form-control" name="notes" rows="8" placeholder="Notes : "></textarea>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-3 col-sm-10">
<button type="submit" class="btn btn-black">Order Now</button></a>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
And here is the php
<?php
if(isset($_POST ['submit']))
{
$coffee = ($_POST['coffee']));
$quantity = ($_POST['quantity']));
$name = ($_POST['name']));
$email = ($_POST['email']));
$shipping_address = ($_POST['shipping_address']));
$notes = ($_POST['notes']));
}
$email_from ='paradigmcoffee#gmail.com';
$email_subject="New Order Submission";
$email_body ="You have received a new message from user $name.\n".
"Email_address:$email\n".
"Coffee: $coffee\n".
"Quantity: $quantity\n".
"Shipping_Address: $shipping_address\n".
"Notes: $notes\n".
$to ="paradigmcoffee#gmail.com";
$headers = "From: $email \r\n";
mail($to,$email_from,$email_subject,$email_body,$headers);
header("Location: http://www.paradigmcoffee.co/order_thanks.html");
?>
From what you have supplied, likely the reason you can not get data is because you are not sending anything called submit. Try naming your button:
<!-- name="submit" added -->
<button name="submit" type="submit" class="btn btn-black">Order Now</button>
You can do this or make a hidden field:
<input type="hidden" name="submit" value="1" />