Hi guys sorry to bother you i just wanted to ask a quick question

using dreamweaver along with PHP

in dreamweaver i have managed to populate a list menu with data from a database. i wanted to know if it is posible to display text from another field within that database, dependant on the selction within the list menu. so for example if i choose option 1 in the list menu, a description held within the database for option 1 will be populated within a text field or text area (or whatever component is correct for this type of function) within the page.

ps. the text field or text area is not editable, it is just displaying a description

can anyone help me with this query?? Thank you in advance :)

Recommended Answers

All 3 Replies

Member Avatar

You can do it easily. Either by javascript (following loading of json data), ajax or vanilla php. Which option you choose will depend on the volatility (how often changed) and amount of data you have and the expected support for js your users are expected to have (i.e. will they have js turned on?). So, give us an idea of the data and js.

hi, basically im doing a website based on exercising. so the user will select from the drop downlist an exercise i.e. Chest press. from this selection i would like to then dynamically populate some sort of text area with the description of the exercise. i used php coding within the drop down list menu to take the exercise name (chest press) out of the database and into the list menu to be selected. i just dont know how to make the users exercise name selection populate a text area with the exercise description.

example of the database

ex_identify (something to identify the exercise) i.e chest
ex_name (name of the exercise) i.e chest press
ex_description (description of the exercise)

heres the code of how i populated the list menu, if ths helps

<form id="form1" name="form1" method="post" action="">
        <select name="select" class="oneColElsCtrHdr" id="chestExercises">
        <option id="0">---Select Chest Exercise---</option>
        $getallchest = mysql_query("SELECT ex_name FROM getfit_exercises2 WHERE ex_identify = 'chest'");
        while($viewallchest = mysql_fetch_array($getallchest)){
        <option id="<?php echo $viewallchest['id']; ?>"><?php echo $viewallchest['ex_name']; ?></option>
        <?php } ?>

Member Avatar

OK, as this is pretty much a static list, you'd probably be safe in using json. This means that once the page is loaded, you won't need a round trip to the server every time you change the item in the dropdown. Do this above all the html code and the DTD - or place this in a function/include file.

$option = "";
$r = mysql_query("SELECT id, ex_name, ex_description FROM getfit_exercises2 WHERE ex_identify = 'chest'");
$firstkey = 0;
    while($d = mysql_fetch_array($r)){
        $option .= "<option value="{$d['id']}">{$d['ex_name']}</option>";
        $arr[$d['id']] = $d['ex_description'];
        if($firstkey == 0)$firstkey = $d['id']; 
    $json = json_encode($arr); 

That creates all your options in $option, so you can just paste them in the approapriate place like this:

<select id="chestselect" name="select" class="oneColElsCtrHdr" id="chestExercises">
    <?php echo $options;?>

Now the $json data needs to find itself into javascript. I find jQuery really useful as my js skills are rather limited:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
    $(document).ready(function () {
        var json = <?php echo $json;?>;
            var selIndex = $("#chestselect").val();

        function displayMe(sel){
            //now you add the replace content code after this using json
    displayMe(<?php echo $firstkey;?>);

All off top of head - so not tested. Ugly too, but should give you an idea.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.