0

我有一个 javascript,它从 mysql 获取数据并在我的 html 表单中显示它而无需刷新页面。鉴于我不了解 javascript,我得到了以下脚本,这得到了 StackOverflow 社区的大力协助,即@Brant Olsen。

该脚本可以完美地获取 3 个 mysql 结果字段。我想为此添加额外的 2 个字段并将它们显示在我的 html 表单中。

工作脚本是:

<script type="text/javascript">  
  function showUser(userNumber, str)  
  {  
    if (str=="")  
    {  
      document.getElementById("txtHint" + userNumber).innerHTML="";  
      return;  
    }    
    if (window.XMLHttpRequest)  
    {// code for IE7+, Firefox, Chrome, Opera, Safari  
      xmlhttp=new XMLHttpRequest();  
    }  

    xmlhttp.onreadystatechange=function()  
    {  
      if (xmlhttp.readyState==4 && xmlhttp.status==200)  
      {  
        //document.getElementById("txtHint" + userNumber).innerHTML=xmlhttp.responseText; 
        var responseText = xmlhttp.responseText; 
        var description = responseText; 
        var warehouse = ""; 
        var sellingUnits = ""; 
        if (responseText.indexOf("NOT A VALID") == -1) 
        { 
          description = responseText.substring(12, responseText.indexOf(",Warehouse:"));  
          warehouse = responseText.substring(responseText.indexOf(",Warehouse:")+11, responseText.indexOf(",SellingUnits:"));  
          sellingUnits = responseText.substring(responseText.indexOf(",SellingUnits:")+14);  
        } 

        document.getElementById("whse" + userNumber).innerHTML = warehouse;  
        document.getElementById("txtHint" + userNumber).innerHTML = description;  
        document.getElementById("su" + userNumber).innerHTML = sellingUnits; 

      }  
    }  
    xmlhttp.open("GET","getdata1.php?q="+str,true);  
    xmlhttp.send(); 
  } 
</script> 

getdata1.php 是:

<?php 
 $q=$_GET["q"]; 

$con = mysql_connect('localhost', 'dbuser', 'dbpass'); 
 if (!$con) 
   { 
   die('Could not connect: ' . mysql_error()); 
   } 

mysql_select_db("dbname", $con); 


$sql="SELECT Category, Description,SellingUnits,Grouping,CasesPerPallet,ShrinksPerPallet FROM skudata WHERE packcode = '".$q."'"; 
$result = mysql_query($sql); 
$rows=mysql_num_rows($result); 

if($rows==0){echo "<font color=red><b>NOT A VALID PRODUCT CODE</b></font>";} else { 
while($row = mysql_fetch_array($result)) 
 { 
   echo "Description:" . $row['Description'] . ",Warehouse:" . $row['Grouping'] . ",SellingUnits:" . $row['SellingUnits']; 
 } 
} 

mysql_close($con); 
 ?>  

我已经使用要传递给 html 表单的附加字段更改了 getdata1.php:

$sql="SELECT Category, Description,SellingUnits,Grouping,CasesPerPallet,ShrinksPerPallet,if(SellingUnits='cs', CasesPerPallet,ShrinksPerPallet) as SUQTY FROM skudata WHERE packcode = '".$q."'"; 
$result = mysql_query($sql); 
$rows=mysql_num_rows($result); 

if($rows==0){echo "<font color=red><b>NOT A VALID PRODUCT CODE</b></font>";} else { 
while($row = mysql_fetch_array($result)) 
 { 
   echo "Description:" . $row['Description'] . ",Warehouse:" . $row['Grouping'] . ",SellingUnits:" . $row['SellingUnits'] . ",SUQTY:" . $row['SUQTY'] . ",Category:" . $row['Category']; ; 
 } 
} 

从这里开始,我正在努力正确编码另外两个字段。表格行的示例如下:

