Php 检查网站上MYSQL表中的促销代码
Php 检查网站上MYSQL表中的促销代码,php,html,mysql,Php,Html,Mysql,我正试图在我的HTML网站上创建一个系统,在那里你可以输入一个代码,然后通过php文件进行检查php应将代码与my MYSQL表中的代码进行比较。php还应检查代码是否已准备就绪检查完成后,网站上将显示一条消息,告知用户其代码是否有效。 如果代码有效,它将以某种方式通知我有人输入了代码。 我已经编写了一些代码,但当我按下按钮时,它根本不起作用。这里是我的HTML代码: <!DOCTYPE html> <html> <head> <script src="
我正试图在我的HTML网站上创建一个系统,在那里你可以输入一个代码,然后通过php文件进行检查
php应将代码与my MYSQL表中的代码进行比较。php还应检查代码是否已准备就绪
检查完成后,网站上将显示一条消息,告知用户其代码是否有效。
如果代码有效,它将以某种方式通知我有人输入了代码。
我已经编写了一些代码,但当我按下按钮时,它根本不起作用。
这里是我的HTML代码:
<!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>
$(文档).ready(函数(){
//促销代码的最小字符数
var min_chars=6;
//结果文本
var checking_html='checking…';
//当钥匙打开时
$('#code').keyup(函数(事件){
//运行字符号检查
if($('#code').val().length==min_chars){
//显示checking_文本并运行函数进行检查
$('Promo#u code_status').html(检查html);
检查_代码();
}
});
});
//函数检查促销代码
函数检查\u代码(){
//获取代码
var code=$('#code').val();
//使用ajax运行检查
$.post(“check_code.php”,{code:code},
函数(结果){
//如果结果为0
如果(结果==0){
//显示代码是正确的
$('Promo#u code_status').html(code+'是正确的');
}如果(结果==1){
//显示代码正确,但已被使用
$(“#促销代码"状态”).html(代码+”已被使用);
}否则{
//显示代码不正确
$(“#促销代码_状态”).html(代码+”不正确。“);
}
});
}
输入促销代码
这里是我的PHP文件:
<?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中分配post值,这就是原因
$code = mysqli_real_escape_string($connect, $_POST['code']);
确保可以访问您正在使用的jquery CDN。因为它看起来像是错误的CDN
在checkcode()
函数中,使用#Promo\u code\u status
,P
为大写,而在html中使用Promo\u code\u status
和P
为小写
在php文件中,您忘记为$code
我希望这会有所帮助这会给您带来任何错误吗?如果是,你能发布吗?你的ajax代码在哪里?@M不,我没有收到错误谢谢你的帮助,现在我至少得到了“Checking..”输出,但之后什么也没有发生。你在checkcode()
函数中更改了促销代码的状态了吗?