Guest User

Untitled

a guest
Jan 14th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.12 KB | None | 0 0
  1. Trying to implement jquery/ajax into html page
  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. $username = mysql_real_escape_string($_POST['Username']);
  19.  
  20. //mysql query to select field username if it's equal to the username that we check '
  21. $usernameresult = 'Select Username from User where Username = "'. $username .'"';
  22. $uresult = $conn->query($usernameresult);
  23.  
  24. //if number of rows fields is bigger them 0 that means it's NOT available '
  25. if($uresult->num_rows==1) {
  26. //and we send 0 to the ajax request
  27. echo 0;
  28. }else{
  29. //else if it's not bigger then 0, then it's available '
  30. //and we send 1 to the ajax request
  31. echo 1;
  32. }
  33. ?>
  34. <script src="jquery-1.8.1.min.js" type="text/javascript"></script>
  35. <SCRIPT LANGUAGE="JAVASCRIPT" TYPE="TEXT/JAVASCRIPT">
  36. $(document).ready(function() {
  37.  
  38. var checking_html = 'Checking...';
  39.  
  40. //when button is clicked
  41. $('#check_username_availability').click(function(){
  42. //run the character number check
  43. if($('#username').val().length < min_chars){
  44. $('#username_availability_result').html(checking_html);
  45. check_availability();
  46. }
  47. });
  48.  
  49. });
  50.  
  51. //function to check username availability
  52. function check_availability(){
  53.  
  54. //get the username
  55. var username = $('#username').val();
  56.  
  57. //use ajax to run the check
  58. $.post("check_username.php", { username: username },
  59. function(result){
  60. //if the result is 1
  61. if(result == 1){
  62. //show that the username is available
  63. $('#username_availability_result').html(username + ' is Available');
  64. }else{
  65. //show that the username is NOT available
  66. $('#username_availability_result').html(username + ' is not Available');
  67. }
  68. });
  69.  
  70. }
  71. </script>
  72.  
  73. <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
Add Comment
Please, Sign In to add comment