<tr id="r1">  
    <td>
        <input type=checkbox name=kvi1 id=kvi1 value=1>
    </td>
    <td>
        <input size=10  type=number id=sku1 name=sku1 onchange="showUser(1, this.value)"><a href="sku.php" target="_blank"><img src=q.png border=0></a>
    </td>
    <td>
        <div align="left" id="txtHint1">&nbsp;</div>
    </td>
    <td>
        <div align="left" id="whse1">&nbsp;</div>
    </td>
    <td>
        <div align="left" id="su1">&nbsp;</div>
    </td>
    <td>
        <div align="left" id="suqty1">&nbsp;</div>
    </td>
    <td>
        <div align="left" id="category1">&nbsp;</div>
    </td>
</tr>

我已经按如下方式编辑了javascript但没有成功,有人可以帮忙吗?

<script type="text/javascript">  
  function showUser(userNumber, str)  
  {  
    if (str=="")  
    {  
      document.getElementById("txtHint" + userNumber).innerHTML="";  
      return;  
    }    
    if (window.XMLHttpRequest)  
    {// code for IE7+, Firefox, Chrome, Opera, Safari  
      xmlhttp=new XMLHttpRequest();  
    }  

    xmlhttp.onreadystatechange=function()  
    {  
      if (xmlhttp.readyState==4 && xmlhttp.status==200)  
      {  
        //document.getElementById("txtHint" + userNumber).innerHTML=xmlhttp.responseText; 
        var responseText = xmlhttp.responseText; 
        var description = responseText; 
        var warehouse = ""; 
        var sellingUnits = ""; 
        var SUQTY = ""; 
        var Category = ""; 
        if (responseText.indexOf("NOT A VALID") == -1) 
        { 
          description = responseText.substring(12, responseText.indexOf(",Warehouse:"));  
          warehouse = responseText.substring(responseText.indexOf(",Warehouse:")+11, responseText.indexOf(",SellingUnits:"));  
          sellingUnits = responseText.substring(responseText.indexOf(",SellingUnits:")+11, responseText.indexOf(",SUQTY:")); 
          suqty = responseText.substring(responseText.indexOf(",SUQTY:")+11, responseText.indexOf(",Category:")); 
          category = responseText.substring(responseText.indexOf(",Category:")+14);  
        } 

        document.getElementById("whse" + userNumber).innerHTML = warehouse;  
        document.getElementById("txtHint" + userNumber).innerHTML = description;  
        document.getElementById("su" + userNumber).innerHTML = sellingUnits; 
        document.getElementById("suqty" + userNumber).innerHTML = SUQTY; 
        document.getElementById("category" + userNumber).innerHTML = Category; 


      }  
    }  
    xmlhttp.open("GET","getdata1.php?q="+str,true);  
    xmlhttp.send(); 
  } 
</script> 

谢谢,瑞安

4

1 回答 1

2

我知道您不会喜欢这个答案,因为它需要您重新编写代码,但我会尽力提供帮助。

实际上,通过 ajax 调用在 PHP 和 JS 之间发送的这种性质的任何类型的数据都应该使用 >= PHP5.3 中的原生 JSON。这样做的好处是 Ajax 和 PHP 可以来回传递数据,而无需在应用程序级别进行任何真正的解析。而且它非常容易改变。如果您需要再次更改它,这是完美的。

注意:如果您的 PHP 版本低于 5.3 并且您没有努力安装 JSON 插件,请不要使用下面的代码。此外,JSON 对象在旧版浏览器中也不是原生的。http://caniuse.com/json

PHP端

使 while 循环内的回显如下所示:

echo json_encode(array(
    "description" => $row['Description'],
    "warehouse" => $row['Grouping'],
    "sellingunits" => $row['SellingUnits'],
    "suqty" => $row['SUQTY'],
    "category" => $row['Category']
));

JS端

var data = {};
if (responseText.indexOf("NOT A VALID") == -1) { 
    data = JSON.parse(responseText);
}
// Check the values or just place an empty string if undefined
document.getElementById("whse" + userNumber).innerHTML = data.warehouse || '';  
document.getElementById("txtHint" + userNumber).innerHTML = data.description || '';  
document.getElementById("su" + userNumber).innerHTML = data.sellingunits || ''; 
document.getElementById("suqty" + userNumber).innerHTML = data.suqty || ''; 
document.getElementById("category" + userNumber).innerHTML = data.category || '';
于 2012-04-03T23:06:24.730 回答