dongshi2836 2015-10-22 23:43
浏览 27

无法使用从属ajax下拉菜单列出products表中的类别

I have the following dropdown

index.php (Where I believe the error is, can't see, error reporting is on)

<!doctype html public "-//w3c//dtd html 3.2//en">
<html>
<head>
<title></title>

<script type="text/javascript">
function ajaxFunction(choice)
{

var httpxml;
try
  {
  // Firefox, Opera 8.0+, Safari
  httpxml=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    httpxml=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    try
      {
      httpxml=new ActiveXObject("Microsoft.XMLHTTP");
      }
    catch (e)
      {
      alert("Your browser does not support AJAX!");
      return false;
      }
    }
  }
function productChanged() 
    {
    if(httpxml.readyproduct==4)
      {
//alert(httpxml.responseText);
var myObject = JSON.parse(httpxml.responseText);

for(j=document.myForm.product.options.length-1;j>=0;j--)
{
document.myForm.product.remove(j);
}

var `=myObject.value.product1;

var optn = document.createElement("OPTION");
optn.text = 'Select product';
optn.value = '';
document.myForm.product.options.add(optn);
for (i=0;i<myObject.product.length;i++)
{
var optn = document.createElement("OPTION");
optn.text = myObject.product[i];
optn.value = myObject.product[i];
document.myForm.product.options.add(optn);

if(optn.value==product1){
var k= i+1;
document.myForm.product.options[k].selected=true;
}
} 

//////////////////////////
for(j=document.myForm.employee.options.length-1;j>=0;j--)
{
document.myForm.employee.remove(j);
}
var employee1=myObject.value.employee1;
//alert(employee1);
for (i=0;i<myObject.employee.length;i++)
{
var optn = document.createElement("OPTION");
optn.text = myObject.employee[i];
optn.value = myObject.employee[i];
document.myForm.employee.options.add(optn);
if(optn.value==employee1){
document.myForm.employee.options[i].selected=true;
}

} 


///////////////////////////
document.getElementById("txtHint").style.background='#00f040';
document.getElementById("txtHint").innerHTML='done';
//setTimeout("document.getElementById('txtHint').style.display='none'",3000)
    }
    }

var url="check.php";
var category=myForm.category.value;
if(choice != 's1'){
var product=myForm.product.value;
var employee=myForm.employee.value;
}else{
var product='';
var employee='';
}
url=url+"?categories="+categories;
url=url+"&products="+products;
url=url+"&employees="+employees;
url=url+"&id="+Math.random();
myForm.st.value=product;
//alert(url);
 document.getElementById("txtHint2").innerHTML=url;
httpxml.onreadyproductchange=productChanged;
httpxml.open("GET",url,true);
httpxml.send(null);
 document.getElementById("txtHint").innerHTML="Please Wait....";
document.getElementById("txtHint").style.background='#f1f1f1';
}
</script>


</head>

<body >

<?php
require 'connect-db.php';

session_start();
$session_id=session_id(uniqid());
?>
</head>

<body>
<div id="txtHint" style="width : 100px;background-color: #cccc33;"></div>
<br><br>
<form name="myForm" action='details.php' method='post'>
<input type=hidden name=st value=0>
<table width=500>
<tr><td >
Select category<br><select name=category id='s1' onchange=ajaxFunction('s1');>
<option value=''>Select One</option>

<?Php
$select_category="SELECT DISTINCT categories FROM products ";
foreach ($dbo->query($select_category) as $row) {
echo "<option value=$row[categories]>$row[categories]</option>";
}
?>
</select>

</td><td ><select name=product  onchange=ajaxFunction('s2');>
<option value=''>Select Product</option></select></td>

<td ><select name=employee  onchange=ajaxFunction('s3');>
<option value=''>Select Employee</option></select></td>
</tr></tr>
<tr><td colspan=3><input type=submit value='Submit'></td></tr>
</form>
</table>
<br><br>
<div id="txtHint2"></div>

</body>
</html>

details.php

<!doctype html public "-//w3c//dtd html 3.2//en">
<html>
<head>
<title></title>
</head>

<body>
<?Php
echo "category : $_POST[categories]<br>
product : $_POST[products]<br>
employee : $_POST[employee]<br>
<br><br><br>
Return to <a href=index.php>Drop down list</a>
";
?>



</body>
</html>

check.php

<?Php
require "connect-db.php"; // connection details

$category=$_GET['categories'];
$product1=$_GET['products'];
$employee1=$_GET['employees'];
;
///////////// Validate the inputs ////////////
// Checking category variable ///
if((strlen($category)) > 0 and (!ctype_alpha($category))){ 
echo "Data Error";
exit;
}
// Checking product variable (with space ) ///

if ((strlen($product1)) > 0 and ctype_alpha(str_replace(' ', '', $product1)) === false) {
echo "Data Error";
exit;
}

/////////// end of input validation //////

if(strlen($category) > 0){
$q_category="SELECT DISTINCT(name) FROM products WHERE categories = '$category'";
}else{
$q_category="SELECT DISTINCT(name) FROM products";
}
//echo $q_category;
$sth = $dbo->prepare($q_category);
$sth->execute();
$product = $sth->fetchAll(PDO::FETCH_COLUMN);

$q_product="SELECT DISTINCT(fname,lname) FROM employees ";
if(strlen($category) > 0){
$q_product= $q_product . " categories = '$category' ";
}
if(strlen($product1) > 0){$q_product= $q_product . " and  products='$product1'";}
$sth = $dbo->prepare($q_product);
$sth->execute();
$employee = $sth->fetchAll(PDO::FETCH_COLUMN);

$main = array('products'=>$products,'employees'=>$employee,'value'=>array("product1"=>"$product1","employee1"=>"$employee1"));
echo json_encode($main); 

////////////End of script /////////////////////////////////////////////////////////////////////////////////
?>

So the error is simple, I have a table called products, that contain categories, i just can't list them in the dropdown menu, and i know it's probably a stupid mistake from my part, it's 3 am so kinda blind. I posted the entire script, if there are other issues I haven't seen, would love to know about them.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
    • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
    • ¥20 软件测试决策法疑问求解答
    • ¥15 win11 23H2删除推荐的项目,支持注册表等
    • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
    • ¥15 qt6.6.3 基于百度云的语音识别 不会改
    • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
    • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
    • ¥15 lingo18勾选global solver求解使用的算法
    • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行