我有一个运行良好的用户表单。该页面使用一个javascript脚本,该脚本从外部php文件执行mysql查询,并在表上显示结果。
我想做的是能够在表单上显示多个不同查询的结果。
在这个例子中,我有4个文件。test.php,它是表单getdata1.php,用于获取产品信息的mysql结果获取仓库信息的mysql结果的getwhse1.phpgetsu1.php获取用于销售单位信息的mysql结果
目前,该脚本仅在从getdata1.php获取结果时工作?如何更改javascrippscript以允许显示getwhse1.php和getsu1.php的结果?
下面是现有页面的代码,我希望能够输入一个产品代码,并在每个表字段中显示该产品代码的详细信息。
test.php
<html>
<head>
<title>Sales Portal</title>
<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();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("txtHint" + userNumber).innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","getdata1.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body topmargin=0>
<form name="orderform" id="orderform" action="newsale.php" method="post">
<table border=1>
<tr>
<td>Product Code</td>
<td>Description</td>
<td>Warehouse</td>
<td>Selling Units</td>
</tr>
<tr id="r1">
<td width=100>
<input size=10 type=number id=sku1 name=sku1 onchange="showUser(1, this.value)">
</td>
<td width=280>
<div align="left" id="txtHint1"> </div>
</td>
<td width=100>
<div align="left" id="whse1"> </div>
</td>
<td width=100>
<div align="left" id="su1"> </div>
</td>
</tr>
<tr id="r2">
<td>
<input size=10 type=number id=sku2 name=sku2 onchange="showUser(2, this.value)">
</td>
<td>
<div align="left" id="txtHint2"> </div>
</td>
<td>
<div align="left" id="whse2"> </div>
</td>
<td width=100>
<div align="left" id="su2"> </div>
</td>
</tr>
<tr id="r3">
<td>
<input size=10 type=number id=sku3 name=sku3 onchange="showUser(3, this.value)">
</td>
<td>
<div align="left" id="txtHint3"> </div>
</td>
<td>
<div align="left" id="whse3"> </div>
</td>
<td width=100>
<div align="left" id="su3"> </div>
</td>
</tr>
<tr id="r4">
<td>
<input size=10 type=number id=sku4 name=sku4 onchange="showUser(4, this.value)">
</td>
<td>
<div align="left" id="txtHint4"> </div>
</td>
<td>
<div align="left" id="whse4"> </div>
</td>
<td width=100>
<div align="left" id="su4"> </div>
</td>
</tr>
</table>
</form>
</body>
</html>
getdata1.php
<?php
$q=$_GET["q"];
$con = mysql_connect('localhost', 'username', 'password');
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 "<font color=red>".$row['Description']."</font>, ";
if($row['SellingUnits']=="CS"){echo "<font color=red>".$row['CasesPerPallet']."</font> ";} elseif($row['SellingUnits']=="SHR") {echo "<font color=red>".$row['ShrinksPerPallet']."</font> ";}
}}
mysql_close($con);
?>
getwhse1.php
<?php
$q=$_GET["q"];
$con = mysql_connect('localhost', username', 'password');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("database", $con);
$sql="SELECT grouping FROM skudata WHERE packcode = '".$q."'";
$result = mysql_query($sql);
$rows=mysql_num_rows($result);
if($rows==0){echo " ";} else {
while($row = mysql_fetch_array($result))
{
echo "<font color=red>".$row['grouping']."</font>, ";
}}
mysql_close($con);
?>
getsu1.php
<?php
$q=$_GET["q"];
$con = mysql_connect('localhost', 'username', 'password');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("database", $con);
$sql="SELECT SellingUnits FROM skudata WHERE packcode = '".$q."'";
$result = mysql_query($sql);
$rows=mysql_num_rows($result);
if($rows==0){echo " ";} else {
while($row = mysql_fetch_array($result))
{
echo "<font color=red>".$row['SellingUnits ']."</font>, ";
}}
mysql_close($con);
?>
我的javascript技能不存在,我如何编辑这个脚本来执行所有三个mysql查询并在页面上显示结果?所有这些都是通过输入产品代码激活的?
感谢和问候,Ryan
<?php
$q=$_GET["q"];
$con = mysql_connect('localhost', 'username', 'password');
header('Content-type: application/json');
if (!$con ||strlen($q)>5){ die(json_encode(array('results'=>-1)))}
mysql_select_db("dbname", $con);
$result=mysql_query("SELECT * FROM skudata WHERE packcode = '$q'");
echo json_encode(array('results'=>$result));
mysql_close($con);
?>
<html><head><title>Sales Portal</title>
<style type="text/css">
.redtext {color: red}
</style>
<script type="text/javascript">
function showUser(userNumber, str){
if (str==""){
document.getElementById("txtHint" + userNumber).innerHTML="";
}
if (window.XMLHttpRequest){xmlhttp=new XMLHttpRequest()}
else{xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")}
xmlhttp.onreadystatechange=function(){
if (xmlhttp.readyState==4 && xmlhttp.status==200){
var json = JSON.parse(xmlhttp.responseText);
result = json.results;
if(result==-1){
document.getElementById('txtHint'+userNumber).innerHTML="Database error.";
}
if(result.length = 0){
document.getElementById("txtHint"+userNumber).innerHTML="INVALID PRODUCT CODE";
}
else {
i=0;
while(i<result.length){
var desc=result[i].Description+" ";
switch(result[i].SellingUnits){
case "CS": desc += result[i].CasesPerPallet+"<br/>";
break;
case: "SHR": desc += result[i].ShrinksPerPallet+"<br/>";
break;
}
document.getElementById('txtHint'+userNumber).innerHTML = desc;
document.getElementById('whse'+userNumber).innerHTML=result[i].grouping+"<br'>";
document.getElementById('su'+userNumber).innerHTML=result[i].SellingUnits+"<br/>";
i++;
}
xmlhttp.open("GET","getdata1.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body topmargin=0>
<form name="orderform" id="orderform" action="newsale.php" method="post">
<table border=1>
<tr>
<td>Product Code</td>
<td>Description</td>
<td>Warehouse</td>
<td>Selling Units</td>
</tr>
<tr id="r1">
<td width=100>
<input size=10 type="number" id="sku1" name="sku1" onchange="showUser(1, this.value)">
</td>
<td width=280>
<div align="left" id="txtHint1" class="redtext"> </div>
</td>
<td width=100>
<div align="left" id="whse1" class="redtext"> </div>
</td>
<td width=100>
<div align="left" id="su1" class="redtext"> </div>
</td>
</tr>
<tr id="r2">
<td>
<input size=10 type="number" id="sku2" name="sku2" onchange="showUser(2, this.value)">
</td>
<td>
<div align="left" id="txtHint2" class="redtext"> </div>
</td>
<td>
<div align="left" id="whse2" class ="redtext"> </div>
</td>
<td width=100>
<div align="left" id="su2" class="redtext"> </div>
</td>
</tr>
<tr id="r3">
<td>
<input size=10 type=number id=sku3 name=sku3 onchange="showUser(3, this.value)">
</td>
<td>
<div align="left" id="txtHint3" class="redtext"> </div>
</td>
<td>
<div align="left" id="whse3" class="redtext"> </div>
</td>
<td width=100>
<div align="left" id="su3" class="redtext"> </div>
</td>
</tr>
<tr id="r4">
<td>
<input size=10 type=number id=sku4 name=sku4 onchange="showUser(4, this.value)">
</td>
<td>
<div align="left" id="txtHint4" class="redtext"> </div>
</td>
<td>
<div align="left" id="whse4" class="redtext"> </div>
</td>
<td width=100>
<div align="left" id="su4" class="redtext"> </div>
</td>
</tr>
</table>
</form>
</body>
</html>
我还没有测试过这段代码,但错误应该很容易在浏览器控制台中找到。php不允许$q超过5个字符以防止注入。
既然它们都接受相同的GET输入,为什么不将所有三个查询合并到一个脚本中呢?然后将它们输出为三个JSON对象的数组。这样做所需的额外处理可以忽略不计,即使一次只使用一个查询,脚本也会更容易维护,JSON会将显示与代码分离,并使其更容易查看和管理。
当脚本中包含所有三个查询时,创建一个新数组来存储结果。
$results=数组('results'=>数组($row1,$row2,$row3));打印json_encode($results);
因为您在这个响应中没有任何HTML,所以您永远不必再次访问php文件来更改输出的HTML外观。
然后在ajax responseText上使用JSON.parse()。现在,您的三个表结果都是JavaScript对象。你甚至可以用它们的表名来称呼它们:
var response=JSON.parse(xmlhttp.responseText);
var row1=response.results[0];
var row2=response.results[1];
var row3=响应。结果[3];
现在,您可以使用点和字段名称访问结果。像row3[i].SellingUnits(其中i是row3数组的索引。换句话说,在JS中循环结果,而不是在php中)。您可以像往常一样使用JS将其写入HTML。它很优雅。结果可以在JS中处理(例如,你可以对结果进行数学运算,但现在不能这样做。最重要的是,你不必重新访问php来调整某些内容(例如,将b标记更改为h4标记)。另一个优点是,如果您决定更改MySQL表,JSON将紧随其后(您仍然不必编辑php)。