-2
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Order</title>

以下は、選択されたチェックボックスに基づいて合計を計算し、合計を表示する確認ボックスを提供します。この合計が 0 と表示されても。

        <script type="text/javascript">
         function validate()
           {
              var x,y,i,j,p=0;
        j=parseInt(document.getElementById('loopcount').value);
              document.write(j);
        for (i=1;i<=j;i++)
        {
            document.write(i);
               if(document.getElementsByName('itemCode['+i+']').checked==true){
                x=parseFloat( document.getElementsByName('quantity['+i+']').value);
                document.write(x);
                y=parseFloat( document.getElementsByName('price['+i+']').value);
                document.write(y);
        p+=x*y;
                }
        }

    var conf=confirm("The total amount is:"+ p);

              if(conf==true){
                  header("Location:userHome.php");
              }
              else{
                  header("Location:orderveggies.php");
              }

        }


    </script>
    </head>

このコードは、mysql からのデータをチェックボックスとして表示します。私の問題は、配列を取る入力タグの命名にあります。

    <body>
        <form name="myForm" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" onsubmit="validate()">
        <table align="center" border="0">
            <tr>

                <th>item Code</th>
                <th>Vegetable</th>
                <th>Price/KG</th>
                <th>Quantity</th>
            </tr>


             <?php

             $db=  mysqli_connect('localhost','root','','ourveggies')
                            or die("Error!Could not connect to the database");
                    $query="select itemCode,itemName,price from veggies";
                    $result=mysqli_query($db,$query)
                            or die("Error in query".mysqli_error($db));
                    $count=mysqli_num_rows($result);
                    while($row= mysqli_fetch_array($result)){?>
                        <tr>
                        <td><input type="checkbox" name="itemCode[]" value="<?php echo $row['itemCode']; ?>"/><?php echo $row['itemCode']; ?></td>
                        <td><?php echo $row['itemName']; ?></td>
                        <td><input type="hidden"  name="price[]" value="<?php echo $row['price']; ?>"/><?php echo $row['price']; ?></td>
                        <td><select name="quantity[]">
                                <option value="0.00">--Quantity--</option>
                                <option value="0.100">100 gm</option>
                                <option value="0.250">250 gm</option>
                                <option value="0.500">500 gm</option>
                                <option value="1">1 Kg</option>
                                <option value="2">2 Kg</option>
                                <option value="3">3 Kg</option>
                                <option value="4">4 Kg</option>
                                <option value="5">5 Kg</option>

                            </select></td>
                        </tr>

                  <?php  }?>
                    <input type="hidden" id="loopcount" name="loopcount" value="<?php echo $count; ?>"/>

            <tr>
                <td><input type="submit" name="order" value="order" /></td>
            </tr>
        </table>
        </form>
    </body>
</html>
4

3 に答える 3

0
name="itemCode[]"

これは name="itemCode[]" を意味し、name="itemCode[何でも]" ではありません

これを試して:

<td><input 
        type="checkbox" 
        name="itemCode[<?php echo $row['itemCode']; ?>]"
        etc etc
</td>

getElementsByName も配列を返します

または (名前の代わりに ID を使用):

<td><input 
        type="checkbox" 
        id="itemCode[<?php echo $row['itemCode']; ?>]"
        etc etc
</td>

次にJavaScriptで:

document.getElementById('itemCode['+i+']')

それでも理解に苦しむ場合は、ブラウザーで firebug などを使用して、出力された html を調べてください。すべての入力に同じ名前を付けています

于 2012-10-15T14:44:44.247 に答える
0

document.getElementsByName() は、単一の要素ではなく、配列を返します。これを試してください: document.getElementsByName('itemCode[]')[i].checked. ループを 0 で開始します。

for (i=0;i<j;i++)
{
    document.write(i);
    if(document.getElementsByName('itemCode[]')[i].checked==true){
        x=parseFloat( document.getElementsByName('quantity[]')[i].value);
        document.write(x);
        y=parseFloat( document.getElementsByName('price[]')[i].value);
        document.write(y);
        p+=x*y;
    }
}
于 2012-10-15T14:45:01.373 に答える
0

私の答えは少し長くなってきたので、ここにあなたの実際の問題の詳細を示します: コード行の横にコメントを入れましたが、それはあなたの混乱を引き起こしていると思います.

    <script type="text/javascript">
     function validate()
       {
          var x,y,i,j,p=0;
          j=parseInt(document.getElementById('loopcount').value);
          document.write('j='+j);    // make your debugging messages more meaningful -S
    for (i=1;i<=j;i++)
    {
           document.write('i='+i);      // make your debugging messages more meaningful -S
           if(document.getElementsByName('itemCode['+i+']').checked==true)
           {
                //getElementsByName returns an array: replace them with getElementByName -S
                x=parseFloat( document.getElementByName('quantity['+i+']').value);
                document.write('x='+x);   // make your debugging messages more meaningful  
                y=parseFloat( document.getElementByName('price['+i+']').value);
                document.write('y='+y);   // make your debugging messages more meaningful
                p+=x*y;
            }
    }

var conf=confirm("The total amount is:"+ p);

          if(conf==true){
              header("Location:userHome.php");
          }
          else{
              header("Location:orderveggies.php");
          }

    }


</script>
</head>

次に、PHP の場合:

blah blah blah
while($row= mysqli_fetch_array($result)){?>
    <tr>
    <td>
         <input 
           type="checkbox" 
           name="itemCode[<?php echo $row['itemCode']; ?>]"  //Do this. seriously otherwise everything will have the same name -S
           value="<?php echo $row['itemCode']; ?>"/>
         <?php echo $row['itemCode']; ?>
    </td>
    <td>
         <?php echo $row['itemName']; ?>
    </td>
    <td>
         <input 
              type="hidden"  
              name="price[<?php echo $row['itemCode']; ?>]"  //Im assuming here you want to know what price belongs to what item -S
             value="<?php echo $row['price']; ?>"/>
         <?php echo $row['price']; ?>
    </td>

    etc etc

あなたのコードにはたくさんのバグがあります。私が指摘した問題を取り除くためにコードを更新してください。

于 2012-10-16T11:56:03.343 に答える