Php 如何验证文本上的数据?

Php 如何验证文本上的数据?,php,html,Php,Html,我有一个项目,其中需要验证文本字段上的数据,如果用户输入“0”,则进程不能运行,而是显示错误消息。。这是我的密码: <?php include('connect.php'); include('func.php'); if (isset($_REQUEST['command'])=='delete' && $_REQUEST['pid']>0){ remove_product($_REQUEST['pid']); } else if (isset($_REQU

我有一个项目,其中需要验证文本字段上的数据,如果用户输入“0”,则进程不能运行,而是显示错误消息。。这是我的密码:

<?php
include('connect.php');
include('func.php');
if (isset($_REQUEST['command'])=='delete' && $_REQUEST['pid']>0){
    remove_product($_REQUEST['pid']);
}
else if (isset($_REQUEST['command'])=='update') {
    $max=count($_SESSION['cart']);
    for($i = 0;$i < $max; $i++) {
        $pid = $_SESSION['cart'][$i]['productid'];
        $q = intval($_REQUEST['product'.$pid]);
        if( $q > 0 && $q <= 999){
            $_SESSION['cart'][$i]['qty']=$q;
        }
        else{
             echo "Some products not updated!, quantity must be a number between 1 and 999";
        }
    }
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script language="javascript">
function del(pid){
    if(confirm('Do you really mean to delete this item')){
        document.form1.pid.value=pid;
        document.form1.command.value='delete';
        document.form1.submit();
    }
}
function clear_cart(){
    if(confirm('This will empty your shopping cart, continue?')){
        document.form1.command.value='clear';
        document.form1.submit();
    }
}
function update_cart(){
    document.form1.command.value='update';
    document.form1.submit();
}
</script>
<script type="text/javascript" src="lib/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
    $("#qty").keypress(function (e) {
        if( e.which!=8 && e.which!=0 && (e.which<48 || e.which>57))
        {
            return false;
        }
    });
});
</script>
<script type="text/javascript" src="lib/jquery.js"></script>
<script type="text/javascript" src="src/facebox.js"></script>

<script type="text/javascript">
jQuery(document).ready(function($) {
    $('a[rel*=facebox]').facebox({
        loadingImage : 'src/loading.gif',
        closeImage   : 'src/closelabel.png'
    })
})
</script>

<script type="text/javascript">
function checkCheckBox(){
    agree_check = oForm.elements["agree"].checked;
    if(!agree_check){
        alert("You must agree to the terms and conditions before purchasing.");
    }
    return agree_check;
}
</script>

<style type="text/css">
body {
    background-color: #FFF;
}
table {
    margin-top: 15px;
}
#wrapper {
    width:900px;
    height:auto;
    margin-left:40px;
}
</style>
</head>

<body>
<div id="wrapper">
    <table width="900" align="center">
    <tr>
        <td><img src="images/banner.png" width="900" height="138" /></td>
    </tr>



<form name="form1">
    <input type="hidden" name="pid" />
    <input type="hidden" name="command" />
    <div style="margin:0px auto; width:600px;" >
    <div style="padding-bottom:10px">
        <h1 align="center">Your Shopping Cart</h1>
        <input type="button" value="Continue Shopping" <onclick="window.location='customerproduct.php'" />
    </div>
    <table border="0" cellpadding="5px" cellspacing="1px" style="font-family:Verdana, Geneva, sans-serif; font-size:11px; background-color:#E1E1E1" width="100%">
    <?php
    if(isset($_SESSION['cart'])){
        echo '<tr bgcolor="#FFFFFF" style="font-weight:bold"><td>Serial</td><td>Name</td><td>Price</td><td>Qty</td><td>Amount</td><td>Options</td></tr>';
        $max = count($_SESSION['cart']);
        for($i = 0; $i < $max; $i++){
            $pid = $_SESSION['cart'][$i]['productid'];
            $q = $_SESSION['cart'][$i]['qty'];
            $pname = get_product_name($pid);
            if ($q == 0) continue;
    ?>
        <tr bgcolor="#FFFFFF"><td><?php echo $i+1?></td><td><?php echo $pname?></td>
        <td><?php echo number_format(get_price($pid))?>Php</td>
        <td><input type="text" id="qty" name="product<?php echo $pid?>" value="<?php echo $q?>" maxlength="3" size="2" /></td>                    
        <td><?php echo number_format(get_price($pid)*$q)?>Php</td>
        <td><a href="javascript:del(<?php echo $pid?>)">Remove</a></td></tr>
        <?php } ?>
        <tr>
            <td><b>Order Total: <?php echo number_format(get_order_total())?>Php</b></td>
            <td colspan="5" align="right">
                <input type="button" value="Update Cart" onclick="update_cart()">
                <input type="checkbox" value="0" name="agree">
                <a rel="facebox" href="terms.php"?>terms and condition</a>
                <input type="button" id="place" value="Place Order" onclick="window.location='order.php'" >
            </form></td>
        </tr>
    <?php } ?>
    </table>
    </div>
