通过AJAX从HTML页面上的Php页面访问元素时出现问题
因此,我正在开发一个使用AJAX连接到下面所示Php页面的网站。php连接到数据库并根据if条件进行查询。然后它作为一个表返回html。我现在要做的是访问输入标签(myCheckBox)以及相应的价格和商品描述。从数据库返回项目描述和价格。这两个名称分别为$row['ItemDescription]和$row[Price]。输入标记被赋予一个id(myCheckBox)。如何在html中引用这些元素。我可以只接受商品描述和价格,但是输入标签也很好。html显示在Php的下面。通过AJAX从HTML页面上的Php页面访问元素时出现问题,php,ajax,Php,Ajax,因此,我正在开发一个使用AJAX连接到下面所示Php页面的网站。php连接到数据库并根据if条件进行查询。然后它作为一个表返回html。我现在要做的是访问输入标签(myCheckBox)以及相应的价格和商品描述。从数据库返回项目描述和价格。这两个名称分别为$row['ItemDescription]和$row[Price]。输入标记被赋予一个id(myCheckBox)。如何在html中引用这些元素。我可以只接受商品描述和价格,但是输入标签也很好。html显示在Php的下面。 <hea
<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>
感谢所有有用的回复。注意,输入标记是在while循环中创建的。这有助于格式化。我想能够访问每个输入标签,它的相应价格和项目描述。然后我可以通过数组索引引用这些 所以我能够解决这个问题,但我不得不在我的Php页面中重新格式化。目标是从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>
function myFun(){myDivVar =document.getElementById("myDiv2").children;
varArrayNew=Array.prototype.
slice.c all(myDivVar);
alert(varArrayNew[1].innerHTML);alert(varArrayNew).length;
}
还有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>
<!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>
表{宽度:100%;边框折叠:折叠;}
表,td,th{边框:1px纯黑色;填充:5px;}
th{文本对齐:左;}