php+mysql+Ajax为新增数据行设置ID并对xml数据(包括为空的情况)进行验证

来源:互联网 发布:淘宝视频几分钟 编辑:程序博客网 时间:2024/05/16 03:20

//author:lucas

//function:代码演示了如何透过javascript手工添加数据行,并通过dom为数据行动态添加ID,以方便输入料号名称后通过ajax技术自动带出料号的描述,并告知如何针对返回数据为空时如何处理( part_desc以及vendor_desc中所示)

 

1、po_add.php

<script type="text/javascript"><!-- 
 var sum;
 sum=5; 
 function addDetailProcess(num){
  var num;
  var temp;
  if (num == '')
   num = 1;
  for (i=0;i<num;i++){
   sum=sum+1;
   if (sum % 2 == 0){
    var newRow=tbDetailHide1.rows[0].cloneNode(true);
   }else if (sum % 2 != 0){
    var newRow=tbDetailHide2.rows[0].cloneNode(true);
   }
   temp=sum*1;
   newRow.all.a.value=temp; 
   tbDetailUsed.insertBefore(newRow); 
   var partid = 'part_id' + (sum);
   tbDetailUsed.rows[sum].firstChild.nextSibling.firstChild.setAttribute('id',partid);
  }
 } 
 

 function auto_upper_str(strObj) {
  strObj.value = strObj.value.toUpperCase();
    }
 
 function autoInput(){
  var e;
  with(document.myform){
   var tmp1 = document.myform.po_req_date.value;
   for (var i=0;i<elements.length;i++){
    e = elements[i];
    if (e.tagName == "INPUT" && e.type.toLowerCase() == "text" && e.name.substr(0,12) == "pod_req_date"){
     e.value = tmp1;
    }
   }
  }
 }
 
 //*************************************************************************
 
 var xmlHttp;
 function createXHR(){
  if (window.XMLHttpRequest){
   xmlHttp = new XMLHttpRequest();
  }
  else if (window.ActiveXObject){
   xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
  }
  if (!xmlHttp){
   alert("Your browser doesn't support XMLHTTP object !");
   return false;
  }
 }

 function selectVendor(obj){
  var url = "vendor_desc.php?po_vendor="+obj.value;
  createXHR();
  xmlHttp.open('GET',url,true);
  xmlHttp.onreadystatechange = catchVendorXML;
  xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
  xmlHttp.send(null); 
 }
 
 function catchVendorXML(){
  if(xmlHttp.readyState == 4 && xmlHttp.status == 200){
   var doc = xmlHttp.responseXML;
   if (doc.getElementsByTagName("vendor").length > 0){
    var vd_desc = doc.getElementsByTagName("vd_desc")[0].firstChild.nodeValue;
    document.getElementById("vendor_desc").innerHTML = vd_desc;
   }else{
    var pt_null_desc = doc.lastChild.firstChild.nodeValue;
    document.getElementById('vendor_desc').innerHTML = pt_null_desc;
   }
  }
 }
 
 //***********************************************************************
 var part_id;
 
 function selectPartDesc(obj){
  var url = "part_desc.php?pt_part="+obj.value;
  part_id = obj.id;
  createXHR();
  xmlHttp.open('GET',url,true);
  xmlHttp.onreadystatechange = catchPartDescXML;
  xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
  xmlHttp.send(null); 
 }
 
 function catchPartDescXML(){
  if(xmlHttp.readyState == 4 && xmlHttp.status == 200){
   var doc = xmlHttp.responseXML;
   if (doc.getElementsByTagName("pt_desc").length > 0){
    var pt_desc = doc.getElementsByTagName('pt_desc')[0].firstChild.nodeValue;
    document.getElementById(part_id).parentNode.nextSibling.firstChild.value = pt_desc;
   }else{
    document.getElementById(part_id).parentNode.nextSibling.firstChild.value = '';
      }
  }
 }
