现在有三个表:库存表
物料编号,物料名称,型号,使用机型,备注入库表
入库编号,物料编号,入库日期,入库数量,入库单价,入库类别,设备编码,备注出库表
出库编号,入库编号,职工编号,出库日期,出库数量我想在添加出库信息的时候,出库数量要和剩余的数量做比较,如果出库数量>剩余数量,就弹出对话框提示下。
这个应该怎么实现啊?
<form id="form1" name="form1" method="post" enctype="multipart/form-data" action=""<?=$_SERVER['../PHP_SELF'];?> onsubmit="checked();">
  <table width="600" border="0" align="center" bgcolor="#CCCCCC">
    <caption class="title">
      出库表
    </caption>
     <tr>
      <td width="150" height="30" align="right" bgcolor="#FFFFFF">入库编号:</td>
      <td height="30" align="left" bgcolor="#FFFFFF"><input name="txtID" type="text" id="txtID" /></td>
    </tr>
    <tr>
      <td width="150" height="30" align="right" bgcolor="#FFFFFF">出库日期:</td>
      <td height="30" bgcolor="#FFFFFF"><select name="sltYear" id="sltYear">
          <?php sltDate(2006,2020);
?>
        </select>
        年
        <select name="sltMonth" id="sltMonth">
          <?php sltDate(1,12);
?>
        </select>
        月
        <select name="sltDay" id="sltDay">
          <?php sltDate(1,31);
?>
        </select>
        日<span class="xinghao">*</span></td>
    </tr>    <tr>
      <td width="150" height="30" align="right" bgcolor="#FFFFFF">数量:</td>
  <?php 
  $matid= $_POST['txtID'];
  if($matid!=''){
   $sqlNUM = "select count(MIS_qty)-count(MO_qty) from Material m, MaterialInStorage mis, MaterialOutstock mo where m.Mat_id=mis.Mat_id  and mis.MIS_id=mo.MIS_id and m.Mat_id='$matid'";
   $num = mysql_query($sqlNUM);
  }
  ?>
 <script>
 function checkNum(){
  val num1 = document.getElementById("txtQty").value;
val num2 = document.getElementById("hiddenQty").value;
if(num1>num2){
alert("没有足够的数量!");
window.focus();
}
 }
 </script> 
      <td height="30" align="left" bgcolor="#FFFFFF"><input name="txtQty" type="text" id="txtQty" size="10" onblur="checkNum();" />
          <span class="xinghao">*
          <input name="hiddenQty" type="hidden" id="hiddenQty" value="<?=$num?>" />
          </span></td>
    </tr>
    <tr>
      <td height="30" align="right" bgcolor="#FFFFFF">领用人工号:</td>
      <td height="30" align="left" bgcolor="#FFFFFF"><input name="txtStfID" type="text" id="txtStfID" size="10" />
          <span class="xinghao">*</span>        <input name="btnSearchName" type="button" id="btnSearchName" value="查询员工号" onclick="javascript:window.open('<?=$rootpath?>/ITOfficeSupplies/In&amp;Out/StfName.php','查询员工工号','directories=no,width=450,height=350');" /></td>
    </tr>    <tr>
      <td height="30" colspan="2" align="center" bgcolor="#FFFFFF"><input type="submit" name="Submit" value="出库" /></td>
    </tr>
  </table>
</form>

