我不断从ajax在POST上收到404错误代码

时间:2018-11-26 15:27:02

标签: php ajax wordpress mailer

我目前正在为客户在wordpress网站上工作,我需要手动创建邮件程序以从表单发送客户信息。我需要发送此表单,其中包含客户添加到客户电子邮件中的动态数据。话虽这么说,但我在提交表单时遇到了错误: enter image description here

我的HTML结构:

<form action="handler.php" method="post" role="form">

                <div class="group">
                  <input type="text" required id="inputName">
                  <span class="highlight"></span>
                  <span class="bar"></span>
                  <label for="inputName">Name</label>
                </div>

                <div class="group">
                  <input type="text" required id="inputEmail"> 
                  <span class="highlight"></span>
                  <span class="bar"></span>
                  <label for="inputEmail">Email</label>
                </div>

                <div class="group">
                  <input type="text" required id="inputNumber">
                  <span class="highlight"></span>
                  <span class="bar"></span>
                  <label for="inputNumber">Mobile No</label>
                </div>

                <div class="form-group">
                  <h6 for="exampleSelect1" class="occastion-title">Occasion</h6>
                  <select class="form-control" id="exampleSelect1">
                                <option>Wedding</option>
                                <option>Conference</option>
                                <option>Sales and Marketing</option>
                                <option>Other</option>

                            </select>
                </div>

                <div class="group">
                  <span class="highlight"></span>
                  <span class="bar"></span>
                  <label for="inputMessage">Message</label>
                  <textarea type="text" id="form8 inputMessage" class="md-textarea form-control" rows="4"></textarea>
                </div>

                <div class="group mb-2">
                  <div id="render-quote"></div>
                </div>
              </form>

我的Javscript是:

var url = "https://developer-website.org/";

function submitContactForm(){
    var reg = /^[A-Z0-9._%+-]+@([A-Z0-9-]+\.)+[A-Z]{2,4}$/i;
    var name = $('#inputName').val();
    var email = $('#inputEmail').val();
    var number = $('#inputNumber').val();
    var message = $('#inputMessage').val();
    if(name.trim() == '' ){
        alert('Please enter your name.');
        $('#inputName').focus();
        return false;
    }else if(email.trim() == '' ){
        alert('Please enter your email.');
        $('#inputEmail').focus();
        return false;
    }else if(email.trim() != '' && !reg.test(email)){
        alert('Please enter valid email.');
        $('#inputEmail'
          ).focus();
        return false;
    }else if(number.trim() != '' && !reg.test(email)){
        alert('Please enter valid number.');
        $('#inputNumber'
          ).focus();
        return false;
    }else{
        $.ajax({
            type:'POST',
            url: url+'form/handler.php',
            data:'contactFrmSubmit=1&name='+name+'&email='+email+'&message='+message,
            beforeSend: function () {
                $('.submitBtn').attr("disabled","disabled");
                $('.modal-body').css('opacity', '.5');
            },
            success:function(msg){
              console.log("yay");
                if(msg == 'ok'){
                    $('#inputName').val('');
                    $('#inputEmail').val('');
                    $('#inputMessage').val('');
                    $('.statusMsg').html('<span style="color:green;">Thanks for contacting us, we\'ll get back to you soon.</p>');
                }else{
                    $('.statusMsg').html('<span style="color:red;">Some problem occurred, please try again.</span>');
                }
                $('.submitBtn').removeAttr("disabled");
                $('.modal-body').css('opacity', '');
            }
        });
    }
}

和我的php代码:

    <?php
if(isset($_POST['contactFrmSubmit']) && !empty($_POST['name']) && !empty($_POST['email']) && (!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) === false) && !empty($_POST['message'])){

    // Submitted form data
    $name   = $_POST['name'];
    $email  = $_POST['email'];
    $message= $_POST['message'];

    /*
     * Send email to admin
     */
    $to     = 'phillipdanielscottsc@gmail.com';
    $subject= 'Contact Request Submitted';

    $htmlContent = '
    <h4>Contact request has submitted at CodexWorld, details are given below.</h4>
    <table cellspacing="0" style="width: 300px; height: 200px;">
        <tr>
            <th>Name:</th><td>'.$name.'</td>
        </tr>
        <tr style="background-color: #e0e0e0;">
            <th>Email:</th><td>'.$email.'</td>
        </tr>
        <tr>
            <th>Message:</th><td>'.$message.'</td>
        </tr>
    </table>';

    // Set content-type header for sending HTML email
    $headers = "MIME-Version: 1.0" . "\r\n";
    $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

    // Additional headers
    $headers .= 'From: CodexWorld<sender@example.com>' . "\r\n";

    // Send email
    if(mail($to,$subject,$htmlContent,$headers)){
        $status = 'ok';
    }else{
        $status = 'err';
    }

    // Output status
    echo $status;die;
}

0 个答案:

没有答案