douyinglan2599 2018-07-21 14:49
浏览 341

admin-ajax.php返回0

I'm currently working on a WordPress contact form, which is created as a plugin. I need the form after submit to send an email without reloading the page. When it reloads everything is ok. The problem is when submit button is pressed and e.preventDefault() is active the browser shows me 500 (Internal Server Error) with admin-ajax.php = 0

P.S The post is edited

     <?php
        add_action( 'admin_footer', 'html_form_code' ); // Write our JS below here
        //
        function html_form_code() {
        ?>
        <form action="<?php esc_url( $_SERVER['REQUEST_URI'] )?>" method="post" class="contact-form" id="contact-form" >
            <div class=header-contact>
                <p><h2>Contact Form</h2></p>
                <hr>
            </div>
            <div class=input-containers>
                <input type="text" id="name" name="cf-name" pattern="[a-zA-Z0-9 ]+" value="" size="40" placeholder="Име и фамилия"/>
            </div>
            <div class=input-containers>
                <input type="email" id="email" name="cf-email" value="" size="40" placeholder="Поща"/>
            </div>
            <div class=input-containers>
                <input type="text" id="subject" name="cf-subject" pattern="[a-zA-Z ]+" value="" size="40" placeholder="Относно"/>
            </div>
            <div class=input-containers>
                <textarea rows="10" id="message" cols="35" name="cf-message" placeholder="Текст"></textarea>
            </div>
            <div class=input-containers>
                <input type="submit" name="cf-submitted" value="Send" id="submitForm">
            </div>
            <p id="verify" style="display:none;">Your message has been sent.<br /><br /></p> 
        </form>
        <script>
    jQuery('#contact-form').submit(function(e){

         e.preventDefault();
         var name =  jQuery('#name').val();
         var email =  jQuery('#email').val();
         var subject =  jQuery('#subject').val();
         var message =  jQuery('#message').val();
                  jQuery.ajax({
                   url: '<?php echo admin_url('admin-ajax.php'); ?>',
                   type: "POST",
                   data:{ 
                      action: 'send_email', 
                      name: name,
                      email: email,
                      subject: subject,
                      message: message,
                        },
                   success:function(res){
            alert("Email Sent.");
            }
         }); 
        });
    </script>
        <?php
        }
        add_action( 'wp_ajax_send_email', 'deliver_mail' );
        add_action( 'wp_ajax_nopriv_send_email', 'deliver_mail' );

        function deliver_mail() {
            require_once "wp-includes/class-phpmailer.php";
            if (isset($_POST["cf-submitted"])) {

                // sanitize form values
                $name    = sanitize_text_field( $_POST["cf-name"] );
                $email   = sanitize_email( $_POST["cf-email"] );
                $subject = sanitize_text_field( $_POST["cf-subject"] );
                $message = esc_textarea( $_POST["cf-message"] );

                // get the blog administrator's email address

                //$to = "email@gmx.com";
                $headers = "From: $name <$email>" . "
";

                // Localhost
                $mail = new PHPMailer(true);
                $mail->IsSMTP(); // telling the class to use SMTP
                $mail->CharSet = 'UTF-8';

                $mail->SMTPDebug = 0;                     // enables SMTP debug information (for testing)
                $mail->SMTPAuth = true;                  // enable SMTP authentication
                $mail->SMTPSecure = "ssl";                 // sets the prefix to the servier
                $mail->Host = "mail.gmx.com";      // sets GMX as the SMTP server for example: mail.gmx.com
                $mail->Port = 465;                 // set the SMTP port for the GMX server


                $mail->Username = $email;
                $mail->Password = 'pass';

                $mail->SetFrom($email, $name);
                $mail->AddAddress($email);

                $mail->Subject = $subject;
                $mail->MsgHTML($message);

                $headers .= "Content-Type: text/html; charset=utf-8";
                $headers .= "Content-Transfer-Encoding: 8bit";

                try {
                    $mail->send();
                    $msg = "An email has been sent for verfication.";
                    $msgType = "success";

                    wp_safe_redirect( home_url(), 302 );
                    exit();

                } catch (Exception $ex) {
                    $msg = $ex->getMessage();
                    $msgType = "warning"; 

                    wp_safe_redirect( home_url(), 302 );
                    exit();
                }  

        add_action( 'init', function() {
            if ( ! empty( $_POST['form_submitted'] ) ) {
                deliver_mail();        
            }
        });

        function cf_shortcode() {
            ob_start();
            //deliver_mail();
            html_form_code();

            //ob_end_flush(); 
            return ob_get_clean();
        }
        add_shortcode( 'contact_form_second', 'cf_shortcode' );
  • 写回答

1条回答 默认 最新

  • drl47263 2018-07-21 15:21
    关注

    You have to specify the data you want to send, try this:

     jQuery(document).ready(function($) {
        $('#contact-form').submit(function(e) {
            e.preventDefault();
            //e.stopPropagation();
       const email=$('#email').val()
    const email=$('#email').val()
    const name=$('#name').val()
    const subject=$('#subject').val()
    const message=$('#message').val()
    const data={
       'name':name,
        'email':email,
        'subject':subject,
      ' message':message
     }
    
            $.ajax({
                type: 'post',
                url: '/',
                data:{...data},
                success: function () {
                  //alert(formData);
                  $('#verify').show();
                 $('#verify').fadeIn().html("gff");
                    setTimeout(function() {
                        $('#verify').fadeOut("slow");
                    }, 5000 );
                  $(".contact-form")[0].reset();
                },
            error: function() {
                  alert('Error!');
                }
              });
        });
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程