邮编code验证 [英] Zip Code Validation
本文介绍了邮编code验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要完成以下 1.如果拉链code是present在我的数据库,然后打印资格交付 2.否则不符合交付
I want to accomplish the following 1. If the zip code is present in my database then print eligible for delivery 2. Else Not eligible for delivery
我写了code如下: HTML
I wrote code as below: html
<form>
Pincode: <input autocomplete="off" type="text" maxlength="6" name="pincode" class="pincode" id="pin_code" placeholder="Enter Pincode" onkeypress='validate(event)'/><br>
<span class="check" ></span> <br/><br/>
JavaScript和Ajax
javascript and ajax
<script type="text/javascript">
$(function()
{
$('.pincode').keyup(function()
{
var checkcode=$(this).val();
if(checkcode){
$('.check').show();
$('.check').fadeIn(400).html('<img src="image/ajax-loading.gif" /> ');
var Str="pin=" + checkcode;
$.ajax({
type: "POST",
url: "available.php",
data: Str,
cache: false,
success: function(result){
if(result==''){
$('.check').html('<img src="image/error.png" /> This pincode is not valid');
$(".check").removeClass("red");
$('.check').addClass("green");
$(".pincode").removeClass("yellow");
$(".pincode").addClass("white");
}else{
$('.check').html('<img src="image/accept.png" /> This pincode is valid');
$(".check").removeClass("green");
$('.check').addClass("red")
$(".pincode").removeClass("white");
$(".pincode").addClass("yellow");
}
}
});
}else{
$('.check').html('');
}
});
});
</script>
PHP $ C $下available.php
php code for available.php
<?php
$mysql_db_hostname = "localhost";
$mysql_db_user = "healthmate";
$mysql_db_password = "healthmate";
$mysql_db_database = "test";
$con = mysql_connect($mysql_db_hostname, $mysql_db_user, $mysql_db_password) or die("Could not connect database");
mysql_select_db($mysql_db_database, $con) or die("Could not select database");
if(isset($_POST['pincode']) && !empty($_POST['pincode'])){
$query="select * from cod_locations where pin=$pincode";
$res=mysql_query($query);
$count=mysql_num_rows($res);
$HTML='';
if($count > 0){
$HTML='delivery available';
}else{
$HTML='not there';
}
echo $HTML;
}
?>
问题:即使是有效的code在数据库中显示为无效。 任何帮助将AP preciated。我想问题在于结果阿贾克斯的部分。
Problem : Even the valid code in the database is shown as invalid. Any help will be appreciated. I guess the problem lies with result in ajax part.
推荐答案
有一些问题在这里:
- 您将它设置为发送到你的PHP脚本:
VAR海峡=引脚=+检查code;
。因此,在PHP端你需要$ _ POST ['针']
; - 您总是呼应的东西出来的时候销code设置(你的
$ HTML
变量),因此结果
在你的成功函数变量将永远是空的; - 您有一个SQL注入的问题。您应该切换到PDO或mysqli的和prepared声明。
- You are setting this to be sent to your php script:
var Str="pin=" + checkcode;
. So on the php side you would need$_POST['pin']
; - You are always echoing something out when a pincode is set (your
$HTML
variable), so theresult
variable in your success function will never be empty; - You have an sql injection problem. You should switch to PDO or mysqli and prepared statements.
这篇关于邮编code验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文