簡體   English   中英

WordPress Ajax無法正常工作

[英]Wordpress ajax is not working properly

我正在嘗試通過wordpress ajax執行更新查詢,這是我的代碼:

在插件頁面上:

<tr>
                  <td id="username"><?php echo $team->username;?></td>
                  <td id="full_name"><?php echo $rs->user->first_name;?></td>
                  <td>
                      <video class="video" width="200" height="100">
                        <source src="<?php echo $rs->user->profile_video;?>" type="video/mp4">
                        Your browser does not support the video.
                      </video>
                </td>
                  <td id="about"><?php echo $team->about;?></td>
                  <td><?php echo $team->created;?></td>
                  <td id="status"><?php echo $status;?></td>
                  <td><select class="action">
                      <option>-Select-</option>
                      <option value="1" >Active</option>
                      <option value="0">Inactive</option>
                      <option value="edit">Edit</option>
                      <option value="4">Delete</option>
                  </select></td>
                </tr>

Java腳本:

$('.action').on("change",function(e){
  e.preventDefault();
var def=$(this);
var select=$(this).val();
var username=$(this).closest("tr").children('td#username').text();
var full_name=$(this).closest("tr").children('td#full_name').text();
var about=$(this).closest("tr").children('td#about').text();


// Active Inactive Delete

if( select ==='0' || select ==='1' || select ==='4'){
    $.ajax({
      method: 'post',
      url:'<?php echo admin_url( 'admin-ajax.php' ); ?>',
      data:{
        action: 'update_member',
        username : username,
        select : select,
      },
      success:function(response){
            if(response != ''){

            var obj = JSON.parse(response);


                // if inactive
                if(obj.action =='0')
                {
                  def.closest("tr").children('td#status').html('<label class="label label-danger">Inactive</label>');
                }
                 // if active
                if(obj.action =='1')
                {
                  def.closest("tr").children('td#status').html('<label class="label label-success">Inactive</label>');
                }
                 // if Delete
                if(obj.action =='4')
                {
                  def.closest("tr").remove();
                }
          }else{
            alert('Inactive Member First ');
          }
     }
    });

}



})

和Ajax回叫:

add_action('wp_ajax_nopriv_update_member', 'update_member_callback');
add_action('wp_ajax_update_member', 'update_member_callback');

function update_member_callback(){

global $wpdb;

  $update =$wpdb->update('wp_frankly_team', array('status'=>$_REQUEST['select']), array('username'=>$_REQUEST['username']));
  if($update>0){echo json_encode(array('status'=>'updated', 'action'=>$_REQUEST['select']));}

  wp_die();

}

現在我的問題是,當我通過選擇框選擇活動或不活動操作時,它的工作情況很好,但是當我選擇刪除選項時,它通過錯誤了。 如果狀態為活動,則它不起作用。 但是在非活動狀態下工作正常。 請查看屏幕截圖並告訴我我在哪里錯誤。 我沒有為此添加任何條件

屏幕1:您可以看到,當我為kakulsarma選擇了動作'delete'時,它通過錯誤並且ajax沒有通過任何響應2)如果我對action選擇了'delete',則孟買女孩將被刪除,但是“ kakulsarma將在選擇后被刪除“無效”

1)

屏幕2:

您可以看到兩個ajax請求,第一個是“無效”,第二個是“刪除”,結果是kakulsarma被刪除...

為什么會這樣?

 $('.action').on("change",function(e){
    e.preventDefault();
    var def=$(this);
    var select=$(this).val();
    var username=$(this).closest("tr").children('td#username').text();
    var full_name=$(this).closest("tr").children('td#full_name').text();
    var about=$(this).closest("tr").children('td#about').text();


    // Active Inactive Delete

    if( select ==='0' || select ==='1' || select ==='4'){
        $.ajax({
          method: 'post',
          url:"<?php echo admin_url( 'admin-ajax.php' ); ?>",//change this
          data:{
            action: 'update_member',
            username : username,
            select : select,
          },
          success:function(response){
                if(response != ''){

                var obj = JSON.parse(response);


                    // if inactive
                    if(obj.action =='0')
                    {
                      def.closest("tr").children('td#status').html('<label class="label label-danger">Inactive</label>');
                    }
                     // if active
                    if(obj.action =='1')
                    {
                      def.closest("tr").children('td#status').html('<label class="label label-success">Inactive</label>');
                    }
                     // if Delete
                    if(obj.action =='4')
                    {
                      def.closest("tr").remove();
                    }
              }else{
                alert('Inactive Member First ');
              }
         }
        });

    }
   }); //Add semicolon here.

我認為可能是可行的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM