I have 2 dropdowns as below;
<div class="form-group">
<label>Category</label>
<select class="form-control bg-dark btn-dark text-white" id="drpcategory" name="drpcategory" onchange="submit()" required>
<?php
$category = ''.$dir.'/template/post/category.txt';
$category = file($category, FILE_IGNORE_NEW_LINES);
foreach($category as $category)
{
echo "<option value='".$category."'>$category</option>";
}
?>
</select>
</div>
<div class="form-group">
<label>Item</label>
<select class="form-control bg-dark btn-dark text-white" id="drpitem" name="drpitem">
<?php
$category = $_POST['drpcategory'];
$item = ''.$dir.'/template/post/'.$category.'/item.txt';
$item = file($item, FILE_IGNORE_NEW_LINES);
foreach($item as $item)
{
echo "<option value='".$item."'>$item</option>";
}
?>
</select>
</div>
The drpitem dropdown is populated according to the selection made by the drpcategory dropdown. Currently I can catch the $category variable in drpitem by $_POST['drpcategory'] and it works. But the problem is that I use submit() function for the onchange event in drpcategory, so that the whole page simply reloads and then drpitem gets populated as expected. This makes the drpcategory to reset back to it's default selection since it doesn't remember what was it's value before the page was reloaded.
How can I catch the $_POST['drpcategory'] in drpitem without reloading the page? I'm trying to stick with PHP and use minimum amount of JavaScript if required.
This question was later updated and answered here: AJAX POST & PHP POST In Same Page