jQueryとPHPを使用してAJAXフィルタを作成しようとしています。私のユーザーがドロップダウンから原料を選択すると、データベースに、その原料を含むレシピが存在するかどうか、そしてユーザーがレシピを作成したかどうかがわかります。このクエリは完全に機能し、各レシピのHTMLをループして作成することができます。私はAJAX経由でHTMLを更新したい。現時点では私は私のAJAXのためにこれを持っている:それは私がした後、以下のHTMLとIDの料理レシピでのdivのデフォルトのデータを置き換えたいと呼ばれていたときにjQuery AJAXリクエストでHTMLコンテンツを置き換えます。
$(document).ready(function() {
$('#filterButton').click(function(e) {
e.preventDefault();
// When the filter button is clicked, grab the ingredient ID and the cuisine ID
var mainIngred = $('#sel1').val();
var cuisine = $('#cuisine').val();
var userID = $('#userIDHidden').val();
var data = {
"ingredID" : mainIngred,
"tagID" : cuisine,
"userID" : userID
}
var filterajaxurl = '/recipe-project/filter-ajax.php';
$.ajax({
url: filterajaxurl,
type: "POST",
data: data,
success:function(data) {
$("#cookbook-recipe").html(data);
}
});
});
});
これは、私が呼び出すPHPスクリプトですPHPがループし、各レシピを取得しました。以下は
include_once('classes/class-database-functions.php');
$db_functions = new Database_Functions();
$ajax_recipes = $db_functions->ajax_filter_search(23,6);
$recipes_array = array();
foreach ($ajax_recipes as $ajax_recipe) {
$search_recipes = $db_functions->get_single_recipe($ajax_recipe);
$rows = mysqli_fetch_array($search_recipes, MYSQL_ASSOC);
array_push($recipes_array,$rows);
} ?>
<?php
if (isset($recipes_array)) {
foreach ($recipes_array as $single_recipe) { ?>
<div class="col-md-4 portfolio-item">
<a href="single-recipe.php?id=<?php echo $single_recipe['recipe_id'];?>">
<?php if ($single_recipe['recipe_image'] == '') { ?>
<img class="img-responsive" src="http://placehold.it/300x200" alt="">
<?php } else { ?>
<img class="img-responsive" src="<?php echo $single_recipe['recipe_image']; ?>" alt="">
<?php } ?>
</a>
<h4>
<a href="#">
<?php echo $single_recipe['recipe_name']; ?>
</a>
</h4>
</div>
<?php } } else { ?>
<div class="col-md-4 portfolio-item">
<h4>Please Add Some Recipes</h4>
</div>
<?php } ?>
が、私はボタンをクリックすると、HTMLが交換されません。しかし、私のデフォルトのHTML
<button id="filterButton" class="btn btn-lg active" role="button">Filter Recipes</button>
<div id="cookbook-recipes" class="col-md-9">
<?php
if (isset($recipes_array)) {
foreach ($recipes_array as $single_recipe) { ?>
<div class="col-md-4 portfolio-item">
<a href="single-recipe.php?id=<?php echo $single_recipe['recipe_id'];?>">
<?php if ($single_recipe['recipe_image'] == '') { ?>
<img class="img-responsive" src="http://placehold.it/300x200" alt="">
<?php } else { ?>
<img class="img-responsive" src="<?php echo $single_recipe['recipe_image']; ?>" alt="">
<?php } ?>
</a>
<h4>
<a href="#">
<?php echo $single_recipe['recipe_name']; ?>
</a>
</h4>
</div>
<?php } } else { ?>
<div class="col-md-4 portfolio-item">
<h4>Please Add Some Recipes</h4>
</div>
<?php } ?>
</div>
であると私はコンソールのエラーが出ていない、誰もがなぜ見ることができますか?
'#cookbook-recipe'要素はDOMに存在しますか?あなたは 'console.log(data)'を使ってリクエストのレスポンスをチェックしましたか?また、AJAXレスポンスのHTMLには、同じ 'cookbook-recipes' idを持つ複数の要素があり、無効であることに注意してください。これはすぐに問題を引き起こすことはありませんが、クラスに改訂する必要があります。 –
それは私が望むすべてのデータを返します。それはありますか? – pocockn
'#filterButton'はどのようなタイプの要素ですか? –