//--></script>

 <form name="myform" method="post" action="">
  <fieldset>
   <legend>BASIC</legend>
   <table cellpadding="0" cellspacing="1">
     <tr>
      <th class="criteria">Vendor_ID:</th>
      <td><input name="po_vendor" type="text" size="11" maxlength="20" value="<?php echo $_POST['po_vendor']; ?>" onpropertychange="selectVendor(this);" /></td>
      <td id="vendor_desc" colspan="4"></td>
     </tr>
     <tr>
         <th class="criteria">Require_Date:</th>
      <td><input name="po_req_date" type="text" size="15" maxlength="10" value="<?php echo $_POST['po_req_date']; ?>" onChange="autoInput()" /></td>
     </tr>
   </table>
  </fieldset>
  
  <fieldset>
   <legend>EXTRA</legend>
   <table class="list">
    <tbody id="tbDetailUsed">
    <tr>
     <th class="header">Line</th>
     <th class="header">Part Number</th>
     <th class="header">Description</th>
     <th class="header">Vendor Part</th>
     <th class="header">List Price</th>
     <th class="header">Require Date</th>
     <th class="header">Require Qty</th>
     <th class="header">UM</th>
     <th class="header">Notes</th>
    </tr>
    <?php
    for ($i=1;$i<=5;$i++){
     if ($i % 2 == 0){
      echo '<tr class="even">';
     }
     else{
      echo '<tr class="odd">';
     }
     echo '
      <td>'.$i.'<input name="pod_line[]" type="hidden" value="'.$i.'"></input></td>
      <td><input name="pod_part[]" id="part_id'.$i.'" type="text" size="15" maxlength="15" onpropertychange="selectPartDesc(this)" onKeyUp="auto_upper_str(this)"></input></td>
      <td><input name="pod_desc" type="text" size="15" maxlength="50" readonly ></input></td>
      <td><input name="pod_vd_part[]" type="text" size="15" maxlength="15" onKeyUp="auto_upper_str(this)"></input></td>
      <td><input name="pod_list_price[]" type="text" size="6" maxlength="15"></input></td>
      <td><input name="pod_req_date[]" type="text" size="10" maxlength="10"></input></td>
      <td><input name="pod_req_qty[]" type="text" size="7" maxlength="12"></input></td>
      <td><input name="pod_ord_um[]" type="text" size="2" maxlength="2"></input></td>
      <td><input name="pod_notes[]" type="text" size="10" maxlength="20"></input></td>
     </tr>';
    }
    ?>
   </tbody>
   </table>
      Add<input name="add_num" type="text" size="2" maxlength="2" />records
         <input name="add_btn" type="button" value="Add" onclick="addDetailProcess(document.myform.add_num.value)" />
         <input name="save" type="submit" value="Save" />
  </fieldset>
  
  <!--增加出来的表格样式(隐藏)设置两个表格样式是为了区分奇偶行背景色-->
  <table class="list" id="tbDetailHide1" style="display:none">  
   <tr class="even">
    <td><input name="a" type="text"  size="2" readonly="readonly" style="color:#000;background-color:#FFF;border:none;text-align:center;"></input></td>
    <td><input name="pod_part[]" type="text" size="15" maxlength="15" onpropertychange="selectPartDesc(this)" onKeyUp="auto_upper_str(this)"></input></td>
    <td><input name="pod_desc" type="text" size="15" maxlength="50" readonly></input></td>
    <td><input name="pod_vd_part[]" type="text" size="15" maxlength="15" onKeyUp="auto_upper_str(this)"></input></td>
    <td><input name="pod_list_price[]" type="text" size="6" maxlength="15"></input></td>
    <td><input name="pod_req_date[]" type="text" size="10" maxlength="10"></input></td>
    <td><input name="pod_req_qty[]" type="text" size="7" maxlength="12"></input></td>
    <td><input name="pod_ord_um[]" type="text" size="2" maxlength="2"></input></td>
    <td><input name="pod_notes[]" type="text" size="10" maxlength="20"></input></td>
   </tr>
  </table>
  <table class="list" id="tbDetailHide2" style="display:none">  
   <tr class="odd">
    <td><input name="a" type="text"  size="2" readonly="readonly" style="color:#000;background-color:#EEE;border:none;text-align:center;"></input></td>
    <td><input name="pod_part[]" type="text" size="15" maxlength="15" onpropertychange="selectPartDesc(this)" onKeyUp="auto_upper_str(this)"></input></td>
    <td><input name="pod_desc" type="text" size="15" maxlength="50" readonly></input></td>
    <td><input name="pod_vd_part[]" type="text" size="15" maxlength="15" onKeyUp="auto_upper_str(this)"></input></td>
    <td><input name="pod_list_price[]" type="text" size="6" maxlength="15"></input></td>
    <td><input name="pod_req_date[]" type="text" size="10" maxlength="10"></input></td>
    <td><input name="pod_req_qty[]" type="text" size="7" maxlength="12"></input></td>
    <td><input name="pod_ord_um[]" type="text" size="2" maxlength="2"></input></td>
    <td><input name="pod_notes[]" type="text" size="10" maxlength="20"></input></td>
   </tr>
  </table>
  
  <?php
  if ( $_POST['save'] == 'Save'){
     
   function db_link()
   {
    $access_id = "root";
    $db_name   = "inv";
    @ $db      = mysql_connect('localhost', $access_id, '831025') or
        die("Could not connect to database. Please contact with IT supporting team ASAP.");
    mysql_query("SET NAMES 'GBK'");
    mysql_select_db($db_name);
    return $db;
   }
   $link = db_link();
   
   $pod_line   = $_POST['pod_line'];
   $pod_part   = $_POST['pod_part'];
   $pod_vd_part  = $_POST['pod_vd_part'];
   $pod_list_price = $_POST['pod_list_price'];
   $pod_req_date  = $_POST['pod_req_date'];
   $pod_req_qty  = $_POST['pod_req_qty'];
   $pod_ord_um  = $_POST['pod_ord_um']; 
   $pod_notes   = $_POST['pod_notes'];
   
   foreach ($pod_part as $value){
    static $m=0;
    if ($value != null){
     $pod_sql = "INSERT INTO pod VALUES('".$po_po."','".($m+1)."','".$pod_part[$m]."','".$pod_vd_part[$m];
     $pod_sql = $pod_sql."','open','".$pod_list_price[$m]."','".$pod_list_price[$m]."','','','".$pod_ord_um[$m];
     $pod_sql = $pod_sql."','".$pod_req_date[$m]."','".$pod_req_qty[$m]."','','','','','','','".$sys_user_id;
     $pod_sql = $pod_sql."',now(),'".$sys_user_id."',now(),'".$pod_notes[$m]."') ";
     //$pod_res = mysql_query($pod_sql);
     $m++;
    }
    else{
     $m++;
     continue;
    }
   }
  }
