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

标签 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';
        }
      }

    }

更新:

我已经完成了这方面的大部分工作,剩下的就是我必须在按下保存按钮之前进行验证,即在文本框中输入的数量是否小于行/订单引用的数量。

最佳答案

您可以使用 jquery 验证插件来做到这一点。 您需要调用与 ajax 相同的 jquery 验证插件的远程方法。 以便您可以检查用户在文本字段中输入的数量是否小于库存数量。

Jquery validate plugin - remote method

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

关于javascript - 验证带有复选框和文本字段的动态 php 网格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28470474/

相关文章:

javascript - 2 选择选项组合值和链接

php - Twitter 在 Modal 中的 bootstrap Form

jquery - 设置文本框格式并附加

javascript - Uncaught Error : Not an Array - google charts

javascript - 为什么这个 div 不响应innerHTML,而页面上的其他 div 却响应了?

javascript - 如何在 ejs 中显示数据库中的分层数据

php ftp_get() 在 Docker 容器中不起作用

php - 如何获取 url 中的参数,并且是数组? yii2

javascript - 创建自动滑动div的水平列表

javascript - 使用 jQuery 选择组中的多个复选框