php mail() script not working - php

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">

Related

php form works on one site but not another

I've been using the same contact form for quite a while:
<?php
if (isset($_POST['submit']) ) {
$name = $_POST['names'];
$email = $_POST['email'];
$message = $_POST['message'];
$robot = $_POST['robot'];
$from = 'online#domain.co.uk';
$to = 'info#domain.co.uk';
$subject = 'Online Enquiry';
$headers = "From: DOMAIN <no-reply#domain.co.uk> \r\n";
$headers .= 'Reply-To:'. $email . "\r\n";
$headers .= "X-Mailer: PHP/" . phpversion(); // Sender's Email
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
} ?>
<?php if (isset($_POST['submit'])) {
if ($name != '' && $email != '' && $message !='') {
if ($robot != 'yes') {
if (mail ($to, $subject, $body, $headers)) {
echo '<p class="approved"><strong>Your message has been submitted</strong></p>';
} else {
echo '<p class="warning"><strong>Something went wrong!</strong></p>';
}
} else if ($_POST['submit'] && $robot == 'yes') {
echo '<p class="warning"><strong>Looks like you are spam!</strong></p>';
}
} else {
echo '<p class="warning"><strong>Please complete all fields.</strong></p>';
}
}
?>
<form id="contact" method="post" action="#contact">
<label>Name</label>
<input name="names" type="text" placeholder="NAME">
<label>Email</label>
<input name="email" type="email" placeholder="EMAIL">
<label>Message</label>
<textarea rows="10" name="message" placeholder="MESSAGE"></textarea>
<input id="capture" name="robot" type="checkbox" value="yes">
<input id="submit" name="submit" type="submit" value="Submit">
</form>
It works absolutely fine on other sites but doesn't work on one domain? submit simply returns the error "Something went wrong!". the site is on the same cloud server as many of the other sites that work fine. It's driving me mad because I cant see an error.
Turning debugging on I get the error:
Notice: Undefined index: robot in /form-contact.php on line 6
Ive tried removing the robot check all together and it still doesn't work, but generates no error when debugging?
Can anyone offer any advice?
When your checkbox is not checked - it will not be sent.
Replace
$robot = $_POST['robot'];
to
$robot = isset($_POST['robot']) ? 'yes' : 'no';
Your Condition Matches the case. Try checking the value of $robot on submit.
<input id="capture" name="robot" type="checkbox" value="yes">
if ($robot != 'yes') {
if (mail ($to, $subject, $body, $headers)) {
echo '<p class="approved"><strong>Your message has been submitted</strong></p>';
} else {
echo '<p class="warning"><strong>Something went wrong!</strong></p>';
}

PHP mail reply to sender instead of server email

Can anyone point out where am I getting this form wrong? Instead of being able to reply to the "email" from the sender I get the server email.
Here is the php code:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: business.com';
$to = 'anyone#gmail.com';
$subject = 'A new Message from your website business.com';
$human = $_POST['human'];
$headers = 'From: info#business.com' . "\r\n" .
'Reply-To: ' . $email . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$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 required fields!!</p>';
}
}
?>
and here is the html
<form method="post" action="contact.php" target="contactIframe" name="contact">
<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></br>
<label>*What is 2+2? (Anti-spam)</label>
<input name="human" placeholder="Type Here"></br></br>
<input id="submit" name="submit" type="submit" value="Submit">
<input type="button" name="reset_form" value="Clear" onclick="this.form.reset();">
</form>
Thanks everyone
You mail function is does not have $headers as additional headers. The code should be...
if ($human == '4') {
if (mail ($to, $subject, $body, $headers)) {
echo '<p>Your message has been sent!</p>';
}
}
ref : PHP Mail Function

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.

Contact form is not working?

I'm trying to create a simple contact page however when I submit the form, it leads me to the blank page contact-form.php. I used this article as reference: http://tangledindesign.com/how-to-create-a-contact-form-using-html5-css3-and-php/
What am I doing wrong?
Code on index.php (corner divs are purely for styling purposes)
<form action="contact-form.php" method="post" autocomplete="off" id="contact">
<div class="corner"></div>
<input type="text" required name="name" placeholder="Name" value="">
<div class="corner"></div>
<input type="text" required name="email" placeholder="Email" value="">
<div class="corner"></div>
<input type="text" required name="check" placeholder="Question of the day: What's 2 + 2 ?" value="">
<div class="corner"></div>
<textarea name="message" rows="25" cols="50" placeholder="Drop me a line!"></textarea>
<button class="send" type="submit" name="submit">Send</button>
</form>
Code on contact-form.php
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: MessageAnita';
$to = 'myemail#gmail.com';
$subject = 'Hello';
$check = $_POST['check'];
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if ($_POST['submit'] && $check == '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'] && $check != '4') {
echo '<p>You answered the anti-spam question incorrectly!</p>';
}
?>
change the button tag to an input tag
< input type ="submit" name="submit"/>
In your form, change:
<button class="send" type="submit" name="submit">Send</button>
to:
<input type="submit" name="submit" value="Submit">
Plus, you may want to use this PHP mailing method, since the mail came into my SPAM when testing.
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = $_POST['email'];
$to = 'myemail#gmail.com';
$subject = 'Hello';
$check = $_POST['check'];
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
$headers = "From: $from" . "\r\n" .
"Reply-To: $from" . "\r\n";
if ($_POST['submit'] && $check == '4') {
if (mail ($to, $subject, $body, $headers)) {
echo '<p>Your message has been sent!</p>';
} else {
echo '<p>Something went wrong, go back and try again!</p>';
}
} else if ($_POST['submit'] && $check != '4') {
echo '<p>You answered the anti-spam question incorrectly!</p>';
}
?>
And if you want to send as HTML use:
$headers = "MIME-Version: 1.0\n";
$headers .= "Content-Type: text/html; charset=iso-8859-1\n";
$headers .= "From: $from" . "\r\n" .
"Reply-To: $from" . "\r\n";

Categories