Using php/javascript to make 3 dependent drop down menus that are populated from a MySQL database -
Using php/javascript to make 3 dependent drop down menus that are populated from a MySQL database -
using php/javascript create 3 dependent drop downwards menus populated mysql database
hi! new coding/web development. know popular topic , have spent hours reading forums , looking @ sample code, however:
a) examples have found include info hard-coded , not pull info database, need do. b) examples utilize extentions mysql_query , mysql_fetch_assoc deprecated. think need utilize pdo unfamiliar that.
here background database:
there 1 table in database--"costs_table." fields trying populate drop downs "subsystem," ,"unit_cost_element" , "unit_cost_component." there 1:m relationship between these fields- subsystem has many elements, , element has many components.
here sql statements want code doing each drop down:
select distinct subsytem costs_table; select distinct unit_cost_element costs_table subsystem = $selectedsubsystem; select distinct unit_cost_component costs_table subsystem = $selectedsubsystem , unit_cost_element = $selectedelement;
i know need arrays have never constructed array before. code have far index page creates list boxes. got code forum may not 100% in-line need do.
anyways here code:
<?php seek { $objdb = new pdo('mysql:host=localhost;dbname=unit_costs_db', 'username', 'password'); $objdb->exec('set character set utf8'); $sql = "select distinct subsystem, id costs_table order subsystem asc"; $statement = $objdb->query($sql); $list = $statement->fetchall(pdo::fetch_assoc); } catch(pdoexception $e){ echo 'there problem'; } ?> <!doctype html> <html lang="en" <head> <meta charset="utf-8" /> <title> dependable dropdown menu</title> <meta name="description" content="dependable dropdown menu" /> <meta name="keywords" content="dependable dropdown menu" /> <link href="/css/6-19_core.css" rel="stylesheet" type="text/css" /> </head> <body> <div id="wrapper"> <form action="" method="post"> <select name="subsystem" id="subsystem" class="update"> <option value="">select subsystem</option> <?php if (!empty($list)) { foreach($list $row) { ?> <option value="<?php echo $row['id']; ?>"> <?php echo $row['subsystem']; ?> </option> <?php } } ?> </select> </select> <br> <select name="element" id="element" class="update" disabled="disabled"> <option value="">----</option> </select> <br> <select name="component" id="component" class="update" disabled="disabled"> <option value="">----</option> </select> </form> </div> <script src="/js/jquery-1.6.4.min.js" type="text/javascript"></script> <script src="/js/6-19_core.js" type="text/javascript"></script> </body> </html>
any help need add together create work appreciated!!! in advance :)
more on pdo prepared statements.
i relative novice php/pdo, if reply can improved, please allow me know how.
firstly, connection needs improved. utilize this:
try { $objdb = new pdo('mysql:host=127.0.0.1;dbname=databasename', 'user', 'password'); $objdb->setattribute(pdo::attr_errmode, pdo::errmode_exception); $objdb->setattribute(pdo::attr_emulate_prepares, false); } catch(pdoexception $e) { echo 'error: ' . $e->getmessage(); }
read more on connections
you need utilize execute
object :) wrap in function. this:
the function written after try
closing brace }
.
function queryselection($objdb,$sql) { $statement = $objdb->prepare($sql); #prepare $statement->execute(); #execute $list = $statement->fetchall(pdo::fetch_assoc); foreach($list $row) { echo '<option value=" ' . $row['id'] . ' "> ' . $row['subsystem'] . '</option>'; } }
in form:
<select> <option value="0">----</option> <?php $sql = 'select distinct subsystem, id costs_table order subsystem asc'; queryselection($objdb,$sql); # $objdb passes database connection variable function # $sql passes query ?> </select> go on each query... <select> <option value="0">----</option> <?php $sql = 'select distinct subsytem costs_table'; queryselection($objdb,$sql); # $objdb passes database connection variable function # $sql passes query ?> </select>
...etc
javascript php jquery mysql ajax
Comments
Post a Comment