2014-02-27 61 views
0

我正在創建一個小插件,我想使用ajax提交表單數據,但它給出的響應爲零。如何在wordpress插件中使用ajax提交表格

這裏是我的電話我已經添加了所有代碼暫且

文件名的單個文件:index.php文件

<?php wp_enqueue_script("jquery");?> 
    <?php 

    function myaddgallery(){ 
    global $wpdb;  
    echo "abac"; 

    } 

    add_action('wp_ajax_myaddgallery' , 'myaddgallery'); 
    add_action('wp_ajax_nopriv_myaddgallery' , 'myaddgallery'); 

    ?> 
    <script type="text/javascript"> 
    jQuery(document).ready(function(){ 
    jQuery('#shaGalleryForm').submit(function(e){ 

     // prevent normal submit behaviour 
    e.preventDefault(); 
    var name = jQuery("#shaGalleryName").val(); 
     alert(name); 
      //var postData = jQuery("#shaGalleryForm").serialize(); 
      //console.log(postData); 
      var name = "shalu"; 
      jQuery.ajax({ 
       type: "POST", 
       url: "http://localhost/plugindevelop/wp-admin/admin-ajax.php", 
       data : {action: "myaddgallery", name : name}, 
       success: function(response){ 
        //console.log(postData); 
        console.log("added success"); 
        alert(response); 
       }, 
       error: function(data){ 
        console.log("fail"); 
       } 
      }); 
    }); 
}) ; 


</script> 


<div class='wrap'> 
    <h2>Add Gallery</h2> 
    <p>This is where from you can create new gallery</p> 
    <form name="shaGalleryForm" id="shaGalleryForm" method="post"> 
     <table class="form-table"> 
      <tr> 
       <th>Gallery Name</th> 
       <td> 
        <input type="text" id="shaGalleryName" name="shaGalleryName"> 
        <p class="description">Add gallery name also please avoid special character</p> 
       </td> 

      </tr> 


     </table> 
     <p class="submit"> 
      <input type="submit" value="Save" id="shaSaveGallery" name="shaSaveGallery" class="button button-primary"> 
     </p> 
    </form> 
</div> 
+0

看看[** AJAX插件**](http://codex.wordpress.org/AJAX_in_Plugins) –

回答

0

嘗試添加模具()時重複數據

function myaddgallery(){ 
global $wpdb;  
echo "abac"; 
die(); 
}