0

I'm currently working on my portfolio. As of right now I'm trying to get the contact form to work. So I don't really know php that well and I'm not that good at it, but I've been googling and trying different ideas for myself. As of right now none of them have worked. Below I will attach the contact form html portion as well as the php I have now. Any help would be appreciated greatly

<section id="contact-form">
        <!--Contact form section-->
            <form method="POST" name="emailForm" action="index.php">
            <!--Beginning of form and mail to my email address-->
                <div class="contact-left">
                <!--Contact info on the left side-->
                    <h1 class="c-l-heading">Contact</h1>
                    <!--Major heading-->
                    <div class="f-name">
                    <!--Name portion-->
                        <font>Name</font>
                        <input type="text" name="name" placeholder="Full Name" required/>
                        <!--Input field with a placeholder-->
                    </div>

                    <div class="f-email">
                    <!--email portion-->
                        <font >Email</font>
                        <input type="email" name="name" placeholder="Example@gmail.com" required/>
                        <!--Input field with a placeholder-->
                    </div>
                </div>
        
                <div class="contact-right">
                <!--Contact info on the right side-->
                    <div class="message">
                    <!--Message portion-->
                        <font >Message</font>
                        <textarea name="message" rows="5" cols="20" placeholder="Write Message..." required></textarea>
                        <!--Textbox created-->
                    </div>
                    <button>submit</button>
                    <!--Submit button-->
                </div>
            </form>
        </section>

PHP section now:

<?php

if(!isset($_POST['submit']))
{
    echo "Error! You need to submit the form!";
}
//Collection
$name = $_POST['name'];
$visitor_email = $_POST['email'];
$message = $_POST['message'];

//Validation
if(empty($name) || empty($visitor_email))
{
    echo "Name and email are required"
    exit;
}

$email_from = 'desmondlambkin17@gmail.com';
$email_subject = "dnjls.ca Contact Submission";
$email_body = "Name: $name\n"
            "Email address: $visitor_email\n"
            "Message: $message";

$to = "desmondlambkin17@gmail.com";
$headers = "From: $email_from";
//Send Email
mail($to, $email_subject, $email_body, $headers);
?>

Currently when I try to submit an email it says This page isn't working, and is currently unable to handle the request

Kevin M. Mansour
  • 2,915
  • 6
  • 18
  • 35
  • 1
    *"it says This page isn't working"* - Sounds like an error is happening server-side. Can you check your web server logs and PHP logs? – David Jan 14 '21 at 16:37
  • 1
    Make sure error logging / reporting is enabled. See https://stackify.com/php-error-logs-guide/ for guidance. Once you understand the underlying error, you'll have a chance of knowing how to fix it. – ADyson Jan 14 '21 at 16:38
  • 2
    BTW `if(!isset($_POST['submit'])) { echo "Error! You need to submit the form!"; }` ...you're missing an `exit();` in there. otherwise it'll carry on regardless and still try to send the email. – ADyson Jan 14 '21 at 16:39
  • The single most important aspect you can take away from this is to [**READ THE PHP ERROR LOGS**](https://stackoverflow.com/questions/5127838/where-does-php-store-the-error-log-php5-apache-fastcgi-cpanel) They will be vital for you to discovering exactly what's wrong and researching solutions. – Martin Jan 14 '21 at 16:40
  • As Adyson said `` –  Jan 14 '21 at 16:41
  • So after a bit of playing around and going over code. I found the error logs and the solved that issue so I wanna thank everyone for the help on there. But I finally got the email and all the response was 0? What's going on there? And how can I do it so after the contact form is submitted, it stays on my webpage and not go to a blank white page? – Desmond Lambkin Jan 14 '21 at 19:27

0 Answers0