无法通过AJAX从HTML页面上的Php页面访问元素

时间:2017-02-14 02:14:41

标签: php ajax

所以我正在使用AJAX连接到下面显示的Php页面的网站上工作。 php连接到数据库并根据条件进行查询。然后它回显到html作为一个表。我现在要做的是访问输入标签(myCheckBox)和相应的价格和项目描述。项目描述和价格从数据库返回。这两个名称是$ row ['ItemDescription]和$ row [Price]。输入标签被赋予一个id(myCheckBox)。我怎样才能在我的html中引用这些元素。我可以只使用项目描述和价格,但输入标签也很好。 html显示在Php下面。            

 <head>
 <style>
     table {width: 100%;border-collapse: collapse;}
     table, td, th {border: 1px solid black;padding: 5px;}
     th {text-align: left;}
 </style>
 </head>

 <body>

 <?php

  $q =$_GET['q'];

  $con = mysqli_connect("connection","uName","pass", "dbName");
   if (!$con) {
    die('Could not connect: ' . mysqli_error($con));
    }

   mysqli_select_db($con,"ajax_demo");

  if($q == 's'){$sql="SELECT * FROM Goods WHERE Category = 'Sporting Goods'";}
  if($q == 'e'){$sql="SELECT * FROM Goods WHERE Category = 'Entertainment'";}
  if($q == 'c'){$sql="SELECT * FROM Goods WHERE Category = 'Clothes'";}

  $result = mysqli_query($con,$sql);

  echo "<table id='myTable'>
  <tr>
  <th>Item Description</th>
   <th>Price</th>

   </tr>";
     while($row = mysqli_fetch_array($result)) {

     echo "<td><input id='myCheckBox' type='checkbox'>" . $row['ItemDescription'] . "</td>";
echo "<td>" . $row['Price'] . "</td>";

echo "</tr>";
 }
 echo "</table>";
 mysqli_close($con);
 ?>
 </body>
 </html>

这是html。

function showUser(str, myInt) {
    if (str == "bob") {
      document.getElementById("myDiv2").innerHTML = "";
        return;
     } else {
    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 (this.readyState == 4 && this.status == 200) {
            document.getElementById("myDiv2").innerHTML = this.responseText;
    alert(this.responseText);

        }
    };
 if(myInt == 1){xmlhttp.open("GET","getuser.php?q="+str,true);}

if(myInt == 2){xmlhttp.open("GET","shop.php?q="+str,true);}

 if(myInt == 3){xmlhttp.open("GET","aInfo.php?q="+myVar3[0],true);}
     xmlhttp.send();
  }
}

感谢任何有用的回复。请注意,输入标记是在while循环中创建的。这有助于格式化。我希望能够访问每个输入标签及其相应的价格和项目描述。然后我可以通过数组索引引用它们。

1 个答案:

答案 0 :(得分:0)

所以我能够解决这个问题,但我不得不在我的Php页面中做一些重新格式化。目标是从php页面访问元素,并能够在其他页面中处理它们。原始页面将与下面的解决方案一起使用,但是需要额外的步骤。

  function myFun(){myDivVar =document.getElementById("myDiv2").children;
   varArrayNew=Array.prototype.
  slice.c all(myDivVar);
  alert(varArrayNew[1].innerHTML);alert(varArrayNew).length;

      }

和Php .....

<!DOCTYPE html>
<html>
<!--<input type='button' id='bob 'value='hello'> -->
<head>
<style>
   table {width: 100%;border-collapse: collapse;}
   table, td, th {border: 1px solid black;padding: 5px;}
   th {text-align: left;}
</style>
</head>
<body>

<?php

$q =$_GET['q'];

$con = mysqli_connect("path","userNa","pass", "db");
if (!$con) {die('Could not connect: ' . mysqli_error($con)); }

mysqli_select_db($con,"ajax_demo");

if($q == 's'){$sql="SELECT * FROM Goods WHERE Category = 'Sporting Goods'";}
if($q == 'e'){$sql="SELECT * FROM Goods WHERE Category = 'Entertainment'";}
if($q == 'c'){$sql="SELECT * FROM Goods WHERE Category = 'Clothes'";}

$result = mysqli_query($con,$sql);


while($row = mysqli_fetch_array($result)) {

echo "<p>". $row['ItemDescription'].":";
echo $row['Price']. ",<br></p>";


 }

  mysqli_close($con);
  ?>
 </body>
 </html>

不仅仅是在for循环中拆分返回值,迭代响应文本数组的长度减去1