在PHP中使用jQuery显示表中的数据



我有一个数据,我使用PHP访问它们。所以我创建了一个简单的按钮,当我点击它,我的程序需要创建一个表的数据。下面你可以看到jQuery代码读取数据,并创建一个表。但问题是,我不能访问DATA中的每个元素。我想说清楚,因为我还添加了代码,这是我在数据中选择的。它叫做"household.php">

<html lang="en">
<head>
<script
src="https://code.jquery.com/jquery-3.6.2.js"
integrity="sha256-pkn2CUZmheSeyssYw3vMp1+xyub4m+e+QK4sQskvuo4="
crossorigin="anonymous"></script>
<link rel="stylesheet" href="style.css">
<script>
function suchen() {
jQuery(document).ready(function($) {
$.ajax({
type: "get",
url: "household.php",
dataType: "json",
success: function(response) {
var data = JSON.parse(response)
var html_table = "";
data.forEach(function(item) {
var row = $("<tr>");
row.append($("<td>").text(item.contact_id_a));
row.append($("<td>").text(item.contact_id_b));
// add more cells for additional columns
html_table += row[0].outerHTML;
});
$("#tabelle").html(html_table);
}
});
});
}
</script>
</head>
<body>
<form id="form" onsubmit="suchen()" method="get">
<label>Enter your age: </label>
<br />
<input type="number" name="min" min="0">
<br />
<input type="number" name="max" min="0">
<br />
<input type="submit">
</form>
<div id="tabelle"></div>
</body>
</html>

这是household.php文件的代码。它工作起来毫无问题。但是我不能连接我的主php文件。

<?php
require_once '/var/www/html/wordpress/wp-content/plugins/civicrm/civicrm/civicrm.config.php';
require_once 'CRM/Core/Config.php';
$config = CRM_Core_Config::singleton();

$relationships = CiviApi4Relationship::get()
->addSelect('contact_id_a', 'contact_id_b', 'contact_id_a.display_name', 'contact_id_b.household_name', 'relationship_type_id')
->addClause('OR', ['relationship_type_id', '=', 7], ['relationship_type_id', '=', 8])
->setLimit(25)
->execute();
foreach ($relationships as $relationship) {
// do something
}
var_dump(json_encode($relationships));
?>

我不能用PHP文件访问数据。我也不能连接我的主php文件,与搜索php.

我想你已经在household.php文件中使用了echo json_encode($relationships)。您不需要解析响应(您使用的),因为您已经将dataType写入json。它会自动转换。希望这个答案对你有所帮助。根据jQuery手册,任何格式错误的JSON都会被拒绝并抛出解析错误。从jQuery 1.9开始,空响应也会被拒绝。所以你可以只使用dataType: 'json',如果你确定,服务器将返回正确格式的json。如果它只返回一个字符串,看起来像JSON",您应该使用dataType: "text JSON"强制jQuery转换。如果上面的答案对你没有帮助,我建议你使用这个函数来解析响应。var data = jQuery.parseJSON(response);var data = $.parseJSON(response)

最新更新