0

I'm using the PHPMailer on my Website and am trying to implement a script inside the .php file that changes the styling of the form when the message was send successfully and another script, that changes the styling when the message could not be send, but i can't get it to work.

HTML

<form id="contact_Form" action="php_Scripts/contact.php" method="post">
    <label for="email"></label>
    <input type="text" id="email" name="email" placeholder="E-Mail">
    <label for="name"></label>
    <input type="text" id="name" name="name" placeholder="Name">
    <label for="textarea"></label>
    <textarea id="textarea" name="message" placeholder="..."></textarea>
    <button id="button" name="send" type="submit">Send</button>
</form>

CSS

.messageSend{
    animation: messageSend 1.5s linear;
}
@keyframes messageSend{
    0%{
          border-color: initial;
    }
    50%{
          border-color: rgba(0,255,0,1);
    }
    100%{
          border-color: initial;
    }
}

.messageNotSend{
    animation: messageNotSend 1.5s linear;
}
@keyframes messageNotSend {
    0%{
          border-color: initial;
    }
    50%{
          border-color: rgba(255,0,0,1);
    }
    100%{
          border-color: initial;
    }
}

PHP

<?php
    // Import PHPMailer classes into the global namespace
    // These must be at the top of your script, not inside a function
    use PHPMailer\PHPMailer\PHPMailer;
    use PHPMailer\PHPMailer\Exception;

    if(isset($_POST['send'])) {

        $email = $_POST['email'];
        $name = $_POST['name'];
        $message = $_POST['message'];

        require '../PHPMailer/src/Exception.php';
        require '../PHPMailer/src/PHPMailer.php';
        require '../PHPMailer/src/SMTP.php';

        // Load Composer's autoloader
        require '../PHPMailer/vendor/autoload.php';

        // Instantiation and passing `true` enables exceptions
        $mail = new PHPMailer(true);

        try {
            // Server settings
            $mail->SMTPDebug = 0;                                                // Enable verbose debug output
            $mail->isSMTP();                                                     // Send using SMTP
            $mail->Host       = 'smtp.gmail.com';                                // Set the SMTP server to send through
            $mail->SMTPAuth   = true;                                            // Enable SMTP authentication
            $mail->Username   = '*************@gmail.com';                       // SMTP username
            $mail->Password   = '**************';                                 // SMTP password
            $mail->SMTPSecure = 'tls';                                           // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` also accepted
            $mail->Port       = 587;                                             // TCP port to connect to

            // Recipients
            $mail->setFrom($email);
            $mail->addAddress('*************@gmail.com');                        // Add a recipient
            $nameContent = htmlentities($name);
            $messageContent = htmlentities($message);
            $body = '<strong>' . $nameContent . '</strong> <br> <br>' . nl2br($messageContent);

            // Content
            $mail->isHTML(true);                                                 // Set email format to HTML
            $mail->Subject = $email;
            $mail->Body    = $body;

            $mail->send();
            echo
            '
                <script src="../scripts/jquery.js"></script>
                <script type="text/javascript">
                    window.parent.document.getElementById("iFrame_Content").src = "contact.html";
                    $("#email, #name, #textarea").addClass("messageSend");
                    alert("test");
                </script>
            '
            ;
        } catch (Exception $e) {
            echo
            '
                <script src="../scripts/jquery.js"></script>
                <script type="text/javascript">
                    window.parent.document.getElementById("iFrame_Content").src = "contact.html";
                    $("#email, #name, #textarea").addClass("messageNotSend");
                    alert("test");
                </script>
            '
            ;
        }
    }
?>

Only the alert works, but it's not changing the styling. There are no error messages in the console either.

Mark H.
  • 1
  • 2

0 Answers0