Search code examples
phphtmlformscontact-form

Form submission returns blank page


I have a contact form that has worked well so far. However, since I created a php router on the site, after submitting the form, although it redirects to the action site (thank-you.php), the site's content is only a blank page (with no HTML content in he body, I checked with dev tools), and the message does not reach the appropriate email address. If I var_dump the $_POST variable on the form submission page, no value is displayed.

I suspect that the problem is related to the router. As a test, I put a "print" PHP command with a number in ascending order before each route. I put the thank-you page (form submission page) directly after the index page to rule out whether the code got stuck on other routes. The strange thing is that, although each sub-page only prints out the numbers up to the place where the given page is among the routers, the thank-you page prints out all the numbers, i.e. those also that would only come after it in the queue... I would be really grateful for some help with this issue. Thank you in advance.

HTML (the form):

                <form class="contact-form" method="POST" action="/thank-you">

                    <input type="text" id="website" name="website" />

                    <div class="contact-wrapper name">
                        <label for="name">
                            <?php echo $lang["contact-name"] ?>
                        </label>
                        <input type="text" id="name" name="name" placeholder="<?php echo $lang['name-placeholder'] ?>" required>
                    </div>
                    <div class="contact-wrapper email-and-phone">
                        <div>
                            <label for="email">E-mail</label>
                            <input type="text" id="email" name="email" placeholder="<?php echo $lang['email-placeholder'] ?>" required>
                        </div>

                        <div>
                            <label for="telephone">
                                <?php echo $lang["contact-telephone"] ?>
                            </label>
                            <input type="text" id="telephone" name="telephone" placeholder="<?php echo $lang['telephone-placeholder'] ?>">
                        </div>
                    </div>
                    <div class="contact-wrapper subject">
                        <label for="subject">
                            <?php echo $lang["contact-subject"] ?>
                        </label>
                        <input type="text" id="subject" name="subject" placeholder="<?php echo $lang['subject-placeholder'] ?>" required>
                    </div>

                    <div class="contact-wrapper message">
                        <label for="message">
                            <?php echo $lang["contact-message"] ?>
                        </label>
                        <textarea id="message" name="message" placeholder="<?php echo $lang['message-placeholder'] ?>" style="height:200px" required></textarea>
                    </div>
                    <input type="submit" id="submit" name="submit" value="<?php echo $lang['contact-submit'] ?>">
                </form>

PHP (the form submission page):

<?php

// exit if accessed directly.
defined('ABSPATH') || die(header('location: /home'));

if (isset($_POST['submit']) && $_POST['submit'] != '' && $_POST['submit'] != null) {

    $name = $_POST['name'];
    $mailFrom = $_POST['email'];
    $telephone = $_POST['telephone'];
    $subject = $_POST['subject'];
    $message = $_POST['message'];

    $mailTo = "[email protected]";
    $txt = "You have received an e-mail from " . $name . ".\n\n" . "E-mail: " . $mailFrom . ".\n\n" . "Telephone: " . $telephone . ".\n\n" . $message;

    mail($mailTo, $subject, $txt);
}
?>

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset='UTF-8' />
    <title>Thank you!</title>
    <link rel="stylesheet" type="text/css" href="css/style.css">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>

<body>
    <div class="thank-you-wrapper">
        <p>Thank you for reaching out to us. We will get back to you shortly. Please click <a href="/home" style="text-decoration: none; color: #0986d9;">here </a> if you are not redirected within a few seconds.
</p>
    </div>
    <script>
        setTimeout(function() {
            window.location.replace("/home"); // will redirect to main page
        }, 3000); // will call the function after 3 secs.
    </script>
</body>

</html>

PHP (the router):

<?php

require_once __DIR__ . '/router.php';

// ##################################################
// ##################################################
// ##################################################

// Static GET
// In the URL -> http://localhost

print '1';
// The output -> Index
get('/', 'pages/index.php');

print '2';
// The output -> Thank You
get('/thank-you', 'pages/thank-you.php');

print '3';
// The output -> About
get('/about', 'pages/about.php');

print '4';
// The output -> Services
get('/services', 'pages/services.php');

print '5';
// The output -> Web
get('/web', 'pages/web.php');

print '6';
// The output -> Contact
get('/contact', 'pages/contact.php');
``````````````````````````````````````````````````````

Solution

  • The router defines /thank-you as GET:

    get('/thank-you', 'pages/thank-you.php');
    

    while your form is sent via POST:

    <form class="contact-form" method="POST" action="/thank-you">