0

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 = "kk@prisa.com";
    $mail->Password = "1234";
    $mail->SMTPSecure = "false"; //TLS
    $mail->Port = 587; //587

    $mail->addAddress('kk@prisa.com');
    $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>
Jmainol
  • 167
  • 9
  • Just remove that echo statement if you don't want it included in the email message? – Curious13 Jun 13 '18 at 22:06
  • " if I include a single line of php code in it, it must be .php in order to work " no file extensions mean nothing, if you want to run `*.zzzzzzzzzzzzz` through the php parser you can. if you want php in *.html file parse it as one –  Jun 13 '18 at 22:07
  • Can you explain a little more about why you want to make this into just a .html file? – NorthStarCode Jun 13 '18 at 22:36
  • @Curious13 if I remove the echo statement I will have no confirmation message. – Jmainol Jun 15 '18 at 12:20
  • @DonCarlosII, I want to do it because this contact form is part of a index.html with many sections and I would like this file to keep being .html – Jmainol Jun 15 '18 at 12:22
  • There's nothing wrong with having HTML in a .php file. You could have an entire .php file be pure HTML if you felt like it. There shouldn't be an issue in having that echo statement in there. The only way around this is to use JavaScript hacks to get a URL response parameter, and that defeats the purpose of using PHP. I guess I still don't understand why you don't just want to use the .php extension? – NorthStarCode Jun 15 '18 at 13:48
  • 1
    @DonCarlosII At last I got my target following this tutorial: https://www.youtube.com/watch?v=zXwA4N-kR2g&t=850s – Jmainol Jun 16 '18 at 20:54

1 Answers1

0

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.

NorthStarCode
  • 356
  • 1
  • 12