0

我有这个项目,如果用户输入“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>

我喜欢在用户在quantity.

4

3 回答 3

0

使用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");
    }

};
于 2013-10-18T03:25:40.867 回答
0

更换你的

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

echo "<script>alert('Some products not updated!, quantity must be a number between 1 and 999')";
exit;
于 2013-10-18T03:28:35.653 回答
0

我可以看到您一直在使用 JavaScript 在用户清除购物车时提示用户,如果用户给出 qty 值“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" />

我还可以看到您尝试在 PHP 中验证请求。您可以将其存储到变量中,而不是回显错误:

    $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";
    }

然后你选择一个地方在 HTML 中显示消息

<td><input type="text" id="qty" name="product<?php echo $pid?>" value="<?php echo $q?>" maxlength="3" size="2" /><?php echo $err; ?></td>                    
于 2013-10-18T03:29:43.093 回答