Guest User

Untitled

a guest
Dec 6th, 2018
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.64 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11/jquery.min.js">
  5. </script>
  6.  
  7. <script>
  8. $(document).ready(function() {
  9.  
  10. //the min chars for promo-code
  11. var min_chars = 6;
  12.  
  13. //result texts
  14. var checking_html = 'Checking...';
  15.  
  16. //when keyup
  17. $('#code').keyup(function(event){
  18. //run the character number check
  19. if($('#code').val().length == min_chars){
  20.  
  21. //show the checking_text and run the function to check
  22. $('#Promo_code_status').html(checking_html);
  23. check_code();
  24. }
  25. });
  26.  
  27. });
  28.  
  29. //function to check the promo code
  30. function check_code(){
  31.  
  32. //get code
  33. var code = $('#code').val();
  34.  
  35. //use ajax to run the check
  36. $.post("check_code.php", { code: code },
  37. function(result){
  38.  
  39. //if the result is 0
  40. if(result == 0){
  41. //show that the code is correct
  42. $('#Promo_code_status').html(code + ' is correct.');
  43. }else if(result == 1){
  44. //show that the code is correct, but already has been used
  45. $('#Promo_code_status').html(code + ' has allready been used.');
  46. }else{
  47. //show that the code is not correct
  48. $('#Promo_code_status').html(code + ' is not correct.');
  49. }
  50. });
  51. }
  52. </script>
  53. </head>
  54. <body>
  55.  
  56. <center><h1>Enter Promo Code</h1>
  57. <form method="post" action="check_code.php">
  58. <input type="text" id="code" name="code" maxlength="6" />
  59. <div id="promo_code_status"></div>
  60. <br>
  61. <input type="submit" value="Let's go"></center>
  62.  
  63. </form>
  64.  
  65. </body>
  66. </html>
  67.  
  68. <?php
  69.  
  70. //connect to database
  71. $user = "***"; //Username
  72. $pass = "***"; //Password
  73. $host = "localhost"; //Host
  74. $dbdb = "***"; //Database name
  75.  
  76. $connect = mysqli_connect($host, $user, $pass, $dbdb);
  77. if(!$connect)
  78. {
  79. trigger_error('Error connection to database: '.mysqli_connect_error());
  80. }
  81.  
  82. //get the code
  83. mysqli_real_escape_string($connect, $_POST['code']);
  84.  
  85. //mysql query to select field code if it's equal to the code that we checked '
  86. $result = mysqli_query($connect, 'select Code from Codes where Code = "'. $code .'"');
  87. $record = mysqli_fetch_array($result);
  88.  
  89. //if number of rows fields is bigger them 0 that means the code in the database'
  90. if(mysqli_num_rows($result) > 0){
  91. if($record['used'] == 0) {
  92. //and we send 0 to the ajax request
  93. echo 0;
  94. } else{
  95. //and we send 1 to the ajax request
  96. echo 1;
  97. }
  98. }else{
  99. //else if it's not bigger then 0, then the code is not in the DB'
  100. //and we send 2 to the ajax request
  101. echo 2;
  102. }
  103. ?>
  104.  
  105. $code = mysqli_real_escape_string($connect, $_POST['code']);
Add Comment
Please, Sign In to add comment