这是个餐馆网站,菜单可以选点的菜的数量,然后放进购物车里面。while(...){
echo "<form name='shopping_cart_form' method='post' action='include/shopping_cart_form.php' onsubmit='return dosubmit();'>";
echo "<div id='item_details' style=''>
<div id='item_label'>
<div id='dish_price'>
<a style='font-size:30pt; margin-top:20px; float:left;'>\$</a>
<a style='font-size:50pt; margin-top:40px; float:left;'>$prices[0]</a>
<a style='font-size:10pt; margin-top:40px; float:left;'>.</a>
<a style='font-size:20pt; margin-top:30px; float:left;'>$prices[1]</a>
</div>
<div id='dish_img'><img src='css_and_img/$dish_img' style='width: 320px; height:160px; border:none' border=none></div>
</div>
<div id='item_info'>
<div id='dish_name'>$dish_name</div>
<div id='dish_descr'>Description: $dish_descr</div>
<div id='add_to_function'>&nbsp;&nbsp;Qty:
<input class='txt_qty' type='text' name='textbox_qty' id='textbox_qty' />
<input class='add_button' type='submit' name='btn_add' id='btn_add' value='Add to Cart' />

<input type='hidden' name='shopID' value='$shop_id'>
<input type='hidden' name='destID' value='$dest_id'>
<input type='hidden' name='catID' value='$cat_id'>
<input type='hidden' name='dishID' value='$dish_id'>
<input type='hidden' name='dishName' value='$dish_name'>
<input type='hidden' name='dishPrice' value='$dish_price'>
</div>

</div>
</div>";
echo "</br>";
echo "</form>";}
之前发的帖子问了,但是我问错了,form是在循环里面,所以不能用getElementById来判断。在此感谢一下T5500对我帮助。
每个form里面只有一个input type=text  现在就是在每个form提交之前,怎么判断每个text不能为空或者为0 ??

解决方案 »

  1.   

    onsubmit='return dosubmit();'
    改为
    onsubmit='return dosubmit(this);'js
    function dosubmit(fmt) {
      if(fmt.textbox_qty.value == '') {
        fmt.textbox_qty.focus();
        alert('不能为空');
        return false;
      }
      return true;
    }
      

  2.   

    大神来了,我很开心。
    顺便再帮个忙呗?
    有是一个form 里面有两个radio,第一个radio name是动态的,第二个是固定的,怎么判断有没checked?
    这个问题我在另一个帖子里面问了(http://topic.csdn.net/u/20120502/18/856f0548-70ee-4142-b691-a333544da194.html?seed=863989089&r=78436396#r_78436396),但是没能解决,T5500给的答复在html下可以用,在php里面失效,是在不明白为什么啊
    拜托了!<!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>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <link rel="stylesheet" type="text/css" href="css_and_img/user_header.css" />
    <link rel="stylesheet" type="text/css" href="css_and_img/user_main.css" />
    <title>Your Cart</title>
    </head>
    <?php
    include_once 'lib/functions.php';
    ini_set('display_errors', 'Off');
    $db = new Connection();
    $connection = $db->connect();
    session_start();
    ?><body>
    <div id="main">
        <?php
        if(!isset($_SESSION['user']))
        {
            header("Location:login.php");
        }
        else
        {    
            echo "<table border='0' style='width:90%;color:#6B6B6B; margin:auto'>";
            echo "<form name='submit_form' method='post' action='order_confirmation.php' onsubmit='return isEmpty();'>";
                                                    
            echo "<input type='radio' name='2' value='1130'>11:30";
              //这里的radio实际上是个循环,在while{}里面,
              //源代码是:
              //while(...){echo "<input type='radio' name='$dest_id' value='$pick_time'>time";}  
            echo "</table>";
            echo "<table border='0' style='width:90%;color:#6B6B6B; margin:auto'>";            echo "<tr><td style='font-size:11.5pt'><input type='radio' name='payment_methods' value='credit' disabled='disabled'>Pay by Whim Win Credit - <a style='color:#d34836' href='recharge.php'>Recharge</a> will receive <a style='color:red'>5% discount off.</a></td></tr>";
     
            echo "<tr><td style='font-size:11.5pt'><input type='radio' name='payment_methods' value='cr_and_card'>\$$credit Whim Win Cr+\$$difference by Credit / Debit Card / Paypal";
            echo " <a href='#' onclick='javascript:window.open('https://www.paypal.com/au/cgi-bin/webscr?cmd=xpt/Marketing/popup/OLCWhatIsPayPal-outside','olcwhatispaypal','toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, width=400, height=350');'><img  src='https://www.paypalobjects.com/en_AU/i/bnr/horizontal_solution_PP.gif' border='0' alt='Solution Graphics'></a>";
            echo "</td></tr>";
            echo "<tr><td style='font-size:11.5pt'><input type='radio' name='payment_methods' value='card'>Credit / Debit Card / Paypal";
            echo " <a href='#' onclick='javascript:window.open('https://www.paypal.com/au/cgi-bin/webscr?cmd=xpt/Marketing/popup/OLCWhatIsPayPal-outside','olcwhatispaypal','toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, width=400, height=350');'><img  src='https://www.paypalobjects.com/en_AU/i/bnr/horizontal_solution_PP.gif' border='0' alt='Solution Graphics'></a>";
                    
            echo "</td></tr>";
            
            echo "</table>";
            
            echo "<p></p>";
            echo "<p style='width:90%;color:#6B6B6B; margin:auto; text-align:center'><input id='btn_continue' style='' name='continue_order' type='submit' value='Continue Shopping'><input id='btn_confirm' style='' name='go_next' type='submit' value='Confirm'><input name='hidden_credit' type='hidden' value='$credit'></p>";
            echo "<p></p>";
            
        echo "</form>";
        }
                
        ?>
        </div>
        
        <div class="step_num"><b style="color:#d34836">FOLLOWING STEPS</b></div></br><script type="text/javascript">function isEmpty()
    {
        function isEmpty() {
        var obj = document.getElementsByTagName('form')[0].getElementsByTagName('input');
        
        var FLAG = {
            pm: false,  //name = "payment_methods"
            otherRadio: false
        }
        
        for (var i = 0; i < obj.length; i ++) {
            if (obj[i].type == 'radio') {
                if (obj[i].name == 'payment_methods') {
                    if (obj[i].checked == true) FLAG.pm = true;
                }
                else {
                    if (obj[i].checked == true) FLAG.otherRadio = true;
                }
            }
        }
        
        if (!FLAG.pm || !FLAG.otherRadio) {
            alert('Please choose your pick up time and payment method.');
            return false;
        }
        else return true;
    }</script> </body>
    </html>
      

  3.   

    function isEmpty()
    {
        function isEmpty() {
    这里看到函数命名两次。是手误还是什么?