javascript - PHP MySQl Chained Select -
i have 2 drop downs populated mysql tables. in mysql tables have foreign key restraints table "assets" has column "department" linked department table. each asset has associated department.
my first dropdown "department" , want second dropdown show results selected department.
here's code.
<select id="location" name="location"> <option value="">select asset location</option> <?php $pdo = new pdo('mysql:host=localhost;dbname=maintlog', 'root', '*******'); #set error mode errmode_exception. $pdo->setattribute( pdo::attr_errmode, pdo::errmode_exception); $stmt = $pdo->prepare('select id,name location'); $stmt->execute(); while ($row = $stmt->fetch(pdo::fetch_assoc)) { echo "<option value='$row[id]'>$row[name]</option>"; } ?> </select>
and asset:
<select id="asset" name="asset"> <option value="">select asset</option> <?php $pdo = new pdo('mysql:host=localhost;dbname=maintlog', 'root', '*****'); #set error mode errmode_exception. $pdo->setattribute( pdo::attr_errmode, pdo::errmode_exception); $stmt = $pdo->prepare('select id,assetcode assets'); $stmt->execute(); while ($row = $stmt->fetch(pdo::fetch_assoc)) { echo "<option value='$row[id]'>$row[assetcode]</option>"; } ?> </select>
i have no idea start this, i've done javascript chained selects not mysql/php.
thanks
with mighty parameter binding can reduce ammount of code have write , maintain.
try following example rid of if (1), if (2), if (3) .....
if (filter_input(input_get, "smartload")) { sleep(1); } header("access-control-allow-origin: *"); $response[""] = "--"; /* locations , asset details data -------------------------------------------------------- */ if (filter_input(input_get, 'location')) { $pdo = new pdo('mysql:host=localhost;dbname=********', 'root', '********'); #set error mode errmode_exception. $pdo->setattribute( pdo::attr_errmode, pdo::errmode_exception); $stmt = $pdo->prepare('select id,assetcode assets location=:getlocation'); $stmt->bindparam(':getlocation', filter_input(input_get, 'location'), pdo::param_int); $stmt->execute(); while ($row = $stmt->fetch(pdo::fetch_assoc)) { $response ["$row[id]"]="$row[assetcode]"; } } print json_encode($response); ?>
bind parameter query , execute. no need repeat code on , over.
Comments
Post a Comment