How to get the sucess message in the same page after submitting the contact form?

前端 未结 5 1476
再見小時候
再見小時候 2021-01-02 11:31

I have created the contact form using HTML and PHP. Also, the mail is coming correctly to mail id. But after the success message, it is redirecting to the form.php

5条回答
  •  鱼传尺愫
    2021-01-02 11:54

    Using the mail.php on the same page where the form is could do the magic. the following code did the magic.

     'Name', 'email' => 'Email', 'subject' => 'Subject', 'message' => 'Message');
    
    $okMessage = 'Thank you for your message. I will write back soon.';
    $errorMessage = 'There is an error while sending message. Please try again later.';
    
    try {if (!empty($_POST)) {
        $emailText = "You have a new message from your contact form\n=====\n";
        foreach ($_POST as $key => $value) {
           if (isset($fields[$key])) {
             $emailText .= "$fields[$key]: $value\n";
           }
        }
        $headers = array('Content-Type: text/plain; charset="UTF-8";',
     'From: ' . $from,
     'Reply-To: ' . $from,
     'Return-Path: ' . $from,
        );
        mail($sendTo, $subject, $emailText, implode("\n", $headers));
        $responseArray = array('type' => 'success', 'message' => $okMessage);
        }
    }
    catch (\Exception $e) {
      $responseArray = array('type' => 'danger', 'message' => $e->getMessage());
    }
    if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
     $encoded = json_encode($responseArray);
     header('Content-Type: application/json');
     echo $encoded;
    } else {
    echo $responseArray['message'];
    }
    
    
    ?>
    
    
    
    
    
    
        
        
        
        Document
    
        
    
    
    

    CONTACT FORM


    By filling out the contact form; You may have information about us and current news.

    *This field must be fill.

提交回复
热议问题