phpmailer contact form with echo submit confirmation message without php code into html form

Jmainol Source

I got to make this contact form works using phpmailer, but I need to separate php and html code completely. I need this because I intent the contact form to be the last section of a index.html web page, so I need this contact form to be .html and I am aware that if I include a single line of php code in it, it must be .php in order to work

The core of the question es how to remove this line of php

 <?php if ($msg != "") echo "$msg<br><br>"; ?>

and keep being the success and error message????

Here the php code:

<?php
$msg = "";
use PHPMailer\PHPMailer\PHPMailer;
include_once "PHPMailer/PHPMailer.php";
include_once "PHPMailer/Exception.php";
include_once "PHPMailer/SMTP.php";

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

    if (isset($_FILES['attachment']['name']) && $_FILES['attachment']['name'] != "") {
        $file = "attachment/" . basename($_FILES['attachment']['name']);
        move_uploaded_file($_FILES['attachment']['tmp_name'], $file);
    } else
        $file = "";

    $mail = new PHPMailer();

    //if we want to send via SMTP
    $mail->Host = "localhost";
    $mail->isSMTP();
    $mail->SMTPAuth = true;
    $mail->Username = "[email protected]";
    $mail->Password = "1234";
    $mail->SMTPSecure = "false"; //TLS
    $mail->Port = 587; //587

    $mail->addAddress('[email protected]');
    $mail->setFrom($email);
    $mail->Subject = $subject;
    $mail->isHTML(true);
    $mail->Body = $message;
    $mail->addAttachment($file);

    if ($mail->send())
        $msg = "Your email has been sent, thank you!";
    else
        $msg = "Please try again!";

    unlink($file);
}

?>

Here the html code with the damn php line:

<?php if ($msg != "") echo "$msg<br><br>"; ?>

            <form method="post" action="index.php">
                <input class="form-control" name="subject" placeholder="Subject..."><br>
                <input class="form-control" name="email" type="email" placeholder="Email..."><br>
                <textarea placeholder="Message..." class="form-control" name="message"></textarea><br>
                <input class="form-control" type="file" name="attachment"><br>
                <input class="btn btn-primary" name="submit" type="submit" value="Send Email">
            </form>
phphtmlphpmailer

Answers

answered 1 week ago DonCarlosII #1

To do this you'd need to access some other file via JavaScript. You can use JavaScript style AJAX, or you can use jQuery (which is often a lot easier, but it does add a dependency to your project.) Otherwise @smith is correct in that you could make your Apache server process files with whatever file extension you'd like.

But, if you want to avoid that then create an onload event that fires an AJAX call to an actual PHP file, which will then return the value that you need and will also keep your .php files separate from your .html files. The response value can then be shown in an element near your email form.

comments powered by Disqus