Admin-ajax.php 404 error

时间:2021-01-26 作者:pythonDON

我正在尝试提交以下表格,但无法正常工作:

HTML:

<div class= "container">
    
<div class="row">
<div class= "col-md-10 mx-auto text-center">
    <div  style="display:none;" id="success">
        <p class="pb-1 pt-1" style="color:white;background-color:#008000;font-size:20px;font-family:poppins;border-radius:3px;border:3px solid #008000;">Your Request Has Been Sent!</p>
    </div>
    
   
    
<form method="POST" id="request_quote" action="request_quote_ajax">

<div class="form-group ">
      <input type="hidden" name="action" value="request_quote_ajax">
    <select placeholder="Product Or Service Of
    Interest" class="form-control form-control-lg" id="product" type="text" name="product" required>
     <option value="">Product Or Service Of
    Interest</option>
     <option value="Solar PV System">Solar PV System</option>
     <option value="Solar Water Heater">Solar Water Heater</option>
     <option value="Solar Pool Heating">Solar Pool Heating</option>
     <option value="Servicing">Servicing</option>
    </select>
     <div class="form-group">
        <div class="form-row">
         <div class="col-md-6">
             <input type="text" class="form-control form-control-lg" id="fullname" name="fullname" placeholder="Full Name" required>
             </div>
              <div class="col-md-6">
              <input type="text" class="form-control form-control-lg" id="email" name="email"  placeholder="Email" required>
    </div>
    </div>
    </div>
    
    
    <div class="form-group">
   
  
  <div class="form-group">
     <div class="form-row">
         <div class="col-md-6">
        <input type="text" class="form-control form-control-lg" name="phonenumber1" id="phonenumber1" placeholder="Phonenumber 1" required>
    </div>
        

         <div class="col-md-6">
    <input type="text" class="form-control form-control-lg" name="phonenumber2" id="phonenumber2" placeholder="Phonenumber 2" >
        </div>
        </div>
</div>
  
  <div class="form-group">
    <div class="form-row">
             <div class="col-md-6">
    <input type="text" class="form-control form-control-lg " id="exampleInputPassword1" placeholder="Street Address" name="address" required>
            </div>
     <div class="col-md-6">
    <input type="text" class="form-control form-control-lg" id="parish" name="parish" placeholder="Parish" required>
  </div>
  </div>
  </div>
  
  <div class="form-group">
    <textarea type="textarea" class="form-control form-control-lg" name="_usermessage" id="_usermessage" placeholder="Your Message" required></textarea>
  </div>
  
  <button type="submit" name="submit" class="btn btn-primary">GO SOLAR</button>
</div>

</div>
</form>
Javascript:

<script>
$(document).ready(
    $(\'#request_quote\').submit(function(e){
    e.preventDefault();
    jQuery.post({ 
        
         url: ajaxurl,
         data: { $(this).serialize() },
         
        die();
         })
       })

</script>
我使用以下脚本使ajax url可用:

    add_action(\'wp_head\', \'myplugin_ajaxurl\');
       function myplugin_ajaxurl() 
               {
                 echo \'<script type="text/javascript">
             var ajaxurl = "\' . admin_url(\'admin-ajax.php\') . \'";
                    </script>\';
               }
   
我的php函数如下:

    add_action(\'wp_ajax_nopriv_request_quote_ajax\', \'request_quote_ajax\');
    add_action(\'wp_ajax_request_quote_ajax\', \'request_quote_ajax\');


function request_quote_ajax()
{

    // if the submit button is clicked, send the email
    if (isset($_POST[\'submit\'])) {
        //user posted variables
        $name = sanitize_text_field($_POST[\'fullname\']);
        $email = sanitize_email($_POST[\'email\']);
        $product = sanitize_text_field($_POST[\'product\']);
        $phonenumber1 =  sanitize_text_field($_POST[\'phonenumber1\']);
        $phonenumber2 =   sanitize_text_field($_POST[\'phonenumber2\']);
        $address =  sanitize_text_field($_POST[\'address\']);
        $parish =  sanitize_text_field($_POST[\'parish\']);
        $_usermessage = esc_textarea($_POST[\'_usermessage\']);

        //combined all fields inputs
        $message = "Request For Quote : {$name} \\n {$product} \\n {$phonenumber1} \\n {$phonenumber2} \\n {$address} \\n {$parish} \\n {$_usermessage} ";

        //php mailer variables
        
        $third_email = "[email protected]";
        $to = $third_email;
        $subject = "Quotation Request From {$name}";
        
        wp_mail($to, $subject, strip_tags($message));
        //$headers = \'From: <[email protected]>\' . "\\r\\n" .\'Reply-To: \' . $email . "\\r\\n";
    }
     
    else{ do_action( \'wp_mail_failed\', WP_Error, $error ); }
 
  
    wp_die();
}


       
每次我尝试提交表单时,都会出现404错误。我在类似的帖子上尝试了所有的解决方案,但都没有成功,请帮助我

2 个回复
SO网友:moped

表单标记中的操作属性错误。您可以删除它,jQuery提交函数将完成这项工作。

SO网友:Nico Cepi

当尝试在wp函数上执行操作时,始终必须在post数据上传递操作名称。在JavaScript文件上更改为:

数据:{$(this).serialize(),操作:\'request\\u quote\\u ajax\'}

相关推荐

未定义jQuery--某些主题功能不起作用!

I know that this issue is common and there are many similar questions about it but still my problem is a little different我对WordPress的最新更新有疑问。侧菜单和搜索栏不工作,除非在自定义模式下,浏览器加载迁移jquery版本3。我尝试了所有不同的解决方案(手动加载jquery-WordPress插件-编辑主题代码-添加一些函数来加载带挂钩和不带挂钩的jquery),但都没有成功