PHP を使い始めたばかりで、jQuery ajax を PHP に移行しようとしています。これが私のPHPファイルです:
<?php
include 'config.php';
include 'opendb.php';
$query = "SELECT * FROM agency ORDER BY name";
$result = mysql_query($query);
while($row = mysql_fetch_assoc($result)){
$id = $row['id'];
$name = $row['name'];
echo "<li class=\"agency\"><a href=\"contentAgency.php?id=$id\">$name</a><ul class=\"agency-sub\"></ul></li>";
}
include 'closedb.php';
?>
これが私の現在のjs関数です:
//Add Agency content
$("ul.top-level").on("click", "li.agency a", function (event) {
if($(this).next().length) {
var numbs = $(this).attr("href").match(/id=([0-9]+)/)[1];
showContentAgency(numbs, this);
} else {
$(this).closest('ul').find('a').removeClass('sub-active');
}
event.preventDefault();
});
そして、ここに showContentAgency(); があります。関数:
function showContentAgency(id, elem) {
$.post("assets/includes/contentAgency.php?id=id", {
id: id
}, function (data) {
$(elem).addClass("nav-active").parent().find("ul").html(data).show();
});
}
私がやりたいのは、jQuery に挿入させるのではなく、PHP に順不同のリストをレンダリングさせることです。これは、上記の PHP ファイルで現在どのように取り上げられているかです。
echo "<li class=\"agency\"><a href=\"contentAgency.php?id=$id\">$name</a><ul class=\"agency-sub\"></ul></li>"
そのため、PHP にリストを入力してもらいたいと思い<ul class="agency-sub">
ます。