Click here to Skip to main content
15,881,898 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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 html and css codes below. Actually I am doing this in wordpress but the problem is in jquery. I have page template named users.
PHP
/*
Template Name: Users
*/
?>

.container{
  width: 800px;
}
.left{
  float:left;
  width: 300px;
  background: ##66CDAA;
  border-right: thick-solid #fff;
}
.right{
  overflow: hidden;
  width: 500px;
  background: powderblue;
}

<?php
 get_header(); ?>
 <div id="primary" class="content-area">
   
   <div class="container">
   <div class="left" id="xyz">
     
       
         
         
         
       
     <?php
      $sql ="SELECT * FROM wp_users";
      $results = $wpdb->get_results($sql) or die(mysql_error());
      foreach( $results as $result ){ ?>
        
          
          
          
        
        <?php
          }
        ?>
        <table><tbody><tr><th>Name</th><th>Gravatar Image</th><th>Add to Div</th></tr><tr><td id="<?php echo $result->ID; ?>">
          <?php echo $result->display_name; ?>
          </td><td>
          <?php echo get_avatar( $result->user_email, 42); ?>
          </td><td>
          <p>Added</p>
          </td></tr></tbody></table>
 </div>
 <div class="right">
   <p>Added Users</p>
   
   
     
   
   <table class="table"><tbody><tr><th>Name</th><th>
     </th><th>Image</th></tr></tbody></table>
 </div>
 </div>



     </div>
    <?php get_footer(); ?>


What I have tried:

I have a jquery file using which I am adding user from first div to another. But the problem is that I want to check a condition before adding the user. If a user is already added then it should not be added again, instead a alert should be prompted that "user is already added". My code is below

JavaScript
(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 td#' + id).length) {
       //Also tried $('.table').find('td').attr('id'); but undefined is the result
         alert("added");
        } else {
          $('.table').append('' + name + '<br>');
      //  }
  })
})
})(jQuery);
Posted
Updated 16-Jun-17 2:12am
v2

You've answered your own question. Make sure they're not added twice.

You can (1) check the database for their existence [such as (COUNT(*) > 0)] and/or (2) add a UNIQUE constraint to the appropriate fields in your database, after which, you'd handle the error return.

You're should also consider: how do you define a user as being the same one? In real-life programming, that's not always a simple decision. People share names, for example. They may select the same avatar. They may be creating a new user name but it's the same person.
 
Share this answer
 
v2
Comments
Member 13262839 16-Jun-17 7:58am    
The problem is not about inserting users from the database. Actually I am adding users from one div to another div with the help of a button using jquery. Once I add a user, I can add that user twice on clicking the same button. So I want to check a condtion that if a user is already added, then alert should be prompted that "user is already added".
I noticed your SQL and perhaps jumped the gun.

The way I'd do this is to remove the record from the first div when I copy it to the second div. Think of it like the multi-pick lists (Windows tool-bar customization?) where you double-click and item and it goes from one area to the other. You cannot pick it twice because it's either in one area or another.

The individual names could be in individual div's or spans of their own, each with an id. Or a table inside each div.

You can recolor them so you know what you picked, disable the 'pick event' for them, or any number of things, depending upon what you wish to see.

Or, primitively, you can keep track of your picks in an array and search it before completing the copy procedure.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900