Wordpress ajax无法正常工作

时间:2015-09-01 12:50:40

标签: javascript php jquery ajax wordpress

我正在尝试通过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: 您可以看到,当我选择操作时,删除&#39;对于kakulsarma它的通过错误和ajax没有任何响应2)如果我选择删除&#39;删除女孩将被删除在行动,但&#34; kakulsarma将在选择&#39;不活动&#39;

1)

屏幕2:

你可以看到两个ajax请求,第一个请求是&#39;非活动&#39;第二个是删除&#39;结果是kakulsarma被删除了......

为什么这么开心?

1 个答案:

答案 0 :(得分:0)

 $('.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.

我认为这可能有用。