Jump to content

PHP Contact Form Not Working


ndgz102489
 Share

Recommended Posts

Greetings,

I have a contact form on my website, but it is not working. When I fill out the form and press "Submit", I don't receive any inquiries to my e-mail. Now, I am not familiar of PHP, but I have found a simple contact form template in the interwebs. I had followed the instructions on how to make to use it, but it doesn't seem to work. I am unsure if I am missing a code, but it is giving me a headache.

 

I've attached the screenshots.

Screen Shot 2021-06-22 at 6.50.48 PM.png

Screen Shot 2021-06-22 at 6.50.59 PM.png

Link to comment
Share on other sites

There's all kinds potential reasons for your code not to work. Please repost your code using the code tags so we can copy paste it, for a closer look.

In the meantime, add this snippet to line two of your contact.php file:

var_dump($_POST);

That will tell you what you're actually getting from your form. So, let us also know what you're seeing.

FYI, your problem may not be with you code.  Your server may not be set up to send emails.

Use this tutorial to check that out and let us know what happened:

https://www.w3schools.com/php/func_mail_mail.asp 

Edited by niche
Link to comment
Share on other sites

You are not checking name, email etc, for a specific value of true or false, and then return false only if any of the field are empty.

example:

 <?php
        $_POST['button'] = "Submit";
        $_POST['name'] = "fff";
        $_POST['email'] = "AA";
        $_POST['phone'] = "123";
        $_POST['message'] = "WTH";


        if (isset($_POST['button'])) {
            if (empty($_POST['name']) || empty($_POST['email']) || empty($_POST['phone']) || empty($_POST['message'])) {
                echo "You gotta fill in required fields of name, email, phone and message";
                return FALSE;
            } else { /* optional, just for testing */
                echo "all fields filled";
            }
        }
        ?>

 

Edited by dsonesuk
Link to comment
Share on other sites

 

23 hours ago, niche said:

There's all kinds potential reasons for your code not to work. Please repost your code using the code tags so we can copy paste it, for a closer look.

In the meantime, add this snippet to line two of your contact.php file:

var_dump($_POST);

That will tell you what you're actually getting from your form. So, let us also know what you're seeing.

FYI, your problem may not be with you code.  Your server may not be set up to send emails.

Use this tutorial to check that out and let us know what happened:

https://www.w3schools.com/php/func_mail_mail.asp 

Here is the PHP code:

<?php
var_dump($_POST);
// Check for empty fields
if (isset($_POST['button'])) {
   empty($_POST['name'])
   empty($_POST['email'])
   empty($_POST['phone'])
   empty($_POST['message'])
     echo "You gotta fill it out!";
     return false;
   }

$name = strip_tags(htmlspecialchars($_POST['name']));
$email = strip_tags(htmlspecialchars($_POST['email']));
$phone = strip_tags(htmlspecialchars($_POST['phone']));
$message = strip_tags(htmlspecialchars($_POST['message']));

// Create the email and send the message
$to = 'ned@nerdcorestudio.com'; // Add your email address inbetween the '' replacing yourname@yourdomain.com - This is where the form will send a message to.
$subject = "Website Contact Form:  $name";
$text = "You have received a new message from your website contact form.\n\n"."Here are the details:\n\nName: $name\n\nEmail: $email\n\nPhone: $phone\n\nMessage:\n$message";
$header = "From: noreply@yourdomain.com\n"; // This is the email address the generated message will be from. We recommend using something like noreply@yourdomain.com.
mail($to,$subject,$text,$header);
header("Location: index.html?mailsent")
return true;
if(mail($to, $subject, $text, $header)) {
    echo 'Thank You!';
    } else {
    echo 'boo, no email was sent';
    }

?>

Link to comment
Share on other sites

11 minutes ago, ndgz102489 said:

 

Here is the PHP code:

<?php
var_dump($_POST);
// Check for empty fields
if (isset($_POST['button'])) {
   empty($_POST['name'])
   empty($_POST['email'])
   empty($_POST['phone'])
   empty($_POST['message'])
     echo "You gotta fill it out!";
     return false;
   }

$name = strip_tags(htmlspecialchars($_POST['name']));
$email = strip_tags(htmlspecialchars($_POST['email']));
$phone = strip_tags(htmlspecialchars($_POST['phone']));
$message = strip_tags(htmlspecialchars($_POST['message']));

// Create the email and send the message
$to = 'ned@nerdcorestudio.com'; // Add your email address inbetween the '' replacing yourname@yourdomain.com - This is where the form will send a message to.
$subject = "Website Contact Form:  $name";
$text = "You have received a new message from your website contact form.\n\n"."Here are the details:\n\nName: $name\n\nEmail: $email\n\nPhone: $phone\n\nMessage:\n$message";
$header = "From: noreply@yourdomain.com\n"; // This is the email address the generated message will be from. We recommend using something like noreply@yourdomain.com.
mail($to,$subject,$text,$header);
header("Location: index.html?mailsent")
return true;
if(mail($to, $subject, $text, $header)) {
    echo 'Thank You!';
    } else {
    echo 'boo, no email was sent';
    }

?>

This is the error that I've received from the error log.

Screen Shot 2021-06-23 at 7.37.11 PM.png

Link to comment
Share on other sites

Yes! Because this

empty($_POST['name'])
   empty($_POST['email'])
   empty($_POST['phone'])
   empty($_POST['message'])

IS WRONG! Its structure does not make sense, they return true or false, ok! Now what? They require a if condition to check each is true ie 'empty' if so, return error message and return false to stop going any further. That is what my example showed, if you set any of these to = "";

$_POST['name'] = "fff";

$_POST['email'] = "AA";

$_POST['phone'] = "123";

$_POST['message'] = "WTH";

Empty Error message would show otherwise 'all fields filled' will show.

Link to comment
Share on other sites

Gotcha. Thank you.

So, I got the form to work, but I receive an e-mail and the results copies what was written in between the quotation marks in the PHP file. How do I get the form to write the appropriate information such as the recipient's name, e-mail, phone number, and message?

Screen Shot 2021-06-24 at 12.58.42 PM.png

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...