我的代码有问题,因为我想同时获取父类别和子类别,但它不起作用
问题描述:
这是产品上传部分,用户可以在其中选择类别:
This is the product upload section where the user can select the category:
<p class="admin-estate-add-section-input">
Parent category
</p>
<option value=""<?=(($parent == '')?' selected':'')?>>
<?php while($p = mysqli_fetch_assoc($parentQuery)): ?>
<option value="<?=$p['id'];?>"<?=(($parent == $p['id'])?' selected':'')?>><?=$p['category'];?>
<?php endwhile; ?>
<br>
<br>
<p class="admin-estate-add-section-input">
Child category
</p>
<br>
<br>
这是调用child_categories.php文件的jQuery函数
This is the jQuery function that calls the child_categories.php file
function get_child_options(selected){
if(typeof selected === 'undefined'){
var selected = '';
}
var parentID = jQuery('#parent').val();
jQuery.ajax({
url: '/tartalomkezelo/admin/parsers/child_categories.php',
type: 'POST',
data: {parentID : parentID, selected: selected},
success: function(data){
jQuery('#child').html(data);
},
error: function(){alert("Something went wrong! Can't select category.")},
});
}
jQuery('select[name="parent"]').change(get_child_options);
这是child_categories.php文件:
This is the child_categories.php file:
<?php
require_once $_SERVER['DOCUMENT_ROOT'].'/tartalomkezelo/core/init.php';
$parentID = (int)$_POST['parentID'];
$selected = sanitize($_POST['selected']);
$childQuery = $db->query("SELECT * FROM categories WHERE parent = '$parentID' ORDER BY category");
ob_start(); ?>
<?php while($child = mysqli_fetch_assoc($childQuery)): ?>
<option value="<?=$child['id'];?>"<?=(($selected == $child['id'])?' selected':'');?>><?=$child['category'];?>
<?php endwhile; ?>
<?php echo ob_get_clean(); ?>
然后是产品上传文件的脚本:
And then the script for the product upload file:
jQuery('document').ready(function(){
get_child_options('<?=$category;?>');
});
我尝试了什么:
我试图从数据库中获取数据进行编辑,但子类别在父类别
What I have tried:
I have tried to fetch data from database for editing but child category data did not fetched at the sametime with parent category
答
parent = = '')?' selected':'')? > >
<? php while(
parent == '')?' selected':'')?>> <?php while(
p = mysqli_fetch_assoc(
p = mysqli_fetch_assoc(
parentQuery)):?>
< 选项 值 = <?=
parentQuery)): ?> <option value="<?=