</form>

函数del(pid){
如果(确认('您真的打算删除此项目')){
document.form1.pid.value=pid;
document.form1.command.value='delete';
文件。表格1。提交();
}
}
函数clear_cart(){
如果(确认('这将清空您的购物车,是否继续?')){
document.form1.command.value='clear';
文件。表格1。提交();
}
}
函数更新_cart(){
document.form1.command.value='update';
文件。表格1。提交();
}
$(文档).ready(函数(){
$(“#数量”).按键(功能(e){
如果(e.which!=8&&e.which!=0&&e.which57))
{
返回false;
}
});
});
jQuery(文档).ready(函数($){
$('a[rel*=facebox]')。facebox({
loadingImage:'src/loading.gif',
closeImage:'src/closelabel.png'
})
})
函数checkCheckBox(){
同意\检查=形式元素[“同意”]已检查;
如果(!同意检查){
警惕(“您必须在购买前同意条款和条件。”);
}
退回支票;
}
身体{
背景色:#FFF;
}
桌子{
边缘顶部:15px;
}
#包装纸{
宽度:900px;
高度:自动;
左边距:40px;
}
你的购物车
Php

使用javascript以这种方式对其进行验证:

document.getElementById("qty").onkeyup=function(){

   if(this.value == "0")
    {
      this.value = "";
      alert("Wrong quantity entered. Please enter again");
    }

};
您也可以使用
onchange
而不是
onkeyup
事件:

document.getElementById("qty").onchange=function(){

   if(this.value == "0")
    {
      this.value = "";
      alert("Wrong quantity entered. Please enter again");
    }

};
替换你的

echo "Some products not updated!, quantity must be a number between 1 and 999";

echo“警报('某些产品未更新!,数量必须介于1和999之间”);
出口

我可以看到,当用户清空购物车时,您一直在使用JavaScript提示用户,如果用户给出的数量值为“0”,您可以使用JavaScript再次警告用户

 <input type="text" id="qty" onblur="if(this.value==0){alert('Please enter a quantity ! ');}"   name="product<?php echo $pid?>" value="<?php echo $q?>" maxlength="3" size="2" />

如果($q>0&&$q)不确定这是否是你的问题,但你的代码有一些问题。1)你包含
lib/jquery.js两次-只需要一次。2) “您的购物车”后面的输入有一个
它不工作。。当我尝试输入0值并单击按钮时,什么也没有发生..:(你把这个javascript@BonCorcino放在哪里了???你需要把它放在结尾的
标记之前,在这一行显示错误未定义变量err好的,需要将$err=''移动到php块内页面的顶部。
 <input type="text" id="qty" onblur="if(this.value==0){alert('Please enter a quantity ! ');}"   name="product<?php echo $pid?>" value="<?php echo $q?>" maxlength="3" size="2" />
    $err = '';
 if($q>0 && $q<=999){
        $_SESSION['cart'][$i]['qty']=$q;

    }
    else{
         $err = "Some products not updated!, quantity must be a number between 1 and 999";
    }
<td><input type="text" id="qty" name="product<?php echo $pid?>" value="<?php echo $q?>" maxlength="3" size="2" /><?php echo $err; ?></td>