I have a simple contact form appearing on a modal in bootstrap. For some reason my form will not send. I've been looking at it for the last few hours and could use a fresh pair of eyes. It seems so simple but I'm at a total loss.
I've put a non-displayed textfield in the form as a spam filter. Since most bots would fill the textfield I want the code to send only if the textfield is blank (which it should be if the user is human since it's not displayed).
index.php
<div class = "modal fade" id = "contact" role = "dialog">
<div class = "modal-dialog">
<div class = "modal-content">
<div class = "modal-header">
<a id = "btn-close" data-dismiss = "modal">X</a>
</div>
<form>
<form method="post" action="index.php">
<fieldset id= "contact-fieldset">
<input name="name" type="text" id="name" class="text-input" placeholder="First and last name" required>
<input name="email" type="email" placeholder="Email address" required>
<textarea name="message" placeholder="Your Message" required></textarea>
<input name="bot-catch" type="text" id="bot-catch">
<input id="submit" class="submit-btn" name="submit" type="submit" value="Submit">
</fieldset>
</form>
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: Me';
$to = 'me#email.com';
$subject = 'Hello';
$human = $_POST['bot-catch'];
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if ($_POST['submit'] && $human == '') {
if (mail ($to, $subject, $body, $from)) {
echo '<p>Your message has been sent!</p>';
} else {
echo '<p>Something went wrong, go back and try again!</p>';
}
} else if ($_POST['submit'] && $human != '') {
echo '<p>You answered the anti-spam question incorrectly!</p>';
}
?>
</form>
<div class = "modal-footer">
</div>
</div>
</div>
</div>
EDIT: So I have now gotten my message to send, but only if I put the PHP code into a separate file and link to it by . Only problem is- I do not want the user to be directed off of my page once they send a message. Is there anything I can do? I still can't figure out why it won't send on my index page.
EDIT: Well I got it working!! Thank you for all your help!
Related
I am a total newbie in terms of php and that's why I always used read made codes for my contact forms. The thing is I don't know how to prevent empty inputs from being sent to my email. I have a one-input form and I would like to validate it. I really searched about a solution, but unfortunately it is not working. I hope someone can help me. I'll put here my code:
<?php
if (isset($_POST["submit"])) {
$email = $_POST['email'];
$from = 'Felipe Felix';
$to = 'email#email.com';
$subject = 'My email listing';
$body = "E-mail: $email";
if (mail ($to, $subject, $body, $from)) {
header("Location: index.html/success.html");
} else {
header("Location: index/error.html");
}
}
?>
And my HTML:
<form id="emailListing" class="form-horizontal" action="index.php" method="POST" enctype="multipart/form-data" name="sentMessage" data-fv-framework="bootstrap" data-fv-icon-valid="glyphicon glyphicon-ok" data-fv-icon-invalid="glyphicon glyphicon-remove" data-fv-icon-validating="glyphicon glyphicon-refresh">
<div class="form-group">
<div class="col-xs-8 col-lg-9">
<input id="email" class="form-control" name="email" type="email" placeholder="メールアドレス" data-fv-emailaddress-message="This is not a valid email" />
</div>
<input id="submit" name="submit" type="submit" value="Submit" class="btn btn-default col-xs-4 col-lg-3">
</div>
</form>
I am also using this (something I saw in another question, but it's not working I guess):
<script>
$(document).ready(function() {
$('#emailListing').formValidation();
});
</script>
For the html page you need to have required='required' or only required attribute in in your input-(html5 feature) which must not empty while submitting form and use it like
<input id="email" class="form-control" name="email" type="email" placeholder="メールアドレス" data-fv-emailaddress-message="This is not a valid email" required />
If you want to jquery validator in front end you can have many plugins but you need to include those code in your html page using <script> tag,in the backend for validation you just need to
<?php
if (isset($_POST["submit"])) {
$email = $_POST['email'];
if(empty($email))
{
header("Location: index/error.html");
}
$from = 'Felipe Felix';
$to = 'email#email.com';
$subject = 'My email listing';
$body = "E-mail: $email";
if (mail ($to, $subject, $body, $from)) {
header("Location: index.html/success.html");
} else {
header("Location: index/error.html");
}
}
?>
Try this..
<?php
if (isset($_POST["submit"])) {
$email = $_POST['email'];
$from = 'Felipe Felix';
$to = 'email#email.com';
$subject = 'Contact';
$body = "E-mail: $email";
if ($_POST['email']){
if (mail ($to, $subject, $body, $from)) {
header("Location: index.html");
}
else {
header("Location: index.html");
}
}
else{
header("Location: index/error.html");
}
}
?>
You can double check the empty field both client side and server side:
client side using jquery before submission:
$(document).ready(function() {
$("#emailListing").on('submit', function(e) {
e.preventDefault(); //to prevent submit before check
if ($("#email").val()!=''){
$("#emailListing").submit();
}
else
{
alert('Please fill email field');
}
});
});
server side by php after from submission:
if (!empty($_POST[email])){
// Do something
}
The thing is, after trying some of the suggestions the other users gave to me, I end up with a form that sends the email address if you input it but sends it anyway even if you let it empty (the error page comes, bu sends the email anyway). So, I tried another way and it worked! My form now prevents the user from sending empty inputs and give them a feedback if the user sends a valid email address. I don't know if it's 100% correct in the syntax, but I'm not getting any errors. Thank you all for the support! I'll put here my code:
PHP
<?php
if (isset($_POST["submit"])) {
$email = $_POST['email'];
$from = 'Me';
$to = 'email#email.com';
$subject = 'My Subject';
$body = "E-mail Address: $email";
if ($_POST['email']){
if (mail ($to, $subject, $body, $from)) {
header("Location: http://website.com/success.html");
}
else{
header("Location: http://website.com/error.html");
}
}
else{
header("Location: http://website.com/error.html");
}
}
?>
HTML (I'm using bootstrap, that's why all those classes)
<form id="emailListing" class="form-horizontal" action="index.php" method="POST" enctype="multipart/form-data" name="sentMessage" onsubmit="return validateForm()">
<div class="form-group">
<div class="col-xs-8 col-lg-9">
<input id="email" class="form-control" name="email" type="email" placeholder="E-mail Address" />
</div>
<input id="submit" name="submit" type="submit" value="Subscribe" class="btn btn-default col-xs-4 col-lg-3">
</div>
</form>
JS
<script>
function validateForm() {
var x = document.forms["sentMessage"]["email"].value;
if (x == null || x == "") {
alert("メールアドレスを入力してください");
return false;
}
}
</script>
I have a contact form on my website. I'd like to change the text of the submit button to say "submitted" after the form has successfully submitted, and maybe even make it say "submitting" while the form is submitting. I am unsure of how to do this, i could do an onclick event that would change the text, but not the route i want to take as the message could fail to send and the button would still say submitted.
Here is my html for the form
<form method="post" action="contact.php">
<input type="text" name="name" placeholder="Name"><br>
<input type="email" name="email" placeholder="Email"><br>
<textarea rows="8" cols="65" name="message"placeholder="Message"></textarea><br>
<input id="submit" type="submit" name="submit" value="Let's Get In Touch">
</form>
and here is my php code:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: Portfolio Website';
$to = 'kyle.a.binger#gmail.com';
$subject = 'Message From Personal Site';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if ($_POST['submit']) {
if (mail ($to, $subject, $body, $from)) {
echo '<p>Your message has been sent!</p>';
} else {
echo '<p>Something went wrong, go back and try again!</p>';
}
}
?>
Is there a way to do this with my existing php code? Thanks in advance for any help.
What you are going to need is something to pass the data to the php script, and return something/echo something back without leaving the page.
Take a look into AJAX. You will be able to exactly this.
Here's a link to one of the first posts on stackoverflow that showed up.
Here's a link to w3schools to give you a quick example/idea.
If you don't want to use AJAX and you're posting to page itself you can do the following
<form method="post" action=""> <!-- removed the PHP file name to post to itself -->
<input type="text" name="name" placeholder="Name"><br>
<input type="email" name="email" placeholder="Email"><br>
<textarea rows="8" cols="65" name="message"placeholder="Message"> </textarea><br>
<?php
if (isset($_POST['submit'])) {
echo '<input id="submit" type="button" name="submit" value="Submitted">'; //Changed type submit to button
} else {
echo '<input id="submit" type="submit" name="submit" value="Let\'s Get In Touch">';
}
?>
</form>
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: Portfolio Website';
$to = 'kyle.a.binger#gmail.com';
$subject = 'Message From Personal Site';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if ($_POST['submit']) {
if (mail ($to, $subject, $body, $from)) {
echo '<p>Your message has been sent!</p>';
} else {
echo '<p>Something went wrong, go back and try again!</p>';
}
}
?>
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 7 years ago.
I have a PHP mail script set up and keep hitting the error "Something went wrong, go back and try again!" all the form fields have been checked and all the names match etc so I am wondering if there is something wrong with my script?
<form method="post" action="contact.php" id="contactForm">
<label for="name">Name</label>
<input type="text" id="name" name="name" class="name" />
<label for="email">Email</label>
<input type="text" id="email" name="email" class="email" />
<label for="phone">Phone</label>
<input type="text" id="phone" name="phone" class="phone" />
<label for="iam">I Am</label>
<select name="iam" class="iam" id="iam">
<option>a recruiter looking to recruit staff</option>
<option>a construction worker looking for work</option>
<option>requesting information</option>
</select>
<label for="message">Message</label>
<textarea name="message" id="message" class="message"></textarea>
<label for="captcha">What is 3+4?</label>
<input type="text" id="captcha" name="captcha" class="captcha" />
<input type="submit" name="submit" id="submit" class="submit" />
</form>
<?php
$name = $_POST['name'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$iam = $_POST['iam'];
$human = $_POST['captcha'];
$message = $_POST['message'];
$from = 'From: Test';
$to = 'sales#test.com';
$headers = "From: $email";
$subject = 'Tradeline Contact';
$body = "From: $name\n E-Mail: $email\n Phone Number:\n $phone I Am:\n $iam Message:\n $message";
if ($_POST['submit'] && $human == '7') {
if (mail($to, $subject, $body, $headers, "-f " . $from)) {
echo '<p>Your message has been sent!</p>';
header( 'Location: http://urlhere.com/thankyou.html' ) ;
} else {
echo '<p>Something went wrong, go back and try again!</p>';
}
} else if ($_POST['submit'] && $human != '7') {
echo '<p>You answered the anti-spam question incorrectly!</p>';
}
?>
Any help is appreciated.
You might want to change:
mail($to, $subject, $body, $headers, "-f " . $from)
to:
mail($to, $subject, $body, $headers."\r\n")
That way your mail headers will be in compliance.
Also, turn on error reporting. I happen to use error_reporting(7); right under the <?php line to turn on all common errors with the exception of catching undefined variables, and that will tell me if the mail function has problems.
Another thing you can do is check the mail server logs to see if mail is actually being sent.
I'm sure you already did this, but in case you haven't, make sure you use valid email addresses.
I just followed this tutorial on how to create a contact form with PHP. Now everything works fine but when I submit a form it returns a message on a new blank page. I want this to happen on the current page underneath the contact form.
This is my first time ever doing anything with PHP so I have no idea how I would do this. In the tutorial it is briefly mentioned that you can just put the script anywhere you would want the message to appear but it doesn't seem to work for me.
This is my HTML code:
<form method="post" action="contact.php">
<label>Name</label>
<input name="name" placeholder="John Doe">
<label>Email</label>
<input name="email" type="email" placeholder="john#doe.com">
<label>Message</label>
<textarea name="message" placeholder="Hello..."></textarea>
<label id="antispam">What is 2+2? (Anti-spam)</label>
<input id="antispambox" name="human" placeholder="4">
<input id="submit" name="submit" type="submit" value="Submit">
</form>
and this is my PHP code:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: TangledDemo';
$to = 'contact#tangledindesign.com';
$subject = 'Hello';
$human = $_POST['human'];
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if ($_POST['submit']) {
if ($name != '' && $email != '') {
if ($human == '4') {
if (mail ($to, $subject, $body, $from)) {
echo '<p>Your message has been sent!</p>';
} else {
echo '<p>Something went wrong, go back and try again!</p>';
}
} else if ($_POST['submit'] && $human != '4') {
echo '<p>You answered the anti-spam question incorrectly!</p>';
}
} else {
echo '<p>You need to fill in all fields!!</p>';
}
}
?>
I've already found some answers but none of which make any sense to me. I also tried inspecting the example on the tutorial's site but, ofcourse I can't access the PHP.
Can anybody explain this to me?
If you want the message on the same page then you have to put the php code on the same page like this:
<form method="post" action="">
<label>Name</label>
<input name="name" placeholder="John Doe">
<label>Email</label>
<input name="email" type="email" placeholder="john#doe.com">
<label>Message</label>
<textarea name="message" placeholder="Hello..."></textarea>
<label id="antispam">What is 2+2? (Anti-spam)</label>
<input id="antispambox" name="human" placeholder="4">
<input id="submit" name="submit" type="submit" value="Submit">
</form>
<?php
if(!empty($_POST['name'])&&!empty($_POST['email'])&&!empty($_POST['message'])&&!empty($_POST['human']))// check if everything has been filled out before doing anything else
{
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: TangledDemo';
$to = 'contact#tangledindesign.com';
$subject = 'Hello';
$human = $_POST['human'];
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if ($_POST['submit']) {
if ($name != '' && $email != '') {
if ($human == '4') {
if (mail ($to, $subject, $body, $from)) {
echo '<p>Your message has been sent!</p>';
} else {
echo '<p>Something went wrong, go back and try again!</p>';
}
} else if ($_POST['submit'] && $human != '4') {
echo '<p>You answered the anti-spam question incorrectly!</p>';
}
} else {
echo '<p>You need to fill in all fields!!</p>';
}
}
}
?>
From what you appear to be asking you need to look into AJAX. You'll need to use a JavaScript library such as jQuery to make it easier. It will do exactly what you're looking for. Post the form data to your "contact.php" and the returned message (success/error) straight back to the page you are currently on. No redirect, no refresh.
There are different ways to go about this. AJAX is one of them, but since you are just starting out my opinion is to avoid that - learn the basics first. The code below is just that - the basics.
This method entails having the form and the processing on the same page. This is generally not the best method, but it is probably the simplest - and again, my opinion is that when you are starting out, simplicity is your friend.
contact.php
<?php
// First, check if the submit button has been pressed - no processing occurs unless the form has been submitted.
// This is necessary because your $_POST variables do not exist until the form is submitted.
if (isset($_POST['submit'])) {
// The button has been pressed, so now gather data and set variables
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: TangledDemo';
$to = 'contact#tangledindesign.com';
$subject = 'Hello';
$human = $_POST['human'];
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
// Now, validate and process
if ($name != '' && $email != '') {
if ($human == '4') {
if (mail ($to, $subject, $body, $from)) {
echo '<p>Your message has been sent!</p>';
} else {
echo '<p>Something went wrong, go back and try again!</p>';
}
} else {
echo '<p>You answered the anti-spam question incorrectly!</p>';
}
} else {
echo '<p>You need to fill in all fields!!</p>';
}
}
?>
<form method="post" action="contact.php">
<label>Name</label>
<input name="name" placeholder="John Doe">
<label>Email</label>
<input name="email" type="email" placeholder="john#doe.com">
<label>Message</label>
<textarea name="message" placeholder="Hello..."></textarea>
<label id="antispam">What is 2+2? (Anti-spam)</label>
<input id="antispambox" name="human" placeholder="4">
<input id="submit" name="submit" type="submit" value="Submit">
</form>
Keep in mind that, while this method works, in the long run it is not the best approach. It is better to keep logic and markup separate. That would mean having at least two pages - one that displays the form, and another page that processes the form. Even better approaches will involve object oriented programming. However, I am a firm believer in the ready, fire, aim methodology... when you are trying to learn, get it to work first, worry about best practices and optimization later. As you run into bigger problems and the need for more complexity arises, you can make adjustments and figure out better ways to do things. Progress only comes from practice, trial, and error. Good luck.
I'm staging a site for a client and I'm trying to create a form from scratch rather than use a plugin.
I'm not sure where I'm going wrong. The page keeps refreshing to the homepage and no email gets sent.
Could someone please point out in my code where I've gone wrong...
Thanks in advance!
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: Test';
$to = 'email#example.com';
$subject = 'Hello';
if ($name == "" OR $email == "") {
echo "You must specify a value for name, email address, and message.";
exit;
}
foreach( $_POST as $value ){
if( stripos($value,'Content-Type:') !== FALSE ){
echo "There was a problem with the information you entered.";
exit;
}
}
require_once("assets/inc/phpmailer/class.phpmailer.php");
$mail = new PHPMailer();
if (!$mail->ValidateAddress($email)){
echo "You must specify a valid email address.";
}
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
header("Location: http://natashamcdiarmid.com/clients/JLP/wp/contact/?status=thanks");
exit;
}
?>
<?php if (isset($_GET["status"]) AND $_GET["status"] == "thanks") { ?>
<p>Thanks, I'll get back to your shortly!</p>
<?php } else ?>
<form method="post" action="contact">
<p><label>Name</label></p>
<input name="name" placeholder="Type Here">
<p><label>Email</label></p>
<input name="email" type="email" placeholder="Type Here">
<p><label>Message</label></p>
<textarea name="message" placeholder="Type Here"></textarea>
<p><label>*What is 2+2?</label></p>
<input name="human" placeholder="Type Here">
<p><input id="submit" name="submit" type="submit" value="Submit"></p>
<?php
if ($_POST['submit']) {
if (mail ($to, $subject, $body, $from)) {
echo '<p>Your message has been sent!</p>';
} else {
echo '<p>Something went wrong, go back and try again!</p>';
}
}
?>
</form>
The form that you are posting to isn't much of a directory.
<form method="post" action="contact">
should it be contact.php?
this action should be the directory of your form handler
A major issue with WordPress that always gets me is that it uses some request variables with common names, and messing with them causes unpredictable errors. For instance, the name parameter is used to locate and display the current post or web page.
Try renaming your name field to something else, like your_name.
When I create forms for use in WordPress, typically I prefix every field with something custom, like acme_contact_name, acme_contact_email, etc. A little more typing, but safer.