如何在wordpress ajax中传递action和formdata?

时间:2017-09-22 07:23:18

标签: php jquery ajax wordpress

我有一个注册表单,我正在尝试注册新成员。 我试图传递用于图像上传的formdata和wordpress中的ajax动作。目前我正在尝试在控制台中打印值。但是ajax函数根本没有被调用,因为动作不起作用。我怎样才能做到这一点。请帮帮我。

HTML

<form id="landlordregform" method="post" action="" enctype="multipart/form-data" novalidate="novalidate">
   <div class="registrationformbox landloardregformbox">
      <div id="custommsg"></div>
      <table width="100%">
         <tbody>
            <tr>
               <td><input type="text" name="username" placeholder="USERNAME*" id="username" class="valid" aria-invalid="false"></td>
               <td><input type="password" name="password" placeholder="PASSWORD*" id="password" class="valid validate-equalTo-blur" aria-invalid="false"></td>
               <td><input type="password" name="confirmpassword" placeholder="CONFIRM PASSWORD*" class="valid" aria-invalid="false"></td>
            </tr>
            <tr>
               <td><input type="text" name="name" placeholder="NAME*" class="valid" aria-invalid="false"></td>
               <td><input type="text" name="email" placeholder="EMAIL*" id="email" class="valid" aria-invalid="false"></td>
               <td><input type="text" name="telephone" placeholder="TELEPHONE*" class="valid" aria-invalid="false"></td>
            </tr>
            <tr>
               <td><input type="text" name="town_city" placeholder="TOWN/CITY*" class="valid" aria-invalid="false"></td>
               <td><input type="text" name="postcode" placeholder="POST CODE*" class="valid" aria-invalid="false"></td>
               <td><input type="file" name="profilepic" class="valid" aria-invalid="false"></td>
            </tr>
         </tbody>
      </table>
      <table width="100%">
         <tbody>
            <tr>
               <td><textarea name="address" placeholder="ADDRESS*" class="valid" aria-invalid="false"></textarea></td>
            </tr>
         </tbody>
      </table>
      <div class="submitubttonbox"><button type="button" name="landlordregisterbutton" id="landlordregisterbutton" class="qbutton white big_large">Register</button></div>
   </div>
</form>

的jQuery

jQuery("#landlordregisterbutton").click(function(){
        if(jQuery("#landlordregform").valid()){
            var formData=new FormData(document.getElementById('landlordregform'));  
            //formData.append( 'file', input.files[0] );            
            jQuery.ajax({
                url: jQuery("#cusajaxurl").val(),
                type: 'POST',
                data: formData + '&action=custom_landlord_registration_process', 
                cache: false,
                processData: false, 
                contentType: false,     
                success: function(data) {
                    console.log(data);
                }
            });
        }
    });

功能

add_action('wp_ajax_nopriv_custom_landlord_registration_process','custom_landlord_registration_process');

add_action('wp_ajax_custom_landlord_registration_process','custom_landlord_registration_process');

    function custom_landlord_registration_process(){
        //$formdata=array();
        //parse_str($_POST['formdata'],$formdata);


        print_r($_POST);

        die();
    }

2 个答案:

答案 0 :(得分:1)

在表单

中使用隐藏的输入
<input type="hidden" name="action" value="custom_landlord_registration_process">

因此您无需使用表单数据附加

答案 1 :(得分:0)

如果你在functions.php中编写了函数,那么在函数之后添加钩子: -

add_action('wp_ajax_custom_landlord_registration_process','custom_landlord_registration_process');

add_action('wp_ajax_nopriv_custom_landlord_registration_process','custom_landlord_registration_process');

希望它对你有用。