Topic: AJAX Contact form showing PHP page after successful email sent

Yves Chaput free asked 5 years ago


Expected behavior Stays on HTML page with updated message

Actual behavior Shows PHP page with successful message

Resources (screenshots, code snippets etc.) {"message":"Courriel envoy\u00e9; avec succ\u00e8s!","code":1}

To see it in action: yveschaput.com, scroll down to the Contact section. After filling up the for with proper info, clicking on "Envoyé" opens up the PHP page (url: yveschaput.com/mail.php)

Besides the cosmetic changes and language translation, the code is exactly as your example here: Bootstrap contact form. This line, in the PHP script, print json_encode(array('message' => 'Courriel envoyé avec succès!', 'code' => 1));, doesn't return to the HTML.

I don't know PHP enough to figure it out myself, I need your help.

Thanks.


Yves Chaput free answered 5 years ago


Thanks Adam for looking into it. But a friend of mine steered me in the right direction and I finally managed to get it working. It was late last night and I was quite tired so I went off to bed not thinking of updating you on it. Sorry for this. Though, you might want to look into your email programming example and make sure it is working as posted. If I go by my experience, it doesn't seem to work as it is posted. But it could be me also. Maybe I did something wrong while copying and pasting. Anyways, thanks for wanting to help me out. I still love MDB nonetheless. :)


Yves Chaput free answered 5 years ago


Just realized, you can't see the PHP script from the website so, here it is:

  <?php
         $name = $_POST['name'];
         $email = $_POST['email'];
         $message = $_POST['message'];
         $subject = $_POST['subject'];

         error_reporting(E_ALL & ~E_NOTICE);
try{
    if(count($_POST) == 0) throw new \Exception('Form is empty');

    if ($name === ''){
        print json_encode(array('message' => 'Le nom ne peut être vide', 'code' => 0));
        exit();
    }
    if ($email === ''){
        print json_encode(array('message' => 'Le courriel ne peut être vide', 'code' => 0));
        exit();
    } else {
        if (!filter_var($email, FILTER_VALIDATE_EMAIL)){
            print json_encode(array('message' => 'Le format du courriel est invalide', 'code' => 0));
            exit();
        }
    }
    if ($subject === ''){
        print json_encode(array('message' => 'Le sujet ne peut être vide', 'code' => 0));
        exit();
    }
    if ($message === ''){
        print json_encode(array('message' => 'Le message ne peut être vide', 'code' => 0));
        exit();
    }
    $content="From: $name \nEmail: $email \nMessage: $message";
    $recipient = "chaput.yves@gmail.com";
    $mailheader = "From: contact@yveschaput.com \r\n";
    mail($recipient, $subject, $content, $mailheader);
    $result = array('message' => 'Courriel envoyé avec succès!', 'code' => 1);
}
catch (\Exception $e){
    $errorMessage = 'Une erreur s\'est produit. Veuillez réessayer plus tard';
    $result = array('type' => 'danger', 'message' => $errorMessage);
}
//
if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
    $encoded = json_encode($result);

    header('Content-Type: application/json');

    echo $encoded;
}
// else just display the message
else {
    echo $result['message'];
}

Adam Jakubowski staff commented 5 years ago

Now when I come to your site everything works, when I complete the contact form and click send, I receive a message that the message was sent successfully. So isn't something else not working that I can help you with ?


Adam Jakubowski staff answered 5 years ago


Hi,

Unfortunately, when I enter your form, I get a 500 error and I can't recreate the problem and help you solve it.

Best,

Adam



Please insert min. 20 characters.

FREE CONSULTATION

Hire our experts to build a dedicated project. We'll analyze your business requirements, for free.

Status

Answered

Specification of the issue

  • ForumUser: Free
  • Premium support: No
  • Technology: Other
  • MDB Version: -
  • Device: desktop
  • Browser: chrome
  • OS: Win 10
  • Provided sample code: Yes
  • Provided link: Yes