简体   繁体   中英

How can I make sure that same user shouldn't be added twice in jquery?

I am using two div(s). In the first div, all the users are shown on a page, with "ADD" Button Option. By adding Users, they get added to the second div using jQuery. But I want to make sure they do not get added twice. I want to implement a condition to check if a user is already added, then it should not be added or button should be disabled. I am writing my code below and I am using in wordpress but the problem is related to jquery-

<?php
/*
Template Name: Users
*/
?>
<style>
.container{
  width: 800px;
}
.left{
  float:left;
  width: 300px;
  background: ##66CDAA;
  border-right: thick-solid #fff;
}
.right{
  overflow: hidden;
  width: 500px;
  background: powderblue;
}
</style>
<?php
 get_header(); ?>
 <div id="primary" class="content-area">
   <main id="main" class="site-main" role="main">
   <div class="container">
   <div class="left" id="xyz">
     <table>
       <tr>
         <th>Name</th>
         <th>Gravatar Image</th>
         <th>Add to Div</th>
       </tr>
     <?php
      $sql ="SELECT * FROM wp_users";
      $results = $wpdb->get_results($sql) or die(mysql_error());
      foreach( $results as $result ){ ?>
        <tr>
          <td id="<?php echo $result->ID; ?>">
          <?php echo $result->display_name; ?>
          </td>
          <td>
          <?php echo get_avatar( $result->user_email, 42); ?>
          </td>
          <td>
          <input type="button" class="submit" data-id="<?php echo $result->ID; ?>" data-name="<?php echo $result->display_name; ?>" data-image="<?php echo get_avatar_url($result->user_email); ?>" value="ADD"><p style="display:none;">Added</p>
          </td>
        </tr>
        <?php
          }
        ?>
        </table>
 </div>
 <div class="right">
   <p>Added Users</p>
   <table class="table">
   <tr>
     <th>Name<th>
     <th>Image</th>
   </tr>
   </table>
 </div>
 </div>
</main>
     </div>
    <?php get_footer(); ?>

In my ajax.js file, I am adding the users to the second div, also there is a cancel button to cancel, but after cancelling a user from the second div, we should again be able to add the user to the second div.

(function($) {
      $(document).ready(function() {
      $(".submit").on("click", function(e){
      e.preventDefault();
      var id = $(this).data('id');
      var name = $(this).data('name');
      var image = $(this).data('image');
      //Here I want to check the condition that if a user is already added then it should not be added.
       $('.table').append('<tr id=" ' + id+ ' "><td>' + name + '</td><br><td><img src=" ' + image + ' "></td><td><input type="button" value="Cancel" class="cancel"></td></tr>');

  })
})
})(jQuery);

So, my question is how can I make sure if a user is already added, then it should not be added again. For this how can I check this. And if the added user get cancelled, then cancel button disappers and add button appears in the first div. And how can I hide the table if there is now users. in my case I tried, but it's not working.

Edit: In functions.php I have used the following code to enqueue to ajax.js file.

<?php 

function umar_scripts(){
    wp_enqueue_script( 'main_ajax', get_template_directory_uri() . '/js/ajax.js', array('jquery'), '', true);
}

add_action( 'wp_enqueue_scripts', 'umar_scripts' );

Assuming that you are adding uniqe id to each User row in your table, you can add a check if row with that id is present in table or not. use below code:

  (function($) {
$(document).ready(function() {
    $(".submit").on("click", function(e) {
        e.preventDefault();
        var id = $(this).data('id');
        var name = $(this).data('name');
        var image = $(this).data('image');
        if ($('table').find('#' + id).length <= 0) {
          {
            $('.table').append('<tr id=" ' + id + ' "><td>' + name + '</td><br><td><img src=" ' + image + ' "></td><td><input type="button" value="Cancel" class="cancel"></td></tr>');
          }

        })
    })
})(jQuery);

Here is how you can check if user exists or not:

if($('.table td#' + id).length) { // user exists }
else { // user doesn't exist }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM