Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <head>
- <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11/jquery.min.js">
- </script>
- <script>
- $(document).ready(function() {
- //the min chars for promo-code
- var min_chars = 6;
- //result texts
- var checking_html = 'Checking...';
- //when keyup
- $('#code').keyup(function(event){
- //run the character number check
- if($('#code').val().length == min_chars){
- //show the checking_text and run the function to check
- $('#Promo_code_status').html(checking_html);
- check_code();
- }
- });
- });
- //function to check the promo code
- function check_code(){
- //get code
- var code = $('#code').val();
- //use ajax to run the check
- $.post("check_code.php", { code: code },
- function(result){
- //if the result is 0
- if(result == 0){
- //show that the code is correct
- $('#Promo_code_status').html(code + ' is correct.');
- }else if(result == 1){
- //show that the code is correct, but already has been used
- $('#Promo_code_status').html(code + ' has allready been used.');
- }else{
- //show that the code is not correct
- $('#Promo_code_status').html(code + ' is not correct.');
- }
- });
- }
- </script>
- </head>
- <body>
- <center><h1>Enter Promo Code</h1>
- <form method="post" action="check_code.php">
- <input type="text" id="code" name="code" maxlength="6" />
- <div id="promo_code_status"></div>
- <br>
- <input type="submit" value="Let's go"></center>
- </form>
- </body>
- </html>
- <?php
- //connect to database
- $user = "***"; //Username
- $pass = "***"; //Password
- $host = "localhost"; //Host
- $dbdb = "***"; //Database name
- $connect = mysqli_connect($host, $user, $pass, $dbdb);
- if(!$connect)
- {
- trigger_error('Error connection to database: '.mysqli_connect_error());
- }
- //get the code
- mysqli_real_escape_string($connect, $_POST['code']);
- //mysql query to select field code if it's equal to the code that we checked '
- $result = mysqli_query($connect, 'select Code from Codes where Code = "'. $code .'"');
- $record = mysqli_fetch_array($result);
- //if number of rows fields is bigger them 0 that means the code in the database'
- if(mysqli_num_rows($result) > 0){
- if($record['used'] == 0) {
- //and we send 0 to the ajax request
- echo 0;
- } else{
- //and we send 1 to the ajax request
- echo 1;
- }
- }else{
- //else if it's not bigger then 0, then the code is not in the DB'
- //and we send 2 to the ajax request
- echo 2;
- }
- ?>
- $code = mysqli_real_escape_string($connect, $_POST['code']);
Add Comment
Please, Sign In to add comment