带有复选框和文本字段的动态php网格的验证

时间:2015-02-12 06:07:28

标签: javascript php jquery mysql validation

enter image description here

我有从数据库中显示的以下数据,这是不行的。

<table class="table-style-one" width="90%" border="0" cellspacing="1" cellpadding="1" style="margin:0 auto;">

              <thead style="color:black;">
                <th style="text-align:center;">S.N</th>
                <th style="text-align:center;">PO REFERENCE</th>
                <th style="text-align:center;"><strong>ITEM NAME</strong></th>
                <th style="text-align:center;"><strong>UNIT</strong></th>
                <th style="text-align:center;"><strong>QUANTITY IN STOCK</strong></th>
                <th style="text-align:center;"><strong>FROM THIS</strong></th>
                <th style="text-align:center;"><strong>THIS QUANTITY</strong></th>
              </thead>
              <tbody>
                <?php
                $i=1;
        while($rs_rows=mysql_fetch_array($result_of_SQL_GET_POREF_FOR_ITEM))
                {
                  ?>
                  <tr <?=$bg?>>
                    <td align="center" style="padding-top:5px;">
                      <?=$i?>
                    </td>
                    <td width="54%" align="center" style="padding-top:5px;"> <font color="#000000"><?=$rs_rows['po_reference_name'];?>
                    </font> </td>
                    <td width="54%" align="center" style="padding-top:5px;"> <font color="#000000"><?=$rs_rows['item_name'];?>
                    </font> </td>
                    <td width="54%" align="center" style="padding-top:5px;"> <font color="#000000"><?=$rs_rows['unit_name'];?>
                    </font> </td>
                    <td width="54%" align="center" style="padding-top:5px;"> <font color="#000000"><?=$rs_rows['Total_Quantity'];?></font></td>
                    <td width="54%" align="center" style="padding-top:5px;"> <font color="#000000">
                      <input type="checkbox" name="check_list[]" value="<?=$rs_rows['po_refrence_id'];?>">
                    </font></td>
                    <td width="54%" align="center" style="padding-top:5px;">
                      <font color="#000000"><input type="text" name="txt[<?=$rs_rows['po_refrence_id'];?>]"></font>
                    </td>
                  </tr>
                  <?php
                  $i++;
                }
                ?>
                <tr class="text">
                  <td colspan="7"><div align="center">
                    <p>
                      <input type="hidden" name="req" value="<?=$req?>">
                      <input type="submit" name="save" value="Save" class="submit" tabindex="3"  >
                      <input type="reset" name="Reset" value="Cancel" class="submit" tabindex="4">
                      <?php
                      if(isset($_GET[msg])) echo "<font color=red>".$_GET[msg]."</font>";
                      // if(isset($messsag)) echo $messsag;

                      ?>
                    </p>
                  </div></td>
                </tr>
              </tbody></table>

这是用户向用户发出数量的表单,假设请求数量为10,那么管理员可以从一个po参考发出这10个项目,或者从po-laptop-hp-1发出一些数量一些来自po-dell-hp-1。我想在提交之前验证它应验证用户在文本字段中为每行输入的数量是否小于库存数量。所有文本字段数量的总和不超过请求数量。而且......

我已经编写了这段代码,但没有像我预期的那样工作,

if(isset($_POST['save']))
{
  if ($_GET[add]==0)
  {
    if($_GET['req']=="") $abc= $_POST['req'];
    if($_POST['req']=="") $abc= $_GET['req'];
    echo $abc;
    $quantity_issued = $qty;
    foreach($_POST['check_list'] as $selected){
      foreach ( $_POST['txt'] as $key => $value )
      {
        echo 'Textbox #'.htmlentities($key).' has this value: ';
        echo htmlentities($value);
      }
      $SQL_Query_Get_Item_Quantity = mysql_query("SELECT * FROM stock WHERE po_refrence_id = '".$selected."'");
      if($SQL_Query_Get_Item_Quantity === FALSE){
        die(mysql_error());
      }
      else{
        $result_SQL_Query_Get_Item_Quantity = mysql_fetch_assoc($SQL_Query_Get_Item_Quantity);
        if($quantity_issued > $result_SQL_Query_Get_Item_Quantity['quantity'] && $result_SQL_Query_Get_Item_Quantity['quantity'] != 0){
          echo 'quantiy you want to issued is more than the quantity that this po refrence holds';
        }
        else{
          //echo $result_SQL_Query_Get_Item_Quantity['quantity']."</br>";
          echo 'quantiy you want to issued is less than the quantity that this po refrence holds';
        }
      }

    }

更新:

我已经完成了大部分工作,其余的是我必须在按下保存按钮之前验证,是否在文本框中输入的数量小于行/ po参考的数量。

1 个答案:

答案 0 :(得分:0)

您可以使用jquery validate plugin来完成此操作。 你需要调用与ajax相同的jquery validate插件的远程方法。 这样您就可以检查用户在文本字段中输入的数量是否小于库存数量。

Jquery validate plugin - remote method

 $( "#myform" ).validate({
  rules: {
    email: {
      required: true,
      email: true,
      remote: "check-email.php"
    }
  }
});