0

I am busy with an email form using PHP and raw HTML and CSS. I am trying to get a confirmation that the email was sent back to the user.

HTML Code:

<div>
        <form class="input-form2" action="mail.php" method="post">
            <h3 class="form-heading2 text-bebas">Send me an email</h3>
            <div class="input-field-row2">
                <p class="form-label2">Name:</p>
                <input type="text" name="name" placeholder="Full Name..." required>
            </div>
            <div class="input-field-row2">
                <p class="form-label2">Email:</p>
                <input class="text-mont" type="text" name="email" placeholder="Email..." required>
            </div>
            <div class="input-field-row2">
                <p class="form-label2">Subject:</p>
                <input class="text-mont" type="text" name="subject" placeholder="Subject..." required>
            </div>
            <div class="flex-row2">
                <p class="form-label2" style="height: 40px;">Message:</p>
                <textarea class="textarea-class text-mont" name="message" placeholder="Message..." required></textarea>
            </div>

            <div style="width: 100%; justify-content: space-between; margin-top: 40px;" class="flex-row2">
                <div class="confirm-box">
                    <input class="largerCheckbox" type="checkbox" id="confirm" name="confirm" required>
                    <p>I'm not a robot</p>
                </div>
                <div class="email-sent">Email Sent</div>
                <button class="form-button text-mont-bold" type="submit" name="submit">Send</button>
            </div>
        </form>
    </div>
    <?php include 'mail.php';?>

PHP Code (file is mail.php):

<?php

if(isset($_POST['submit'])) {
    $name = $_POST['name'];
    $subject = $_POST['subject'];
    $mailFrom = $_POST['email'];
    $message = $_POST['message'];
    $confirm = $_POST['confirm'];

    if($confirm == false) {
        header('Location: https://sleekwebdesign.co.za?sendingFailed');
    }

    $array = explode( "@" , $mailFrom);

    $mailTo = "piet@gmail.com";
    $headers = 'From: ' . $mailFrom . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

    echo "headers: ";
    echo $headers;
    echo "<br>";

    if( mail($mailTo, $subject, $message, $headers) ) {
        echo "mail sent";
        // header('Location: https://sleekwebdesign.co.za?mailsent');
        // alert("Email sent");
    } else {
        echo "mail not sent";
    }
}

If the email was sent correctly I want some confirmation, either by displaying the email-sent div. So my question is how do I bind that HTML element to a PHP variable so when the variable is true I show the element.

You may also provide a different method. I have tried using

alert("Email sent");
header('Location: https://sleekwebdesign.co.za?mailsent');

but the problem here is that it is redirected before doing the alert. So is there an easier way to apply this?

Franco
  • 441
  • 3
  • 18
  • 1
    You can not create output for the browser to show, and redirect elsewhere at the same time. – CBroe Feb 22 '22 at 11:55
  • _"but the problem here is that it is redirected before doing the alert."_ - that alert should come in the _target_ page, that you are redirecting _to_. You already added `mailsent` in the query string - so in that page, _check_ for that - and either output the JS to trigger the alert, or don't. – CBroe Feb 22 '22 at 11:57
  • 1
    You cannot "bind" HTML to PHP; PHP renders some HTML, and sends it to the browser, and _that's it_. To update what's on the screen, you either need to render a new page, or write some JavaScript that makes a background request to PHP and then updates things on screen (known as "AJAX"). – IMSoP Feb 22 '22 at 12:01

0 Answers0