I have a user form that is working perfectly. The page makes use of a javascript script that executes mysql query from an external php file and displays the results on the table.
What I want to do is have the ability to display results from mulitple different queries on the form as well.
I have 4 files in this example. test.php which is the form getdata1.php which fetches mysql results for product information getwhse1.php which fetches mysql results for warehouse information getsu1.php which fetches mysql results for selling units information
Currently the script works when just fetching results from getdata1.php? How do I alter the javascripscript to allow me to display results from getwhse1.php and getsu1.php as well?
Below is the code for the existing pages, what I want to be able to do is enter a product code and display the details of that product code in each of the table fields.
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);
?>
My javascript skills are non existant, how can I edit this script to execute all three mysql queries and display the results on the page? all being activated by entering a product code?
Thanks and Regards, Ryan
$q=$_GET['q'];and preferably use prepared statement. Someone could submit something like' UNION SELECT concat(TABLE_NAME,'.',COULUMN_NAME) from information_schema.COLUMNS where '1'='1(didn't test but you should get the idea)