Form isn't sending - php

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.

Related

PHP Contact Form Failing

I'm wondering what is going wrong in this contact form. I'm not sure why, but it constantly defaults to the else and says Something went wrong, please try again later. I can't figure out why, and the error_log isn't showing anything. Is there anything glaringly obvious I'm missing? I'm pretty new to PHP.
PHP
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$captcha = $_POST['captcha'];
$from = 'From: HankSmith.com Contact Form';
$to = 'thatbraxjohnsonguy#gmail.com';
$subject = 'HANK SMITH CONTACT FORM';
$body = "
From: $name\n
E-Mail: $email\n
Phone: $phone\n
Message: $message\n";
if ($_POST['submit'] and $captcha == 4) {
if (mail ($to, $subject, $body, $from)) {
echo '<p style="margin-top: 150; text-align:center; font-size: 18px;">Your message has been sent! Click here to return to the website.</p>';
} else {
echo '<p>Something went wrong, try again later!</p>';
}
}
?>
HTML Form
<form class="contactform" method="post" action="php/contact.php">
<h3>Name</h3>
<input class="form inputboxes" type="text" name="name">
<h3>Email</h3>
<input class="form inputboxes" type="text" name="email">
<h3>Phone</h3>
<input class="form inputboxes" type="text" name="phone">
<h3>Message</h3>
<textarea class="form inputboxes" name="message"></textarea>
<h3 class="captchastyle">Are you real? What is 2 + 2?</h3><input class="captcha captchastyle" type="text" name="captcha" maxlength="1">
<input class="form submit" name="submit" type="submit" value="Submit">
</form>
When I copied this into my text editor, it threw a parse error because the echo statement in the if (mail)... section didn't have a closing brace. I never use 'and', I usually use '&&', but it actually doesn't seem to matter in this case.
if ($_POST['submit'] && $captcha == 4) {
if (mail ($to, $subject, $body, $from)) {
echo '<p style="margin-top: 150; text-align:center; font-size: 18px;">Your message has been sent! Click here to return to the website.</p>';
} // THIS MUST BE HERE
} else {
echo '<p>Something went wrong, try again later!</p>';
}
Also, you probably want an 'else' condition for that if (mail)... statement. If you're seeing a blank page, it will be because you aren't handling what happens if the mail() function returns false:
if (mail ($to, $subject, $body, $from)) {
echo '<p style="margin-top: 150; text-align:center; font-size: 18px;">Your message has been sent! Click here to return to the website.</p>';
} else {
echo '<p>Problem sending mail!';
}
And honestly, the native PHP mail() function sucks! Consider using SwiftMailer or PHPMailer instead.
Use isset on your condition:
if (isset($_POST['submit']) && $captcha == 4)
It checks if the field is non empty and the form has been submitted.
And && is better here than and see the reason here: https://stackoverflow.com/a/11861068

Changing the value of a Form Submit Button to say submitted after form submits

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>';
}
}
?>

How do I return a message after submitting a form on my page with PHP?

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.

How to make form work

I started a form in a PHP page and I think I have everything right were it belongs. But when I upload it to a server it doesn’t work. How to fix it?
Here is my code:
<section class="body">
<?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'] && $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>';
}
?>
<form method="post" action="new.php">
<label>Name</label>
<input name="name" placeholder="Type Here">
<label>Email</label>
<input name="email" type="email" placeholder="Type Here">
<label>Message</label>
<textarea name="message" placeholder="Type Here"></textarea>
<label>*What is 2+2? (Anti-spam)</label>
<input name="human" placeholder="Type Here">
<input id="submit" name="submit" type="submit" value="Submit">
</form>
</section>
so the error that i get is this whole line of code on my live site
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>';
}
?>
It appears that your web server does not support PHP. PHP runs on the web server (aka server side), and the client side (that is the web browser) should only get valid HTML.
It looks like your webserver is not interpreting the PHP. A quick check for this would be to view the source in your browser - if you see PHP code there, then the webserver did not run the PHP.
There are many possible reasons for PHP not being interpreted - the php extension not being handled, PHP not being installed, other web server configurations, etc.

php mail() script not working

Hey guys I cannot for the life of me figure out where I went wrong here. When the form submits it takes it to a blank page. No errors or confirmed. Just Blank. Im assuming it is a syntax error but I just cant see it for some reason. Do you guys see anything?
Here is my form code:
<form method="POST" action="mailtest.php">
<label>Name<span class="req">*</span></label>
<input name="name" placeholder="Type Here">
<label>Email<span class="req">*</span></label>
<input name="email" type="email" placeholder="Type Here">
<label>Subject</label>
<input name="subjectf" placeholder="Type Here">
<label>Message<span class="req">*</span></label>
<textarea name="message" placeholder="Type Here"></textarea>
<input class="submit" name="submit" type="submit" value="">
<span class="right" style="color:red">* represents a mandatory field.</span>
</form>
And here is the php script I have on the mailtest.php page:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = $_POST['name'];
$to = 'rkoolman#bellsouth.net';
$subject= 'new enquiry on website';
$subjectf= $_POST['subjectf'];
$body = "From: $name\n E-Mail: $email\n Subject: $subjectf\n Message:\n $message";
if ($_POST['submit']) {
if ($name != '' && $email != '' && $message != '') {
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 need to fill in all required fields!!</p>';
}
}
?>
You should always first check whether the form was submitted so
remove if ($_POST['submit']) and put it on the top of the script..
also you should check whther the $_POST["submit"] was set.. the script should look like this:
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = $_POST['name'];
$to = 'test#test.com';
$subject= 'new enquiry on website';
$subjectf= $_POST['subjectf'];
$body = "From: $name\n E-Mail: $email\n Subject: $subjectf\n Message:\n $message";
if ($name != '' && $email != '' && $message != '') {
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 need to fill in all required fields!!</p>';
}
}
Set your submit input value to submit
<input class="submit" type="submit" name="submit" value="submit">

Categories