php - 验证 MySql 表中的促销代码并在提交表单时标记为 "used"

标签 php jquery html mysql

我有一个以输入字段开头的 HTML 表单,用户可以选择编写促销代码以获得一些折扣......

我想在这里做什么。我需要创建一个 keyup 函数来检查是否在 MySql Promo Codes 表中找到键入的代码。

如果找到,请在占位符中写一些内容......,否则,写一些其他内容......

此外,如果提交表单需要 PHP 在 MySql Used 列对应的代码中写入“Yes”...

<form id="form" class="form" name="RevitForm" action="form_revit_architecture_submitted" method="post" enctype="application/x-www-form-urlencoded" accept-charset="UTF-8">

<div class="field" style="background-color:#f3f3f3;">
<span id="promo-msg" style="color:#093; position:relative; bottom:3px; font-style:italic; font-size:13px">[HTML is replaced when successful.]</span>
<center><input style="font-family:Lato; text-align:center; max-width:200px;" type="text" id="PromoCode" name="PromoCode" maxlength="5" size="15px" placeholder="Promo Code"></center>
</div>

//other input fields

</form>

<!-- Promotion Code Match -->
<script>
$("#PromoCode").keyup(function() {
if ($(this).val().length == 5) {

//post the code and check the it in the MySql table thru the PHP file "request.php"
//if code found {write something in $(#promo-msg) } else {do something else} 

}           
});
</script>

在 PHP 中需要执行类似的事情

<?PHP
$code = ucwords($_POST['PromoCode']);


$con=mysqli_connect("localhost","x","y","academy_database");
                // Check connection
                if (mysqli_connect_errno())
                {
                echo "Failed to connect to MySQL: " . mysqli_connect_error();
                }

$db_code = mysqli_query($con," SELECT * FROM `Promo Codes` WHERE (`Code` LIKE '".$code."') AND (`Used` <> 'Yes') ");                    


// if $code is found and the corresponding `Used` column does not == 'Yes' return as found
//else return as not found

?>

最佳答案

为此,我们需要 2 个文件。

  1. HTML、表单 + jQuery AJAX keyup 事件并检查 DB
  2. PHP 连接数据库以检查促销代码

1.HTML

<html>
<head>
<title>Promo check</title>
<!-- load jQuery  library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> 

<script>
    $(document).ready(function() {  

        //the min chars for promo-code
        var min_chars = 10;  

        //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 + ' is already used correct.');  
            }else{
                //show that the code is not correct 
                $('#Promo_code_status').html(code + ' is not correct.');  
            }
        });  
    } 
</script>
</head>

<body>

    <input type='text' id='code'>
    <div id='Promo_code_status'></div>  

</body>
</html> 

2.PHP:check_code.php

您将需要使用连接数据($host、$user、$pass、$dbdb),并且可能需要更改表和字段名称。

<?php

//connect to database  
$user = "";
$pass = "";
$host = "";
$dbdb = "";

$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 promoCode, used from testtable where promoCode = "'. $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;  
}  
?>

关于php - 验证 MySql 表中的促销代码并在提交表单时标记为 "used",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31022783/

相关文章:

javascript - 更改全日历的 View 不保留事件

javascript - Vue.js 与 Laravel 5.3

jquery - 标题高度的计算没有调整大小不按预期工作

php - php 中的登录状态更新

PHP用外键删除记录后,检查int表

javascript - 带有用户 ID 的 Google Analytics 跟踪代码

javascript - HTML5 + JS 极坐标散点图

javascript - JavaScript 在这里实际上做了什么

html - 为什么我的 CSS 不能在我的 LAMP 服务器上运行?

php - html/php 中的多色 TD?