?>
</form>

2、part_desc.php

<?php
header("Content-Type: text/xml");
function db_link()
{
 $access_id = "root";
 $db_name   = "inv";

 @ $db = mysql_connect('localhost', $access_id, '831025') or
  die("Could not connect to database. Please contact with IT supporting team ASAP.");
 mysql_query("SET NAMES 'GBK'");
 mysql_select_db($db_name);
 return $db;
}
$link = db_link();
/*
********************************************************************************************************
Description:part description -xml
********************************************************************************************************
*/

$part = $_REQUEST['pt_part'];

if($part != ''){
$part_sql    = "SELECT * FROM part WHERE pt_part ='".$part."'";
$part_result = mysql_query($part_sql);
$part_nums   = mysql_fetch_array($part_result);
 if($part_nums>0){
         echo '<?xml version="1.0" encoding="gb2312"?>';
   echo '<root>';
   echo '<pt_desc>'.$part_nums['pt_desc'].'</pt_desc>';
   echo  '</root>';
 }
else{
 echo '<?xml version="1.0" encoding="gb2312"?>';
 echo  '<root>';
 echo  '<![CDATA[';
 echo  '<div>&nbsp;</div>';
 echo   ']]>';
 echo   '</root>'; 
   }
}else{
 echo '<?xml version="1.0" encoding="gb2312"?>';
 echo  '<root>';
 echo  '<![CDATA[';
 echo  '<div>&nbsp;</div>';
 echo   ']]>';
 echo   '</root>'; 
}

?>

3、vendor_desc.php

<?php
header("Content-Type: text/xml");
function db_link()
{
 $access_id = "root";
 $db_name   = "inv";

 @ $db = mysql_connect('localhost', $access_id, '831025') or
  die("Could not connect to database. Please contact with IT supporting team ASAP.");
  mysql_query("SET NAMES 'GBK'");
 mysql_select_db($db_name);
 return $db;
}
$link = db_link();

/*
********************************************************************************************************
Description:vendor description -xml
********************************************************************************************************
*/

$po_vendor  = $_REQUEST['po_vendor'];

if($po_vendor != ''){
 $vd_sql_1 = "SELECT * FROM vendor WHERE vd_id='".$po_vendor."' ";
 $vd_res_1 = mysql_query($vd_sql_1); 
 $vd_num = mysql_num_rows($vd_res_1);
 if ($vd_num > 0){
     echo '<?xml version="1.0" encoding="gb2312"?>';
  echo '<vendor>';
  $vd_row = mysql_fetch_array($vd_res_1);
  echo '<vd_desc>'.$vd_row['vd_name'].' ( '.$vd_row['vd_desc'].' )</vd_desc>';
  echo '<vd_pay_term>'.$vd_row['vd_pay_term'].'</vd_pay_term>';
  echo '<vd_curr>'.$vd_row['vd_curr'].'</vd_curr>';
  if ($vd_row['vd_contact1'] == '')
   echo '<vd_contact1>N/A</vd_contact1>';
  else
   echo '<vd_contact1>'.$vd_row['vd_contact1'].'</vd_contact1>';
  echo'</vendor>';
 }
 else{
 echo '<?xml version="1.0" encoding="gb2312"?>';
 echo  '<root>';
 echo  '<![CDATA[';
 echo  '<div>&nbsp;</div>';
 echo   ']]>';
 echo   '</root>'; 
   }
}else{
 echo '<?xml version="1.0" encoding="gb2312"?>';
 echo  '<root>';
 echo  '<![CDATA[';
 echo  '<div>&nbsp;</div>';
 echo   ']]>';
 echo   '</root>'; 
}

?> 

原创粉丝点击