I know that there has been a couple of threads like that, I have looked through them and I can't seem to be able to find an answer. So I have a HTML form with a PHP file action and method POST. Here is the form:
<div class="col-md-4 col-sm-12">
<div class="contact-form bottom">
<h2>Send a message</h2>
<form id="main-contact-form" name="contact-form" method="POST" action='http://creobox.ie/site/sendemail.php'>
<div class="form-group">
<input type="text" name="name" class="form-control" required="required" placeholder="Name">
</div>
<div class="form-group">
<input type="email" name="email" class="form-control" required="required" placeholder="Email Id">
</div>
<div class="form-group">
<textarea name="message" id="message" required="required" class="form-control" rows="8" placeholder="Your text here"></textarea>
</div>
<div class="form-group">
<input type="submit" name="submit" class="btn btn-submit" value="Submit">
</div>
</form>
</div>
</div>
Please note, I tried using action ="sendemail.php" with no effect.
Then I am trying to GET the data in the PHP file, like so:
$name = #trim(stripslashes($_POST['name']));
$from = #trim(stripslashes($_POST['email']));
$subject = #trim(stripslashes($_POST['subject']));
$message = #trim(stripslashes($_POST['message']));
The rest of the code is just composing the e-mail etc - this part works fine.
Am I missing something very simple? The code looks to be fine! I did echo on $name, $from, $subject and $message and all I got was
Details : <br/>
No errors showing up or anything... Thanks for any help!
Ok, so I tried to go with the AJAX... I modified my form to be
<div class="contact-form bottom">
<h2>Send a message</h2>
<form id="main-contact-form" name="contact-form">
<div class="form-group">
<input type="text" name="name" class="form-control" required="required" placeholder="Name">
</div>
<div class="form-group">
<input type="email" name="email" class="form-control" required="required" placeholder="Email Id">
</div>
<div class="form-group">
<textarea name="message" id="message" required="required" class="form-control" rows="8" placeholder="Your text here"></textarea>
</div>
<div class="form-group">
<input type="submit" name="submit" class="btn btn-submit" value="Submit" href="javascript: void(0);" onclick = "sendMail();">
</div>
</form>
</div>
and created a js function
function sendMail()
{
var name = document.getElementById('name');
var email = document.getElementById('email');
var message = document.getElementById('message');
$.ajax({
type: 'POST',
url: 'sendemail.php',
data: {
'name' : name,
'email' : email,
'message' : message
},
success: function(json) {
if (json.status == 'S')
alert('Duplicates removed!');
},
error: function() {}
});
}
But that doesn't work either... I just get a 304 - Not Modified error with a cached index page. This is starting to burn my brain! Could I be using an old version of jquery, hence all the problems?!
Related
I am using PHPMailer to send email from my Gmail account to an email from a submitted form on my index.html page. This works perfectly fine. But since I am using the send.php file in the action attribute, the send.php page appears after form submission. I want to stay on the index.html page and let send.php work in the background and alert on the index.html page, when the email is sent.
Note: There isn't any SQL or any database job here. I am using PHPMailer in send.php to send an email from my Gmail account to the user submitted Gmail id in the form.
<form action="send.php" class="form" id="frm-js" method="POST">
<div class="form__group">
<input type="text" class="form__input" id="name" name="name" placeholder="Full Name" required>
<label for="name" class="form__label">Full Name</label>
</div>
<div class="form__group">
<input type="text" class="form__input" id="email" name="email" placeholder="Email Address" required>
<label for="email" class="form__label">Email Address</label>
</div>
<div class="form__group">
<input type="text" class="form__input" id="phone" name="phone" placeholder="Phone Number">
<label for="phone" class="form__label">Phone Number</label>
</div>
<div class="form__group">
<textarea id="message" rows="4" cols="50" name="message" class="textarea-frm form__input" maxlength="1000" placeholder="Your message"></textarea>
</div>
<div class="form__group">
<button class="btn" type="submit" name="submit">Book</button>
</div>
</form>
You can use jQuery. Try this: remove action from the form tag. I add a p to show the message from send.php:
<form class="form" id="frm-js" method="POST">
<p id="resp_msg"></p>
<div class="form__group">
<input type="text" class="form__input" id="name" name="name" placeholder="Full Name" required>
<label for="name" class="form__label">Full Name</label>
</div>
<div class="form__group">
<input type="text" class="form__input" id="email" name="email" placeholder="Email Address" required>
<label for="email" class="form__label">Email Address</label>
</div>
<div class="form__group">
<input type="text" class="form__input" id="phone" name="phone" placeholder="Phone Number">
<label for="phone" class="form__label">Phone Number</label>
</div>
<div class="form__group">
<textarea id="message" rows="4" cols="50" name="message" class="textarea-frm form__input" maxlength="1000" placeholder="Your message"></textarea>
</div>
<div class="form__group">
<button class="btn" type="submit" name="submit">Book</button>
</div>
</form>
And add a jQuery function (integrate jQuery if you haven't already library):
$("#frm-js").on("submit", function(event){
event.preventDefault();
var data = $( this ).serialize();
$.post("send.php",{data:data }, function (data) {
// Do other stuff like
// reset form items
// Show message response
$("#resp_msg").text(data).show();
});
});
This is send.php and is how it should be:
if(isset($_POST['data'])) {
$params = array();
parse_str($_POST['data'], $params);
// Send mail
echo "Hi, " . $params['name'] . " your email has been sent";
}
I'm working on a site and the goal was to submit a contact form without reloading the whole page. I tried different paths, but nothing seems to help. It looks like the "on submit" part doesn't work at all I'm still leaving site to contact_form.php. I would be very thankful for any help, thanks in advance
This is js part:
$("#contact-form").on('submit', function(){
var name = $("#form_name").val();
var lastname = $("#form_lastname").val();
var email = $("#form_email").val();
var subject = $("#form_subject").val();
var message = $("#form_message").val();
var dataString = 'name=' + name + '&lastname=' + lastname + '&email=' + email +'&subject'+subject + '&message=' + message;
$.ajax({
type: "POST",
url: "contact_form.php",
data: dataString,
cache: false,
success: function(result){
alert(result);
}
});
return false;
});
and here html:
<form id="contact-form" method="post" action="contact_form.php" role="form">
<div class="messages"></div>
<div class="form-row">
<div class="col-md-12">
<div class="form-group">
<input id="form_name" type="text" name="name" class="form-control" placeholder="Name" required="required" data-invalid-message="Please enter your name">
<div class="help-block with-errors"></div>
</div>
</div>
</div>
<div class ="form-row">
<div class="col-md-12">
<div class="form-group">
<input id="form_lastname" type="text" name="surname" class="form-control" placeholder="Last name" required="required" data-invalid-message="Please enter your name">
<div class="help-block with-errors"></div>
</div>
</div>
</div>
<div class="form-row">
<div class="col-md-12">
<div class="form-group">
<input id="form_email" type="text" name="mail" class="form-control" placeholder="Email" required="required" data-invalid-message="Please enter your name">
<div class="help-block with-errors"></div>
</div>
</div>
</div>
<div class="form-row">
<div class="col-md-12">
<div class="form-group">
<input id="form_subject" type="text" name="subject" class="form-control" placeholder="Subject" required="required" data-invalid-message="Please enter your name">
</div>
</div>
</div>
<div class="form-row">
<div class="col-md-12">
<div class="form-group">
<textarea id="form_message" name="message" class="form-control" placeholder="Message" rows="4" required="required" data-invalid-message="Please enter your name"></textarea>
</div>
</div>
<div class="col-md-12">
<input type="submit" class="btn btn-success" value="Send message">
</div>
</div>
</form>
You can start by passing the event and stopping the default behaviour, which would be to hit the action of the form.
$("#contact-form").on('submit', function(e){
e.preventDefault();
// rest of code... ( do not return true in the end of this handler though )
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>
Can anyone help with a php error. My contact form comes back with
404 - File or directory not found
This is my form
<form action="contact.php" class="contact-form" accept-charset="UTF-8" role="form">
<div class="form-group col-md-6">
<label class="sr-only" for="exampleInputEmail1">Your Name: *</label>
<input required type="text" class="form-control" id="inputName" placeholder="Your Name: *">
</div>
<div class="form-group col-md-6">
<label class="sr-only" for="exampleInputEmail1">Email: *</label>
<input required type="email" class="form-control" id="inputEmail1" placeholder="Email: *">
</div>
<div class="clearfix"></div>
<div class="form-group">
<label class="sr-only" for="exampleInputEmail1">Subject:</label>
<input type="text" class="form-control" id="inputName" placeholder="Subject:">
</div>
<div class="form-group">
<textarea required class="form-control" rows="5" placeholder="Message: *"></textarea>
</div>
<button type="submit" class="btn btn-lg btn-primary">Submit</button>
</form>
and this is my PHP
<?php
$mail = "jade#------.CO.UK";
if($_POST['message']) {
$message = "<h2>Hello here is a message from ".$_SERVER['SERVER_NAME']."</h2><hr>
<p><strong>Name:</strong> ".$_POST['name']."</p>
<p><strong>Email:</strong> ".$_POST['email']."</p>
<p><strong>Message:</strong> ".$_POST['message']."</p>";
$subject="Premium template message: ".$_POST['subject'];
mail($mail, $subject, $message, "Content-type: text/html; charset=utf-8 \r\n");
echo 'AAAAAAAAAAAAAAA';
}
?>
I'm quite new to this and its driving me crazy.
Thanks
Your form doesn't have a method. Since you are posting data, add the POST method in html like this
<form action="contact.php" method="POST" class="contact-form" accept-charset="UTF-8" role="form">
And hope that contact.php is in the same directory as well.
Check the proper relative contact.php file location. If you are submiting the form to the same file you are in then just remove action="contact.php". Also you need to add method. Here's how it should be. Mark action and method in the below code.
<form action="" method="post" class="contact-form" accept-charset="UTF-8" role="form">
<div class="form-group col-md-6">
<label class="sr-only" for="exampleInputEmail1">Your Name: *</label>
<input required type="text" class="form-control" id="inputName" placeholder="Your Name: *">
</div>
<div class="form-group col-md-6">
<label class="sr-only" for="exampleInputEmail1">Email: *</label>
<input required type="email" class="form-control" id="inputEmail1" placeholder="Email: *">
</div>
<div class="clearfix"></div>
<div class="form-group">
<label class="sr-only" for="exampleInputEmail1">Subject:</label>
<input type="text" class="form-control" id="inputName" placeholder="Subject:">
</div>
<div class="form-group">
<textarea required class="form-control" rows="5" placeholder="Message: *"></textarea>
</div>
<button type="submit" class="btn btn-lg btn-primary">Submit</button>
</form>
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.