Advertisement
Guest User

Untitled

a guest
Aug 5th, 2017
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $(document).ready(function() {
  2.     $('div.message-error').hide();
  3.     $('div.message-success').hide();
  4.     $('ul#characterList').css( 'margin-left', '120px' );
  5.    
  6.     function removeCharacter(characterID, characterName) {
  7.         event.preventDefault();
  8.         $(this).parent().remove();
  9.        
  10.         var option = $( '<option value=' + characterID + '>' + characterName + '</opton>' );
  11.        
  12.         $( '#charactersDrop' ).append( option );
  13.     }
  14.    
  15.     // Long version
  16.     function HandlerCharacters() {
  17.         function isDupe(which) {
  18.             var result = false;
  19.             $('ul#characterList li').each(function(i, e) {
  20.                 if ($(e).data('characterID') == which) {
  21.                     result = true;
  22.                     return false; // break out of .each()
  23.                 }
  24.             });
  25.             return result;
  26.         }
  27.         var characterID = $('#charactersDrop option:selected').val();
  28.         var characterName = $('#charactersDrop option:selected').text();
  29.         if (characterID > 0 && !isDupe(characterID)) {
  30.             // Create the anchor element
  31.             var anchor = $( '<a href="#">Remove</a>' );  
  32.            
  33.             // Create a click handler for the anchor element
  34.             anchor.click( function() {
  35.                 $( this ).parent().remove();
  36.                 return false;  // makes the href in the anchor tag ignored
  37.             } );
  38.            
  39.             // Create the <li> element with its text, and then append the anchor inside it.
  40.             var li = $( '<li>' + characterName + '&nbsp;</li>' ).append( anchor );
  41.             li.data( 'characterID', characterID );
  42.            
  43.             // Append the new <li> element to the <ul> element
  44.             $( '#characterList' ).append( li );
  45.         }
  46.     }
  47.    
  48.     $("#handlerForm").validate({
  49.         rules: {
  50.             firstName: {
  51.                 required: true
  52.             },
  53.             lastName: {
  54.                 required: true
  55.             },
  56.             userName: {
  57.                 required: true,
  58.                 minlength: 5
  59.             },
  60.             password: {
  61.                 required: true,
  62.                 minlength: 5
  63.             },
  64.             password2: {
  65.                 required: true,
  66.                 minlength: 5,
  67.                 equalTo: "#password"
  68.             },
  69.             email: {
  70.                 required: true,
  71.                 email: true
  72.             },
  73.             statusID: {
  74.                 required: true
  75.             },
  76.             isAdmin: {
  77.                 required: true
  78.             }
  79.         },
  80.         messages: {
  81.             firstName: "Please enter your first name!",
  82.             lastName: "Please enter your last name!" ,
  83.             userName: {
  84.                 required: "Please enter a username!",
  85.                 minlength: "Your username must consist of at least 5 characters!"
  86.             },
  87.             password: {
  88.                 required: "Please provide a password!",
  89.                 minlength: "Your password must be at least 5 characters long!"
  90.             },
  91.             password2: {
  92.                 required: "Please provide a password!",
  93.                 minlength: "Your password must be at least 5 characters long!",
  94.                 equalTo: "Please enter the same password as above!"
  95.             },
  96.             statusID: "Please enter the handler's status!",
  97.             isAdmin: "Please enter the handler's administrative status!",
  98.             email: "Please enter a valid email address!"
  99.         },
  100.         submitHandler: function(form) {
  101.             var handlerID = $("input#handlerID").val();
  102.             var defaultChar = $("select#charactersDrop option:selected").text();
  103.             var userName = $("input#userName").val();
  104.             var firstName = $("input#firstName").val();
  105.             var lastName = $("input#lastName").val();
  106.             var password = $("input#password").val();
  107.             var email = $("input#email").val();
  108.             var statusID = $("select#statusID").val();
  109.             var isAdmin = $("select#isAdmin").val();  
  110.             var liElements = $("ul#characterList li");
  111.             var characterIDList = "";
  112.             for( var i = 0; i < liElements.length; i++ ) {
  113.                 var liElement = $( liElements[ i ] );
  114.                
  115.                 // only start appending commas in after the first characterID
  116.                 if( i > 0 ) {
  117.                     characterIDList += ",";
  118.                 }
  119.                
  120.                 // append the current li element's characterID to the list
  121.                 characterIDList += liElement.data( 'characterID' );
  122.             }
  123.             var dataString = 'userName=' + userName + '&firstName=' + firstName + '&lastName=' + lastName + '&email=' + email + '&statusID=' + statusID + '&isAdmin=' + isAdmin + '&characterIDList=' + characterIDList + '&handlerID=' + handlerID + '&editHandler=True';
  124.             $.ajax({
  125.                 type: "POST",
  126.                 url: "processes/handlers.php",
  127.                 data: dataString,
  128.                 success: function(myNewVar) {
  129.                     if (myNewVar == 'good') {
  130.                         $('div.message-error').hide();
  131.                         $("div.message-success").html("<h6>Operation successful</h6><p>" + userName + " saved successfully.</p>");
  132.                         $("div.message-success").show().delay(10000).hide("slow", function() {
  133.                                  $('#content').load('mods/handlers.php');  
  134.                         });  
  135.                     } else {
  136.                         $('div.message-success').hide();
  137.                         $("div.message-error").html("<h6>Operation unsuccessful</h6><p>" + userName + " already exists in the database.</p>");
  138.                         $("div.message-error").show();    
  139.                     }
  140.                 }
  141.             });
  142.             return false;
  143.         }    
  144.     });
  145. });
  146.  
  147. <label for="charactersDrop">Characters</label>
  148.             <select class="dropdown" name="charactersDrop" id="charactersDrop" title="Characters Dropdown">
  149.                 <option value="">- Select -</option>
  150.                 <?php
  151.                     while ( $row = mysqli_fetch_array (  $charactersByHandlerResult, MYSQL_ASSOC ) ) {
  152.                         print "<option value=\"".$row['ID']."\">".$row['characterName']."</option>\r";
  153.                     }
  154.                 ?>
  155.             </select>
  156.             <input type="button" value="Add Character" class="" onclick="HandlerCharacters()"/>
  157.             <ul id="characterList">
  158.               <?php mysqli_data_seek( $handlerResult, 0 ) ?>
  159.               <?php while($row = mysqli_fetch_array ( $handlerResult, MYSQL_ASSOC )): ?>
  160.                 <li><?php echo htmlentities($row['characterName']); ?> <a href="#" onclick="removeCharacter(<?php echo $row['ID'] . ','. "'" . $row['characterName'] . "'"; ?>);">Remove</a></li>
  161.               <?php endwhile; ?>
  162.             </ul>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement