Try building a website that contains add product function with PHP code. Say we are adding a phone product, category will be Mobile Phones and the subcategory can be Samsung or iPhone. Another example would be Cars - Tesla or Toyota.
Problem facing is the php variable can't get the option value. Example: I selected the category, the subcategory option for the category can't refresh instantly like live. The way to get the subcategory option is SEARCH ALL from the category TABLE and get the subcategory that have the same SELECTED category ID. But the subcategory option can't refresh when I change the value of category ID because php is a server-side code.
I did try ajax and URL parameter but this is already inside a form and these two way need GET and POST, I don't think nesting these 2 form is a good idea. Cookie only works after I reload the page so that the php code can catch the cookie value.(Or it can, but im totally a newbie to cookie so idk). Anyone other way to let the php variable get the html option value in this case?
php
$x = $_COOKIE['scid'];
$c = getByID("scategory",$x);
Script
$('.categoryid').change(function(){
$('#tajax').click(function(){
var q = $('.categoryid').val();
console.log(q);
window.history.replaceState(null, null, "?cid="+q);
document.cookie = "scid="+q;
});
});
For your better understanding.
HTML category
HTML sub category
PHP structure
What I try
Category code
Sub Category code
Function Code