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>
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);
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