values of one combo box on basis of other combo box - php -


i have 2 tables categories , sub-categories in database. on form have 2 combo boxes category , sub categories. want when change category, subcategories should change accordingly. can if arrays defined in javascript. in case subcategories load database on basis of value of category selected. can fetch data database using php. if data in php array how pass javascript. how can solve problem without ajax? if solution json how can implement json never used it.

either put subcategories in javascript or you'll have use ajax.

it's easy pass data php javascript. if have subcategories in array $subcategories can use result of json_encode($subcategories) safely in javascript (it - o wonder - json encodes array).

so either make ajax service returns specific subcategory or write php script generates javascript array containing subcategories.

a simple ajax service this:

$id = (int)$_get['category']; // id of category $subcategories = ... // fetch array of subcategories db echo json_encode($subcategories); 

so everytime user click on category, javascript has make ajax call script above, specifying new category id.

if use jquery, doing ajax easy.

$.ajax({   url: "subcategories.php?cat=123",   success: function(encoded){     $("#output").append(encoded);   } }); 

(the example above more or less comes directly jquery docs.) parse json in javascript use eval(), function evil. better use json.parse() or jquery alternative.

var subcategories = json.parse(encoded); 

now variable subcategories contains exact same datastructure $subcategories in php script contained.


Comments

Popular posts from this blog

java - SNMP4J General Variable Binding Error -

windows - Python Service Installation - "Could not find PythonClass entry" -

Determine if a XmlNode is empty or null in C#? -