0

I have a contact form with the following ajax and php code:

<script>
function _(id){ return document.getElementById(id); }
function submitForm(){
    _("mybtn").disabled = true;
    _("status").innerHTML = 'please wait ...';
    var formdata = new FormData();
    formdata.append( "n", _("n").value );
    formdata.append( "e", _("e").value );
    formdata.append( "t", _("t").value );
    formdata.append( "m", _("m").value );
    var ajax = new XMLHttpRequest();
    ajax.open( "POST", "mail.php" );
    ajax.onreadystatechange = function() {
        if(ajax.readyState == 4 && ajax.status == 200) {
            if(ajax.responseText == "success"){
                _("my_form").innerHTML = '<h2>Thanks '+_("n").value+', your message has been sent.</h2>';
            } else {
                _("status").innerHTML = ajax.responseText;
                _("mybtn").disabled = false;
            }
        }
    }
    ajax.send( formdata );
}
</script>

PHP code of mail.php:

<?php
if( isset($_POST['n']) && isset($_POST['e']) && isset($_POST['t']) && isset($_POST['m']) ){
    $n = $_POST['n']; // HINT: use preg_replace() to filter the data
    $e = $_POST['e'];
    $t = $_POST['t'];
    $m = nl2br($_POST['m']);
    $to = "myemail@mydomain.com";
    $from = $e;
    $subject = 'Contact Form Message';
    $message = '<b>Name:</b> '.$n.' <br><b>Email:</b> '.$e.' <br><b>Telephone:</b> '.$t.' <p>'.$m.'</p>';
    $headers = "From: $from\n";
    $headers .= "MIME-Version: 1.0\n";
    $headers .= "Content-type: text/html; charset=iso-8859-1\n";
    if( mail($to, $subject, $message, $headers) ){
        echo "success";
    } else {
        echo "The server failed to send the message. Please try again later.";
    }
}
?>

It was working fine but I just now checked and when I click on Submit button, the PHP code is printed out. It does not send the email to my mailbox containing all the information. is there anything wrong with this code? Or can I be doing this is a simpler way? I do not have much background in PHP or Ajax.

  • Sounds like the webserver is wrongly configured/missing a PHP package/… – Ulrich Thomas Gabor Dec 14 '18 at 08:02
  • I am running Ubunutu 18.04 with PHP 7.2. Are there are any more modules/packages required? – Ankit Badani Dec 14 '18 at 08:09
  • Check logs they will let you know whats the issue ;) – Ingus Dec 14 '18 at 08:10
  • Sounds as if the Php code is not being interpreted. 'It was working fine', when? Have you migrated code? Have you updated your server? I'm sure something has changed. – Progrock Dec 14 '18 at 08:11
  • Actually, I installed a Lets Encrypt SSL Certificate. It was working fine few weeks back. But I installed the certificate right now and was testing the site and found this issue. Would those be related? – Ankit Badani Dec 14 '18 at 08:14
  • No errors found on logs, I enabled PHP logging and tried doing it multiple time. No logs were created. – Ankit Badani Dec 14 '18 at 08:27
  • @AnkitBadani what do you see at screed? plain php code? – Ingus Dec 14 '18 at 08:33
  • This is the PHP output after clicking on Submit Name: '.$n.' Email: '.$e.' Telephone: '.$t.' '.$m.' '; $headers = "From: $from\n"; $headers .= "MIME-Version: 1.0\n"; $headers .= "Content-type: text/html; charset=iso-8859-1\n"; if( mail($to, $subject, $message, $headers) ){ echo "success"; } else { echo "The server failed to send the message. Please try again later."; } } ?> – Ankit Badani Dec 14 '18 at 08:38
  • Check this could help: https://stackoverflow.com/questions/33587684/jquery-ajax-call-returns-entire-php-file – Ingus Dec 14 '18 at 08:49
  • Another idea is do your code start with ` – Ingus Dec 14 '18 at 09:00
  • 1
    I solved it by issuing a2enmod php7.2 and then systemctl restart apache2 – Ankit Badani Dec 14 '18 at 09:24
  • @AnkitBadani Cool :) I m glad you solved it! – Ingus Dec 14 '18 at 09:40

0 Answers0