解决方案 »

  1.   

    楼主。你这样出来时就读出来。你没考虑多个人操作? 。你是好是提交后再做个判断吧。或是ajax判断。
      

  2.   

              $sqlNUM = "select (sum(MIS_qty)-sum(MO_qty)) as total from Material m, MaterialInStorage mis, MaterialOutstock mo where m.Mat_id=mis.Mat_id  and mis.MIS_id=mo.MIS_id and m.Mat_id='$matid'";
              $res = mysql_query($sqlNUM);
              $total = mysql_fetch_array($res);
              $num = $total['total']; 
      

  3.   

    没考虑的话。。那你php 部分就2楼那样写。
    js那块这么写就行了。不用hidden一个值了
       <script>
         function checkNum(){
            if(document.getElementById("txtQty").value > <?php echo $num;?>){
                alert("没有足够的数量!");
            }
         }
         </script> 
      

  4.   

    那一段代码已经改成这样了,光标移出后没反应啊,哪里错了嘛?
        <tr>
          <td width="150" height="30" align="right" bgcolor="#FFFFFF">数量:</td>
      <?php 
      $matid= $_POST['txtID'];
      if($matid!=''){
       $sqlNUM = "select (sum(MIS_qty)-sum(MO_qty)) as total from Material m, MaterialInStorage mis, MaterialOutstock mo where m.Mat_id=mis.Mat_id  and mis.MIS_id=mo.MIS_id and m.Mat_id='$matid'";
    $res = mysql_query($sqlNUM);
            $total = mysql_fetch_array($res);
            $num = $total['total']; 
      }
      ?>
     <script>
      function checkNum(){
      if(document.getElementById("txtQty").value > <?php echo $num;?>){
       alert("没有足够的数量!");
      }
      }
     </script> 
          <td height="30" align="left" bgcolor="#FFFFFF"><input name="txtQty" type="text" id="txtQty" size="10" onblur="checkNum();" />
              <span class="xinghao">*
              </span></td>
        </tr>
      

  5.   

    1.
    $num = $total['total']; 
    之后echo $num看下值是多少。
    2.
    光标移出后没反应?onclur不止是移出光标,是点击到别处,只是移动鼠标的话不响应onblur事件的吧?
      

  6.   

    帮你测试了,JS代码没问题,看看SQL查询那段吧
      

  7.   

    右击 查看网页源代码 。 复制把它贴出来啊。这样就知道是哪里错了。
    我想是没拿到$sum
      

  8.   

    好像是没得到
    首先是$matid的值
    应该怎样得到它的值呢?
    然后才能得到$numd的值
      

  9.   

    断点测试:
       
       <?php 
        if(isset($_POST['txtID'])){
          $matid= $_POST['txtID'];
          echo $matid;//看这里是否正常
          if($matid!=''){
              $sqlNUM = "select (sum(MIS_qty)-sum(MO_qty)) as total from Material m, MaterialInStorage mis, MaterialOutstock mo where m.Mat_id=mis.Mat_id  and mis.MIS_id=mo.MIS_id and m.Mat_id='$matid'";
            echo $sqlNUM;//看sql语句是否正确。
            $res = mysql_query($sqlNUM);
            $total = mysql_fetch_array($res);
            $num = $total['total']; 
          }
    }
          ?>
      

  10.   


    刚看到你的form的action是action=""<?=$_SERVER['../PHP_SELF'];?>这是什么东东???改为action="<?=$_SERVER['PHP_SELF'];?>"或者action=""
      

  11.   


    必须提交表单才会有 $_POST[]变量,你提交表单了么?
      

  12.   

    提交后$matid是能得到,可$num没有能不能不提交就得到这些数据?有什么办法吗?
      

  13.   


    $num 没有取到是因为sql语句错了,echo $sql;看是什么结果。
      

  14.   

    改好了$sqlNUM = "select (sum(MIS_qty)-sum(MO_qty)) as total from Material m, MaterialInStorage mis, MaterialOutstock mo where m.Mat_id=mis.Mat_id  and mis.MIS_id=mo.MIS_id and mis.MIS_id='$id'";
    这样sql语句没问题了,
    可是现在不管是提交前还是提交后,当出库数量>剩余数量时,都没有错误提示。
    是哪里出错了呢?
    最好有没有什么方法让它在移开光标的时候就弹出错误提示呢?
      

  15.   


    因为你提交后相当于刷新页面了。这样document.getElementById("txtQty").value的其实是刷新了的空值。所以不会有提示。
    应该是提交之前验证。或者ajax。
      

  16.   

    谢谢!现学了下ajax,我用ajax来实现了。