Guest User

Untitled

a guest
Jan 14th, 2019
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.32 KB | None | 0 0
  1. Trying to retrieve username from the database jquery
  2. echo "<form method='post' action='regprocess.php' id='registerform'>";
  3. echo '<fieldset class="register">';
  4. echo"<h2>Register</h2>";
  5. echo "<ul>";
  6. echo '<li><label for="FirstName">First Name: </label> <input type="text" name="FirstName" id="FirstName"></li>';
  7. echo '<li><label for="LastName">Last Name: </label> <input type="text" name="LastName" id="LastName"></li>';
  8. echo '<li><label for="Email">Email: </label><input type="email" name="Email" id="Email"></li>';
  9. echo '<li><label for="Username">Username: </label><input type="text" name="Username" id="Username"></li>';
  10. echo '<li><input type="button" id="check_username_availability" value="Check Availability"></li>';
  11. echo '<div id="username_availability_result"></div>';
  12. echo '<li><label for="Password">Password: </label><input type="password" name="Password" id="Password"></li>';
  13. echo '<li><input type="submit" value="Register"></li>';
  14. echo "</ul>";
  15. echo "</fieldset>";
  16. echo "</form>";
  17.  
  18.  
  19. <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
  20.  
  21. <SCRIPT LANGUAGE="JAVASCRIPT" TYPE="TEXT/JAVASCRIPT">
  22. $(document).ready(function() {
  23.  
  24. var checking_html = 'Checking...';
  25.  
  26. //when button is clicked
  27. $('#check_username_availability').click(function(){
  28. //run the character number check
  29. $('#username_availability_result').html(checking_html);
  30. check_availability();
  31. });
  32.  
  33. });
  34.  
  35. //function to check username availability
  36. function check_availability(){
  37.  
  38. //get the username
  39. var username = $('#username').val();
  40.  
  41. //use ajax to run the check
  42. $.post("check_username.php", { username: username },
  43. function(result){
  44. //if the result is 1
  45. if(result == 1){
  46. //show that the username is available
  47. $('#username_availability_result').html(username + ' is Available');
  48. }else{
  49. //show that the username is NOT available
  50. $('#username_availability_result').html(username + ' is not Available');
  51. }
  52. });
  53.  
  54. }
  55. </script>
  56.  
  57.  
  58. <?php
  59. $conn = new mysqli('sapphire', 'cgreenheld', '', 'cgreenheld_dev');
  60. $username = mysqli_real_escape_string($conn, $_POST['Username']);
  61.  
  62. //mysql query to select field username if it's equal to the username that we check '
  63. $usernameresult = 'Select Username from User where Username = "'. $username .'"';
  64. $uresult = $conn->query($usernameresult);
  65.  
  66. //if number of rows fields is bigger them 0 that means it's NOT available '
  67. if($uresult->num_rows==1) {
  68. //and we send 0 to the ajax request
  69. echo 0;
  70. }else{
  71. //else if it's not bigger then 0, then it's available '
  72. //and we send 1 to the ajax request
  73. echo 1;
  74. }
  75. ?>
  76.  
  77. echo '<li><label for="Username">Username: </label><input type="text" name="Username" id="Username"></li>';
  78.  
  79. var username = $('#username').val();
  80.  
  81. $.post("check_username.php", { username: username },
  82.  
  83. $username = mysqli_real_escape_string($conn, $_POST['Username']);
Add Comment
Please, Sign In to add comment