Hi friends

i am trying to display dynamic dropdown using php. but it is not fetching the second dropdown value. I collected the below code from internet.

please help me to fix it.

<?php

    $dbc = mysqli_connect("localhost","root","escape123","tsheet");

    // Check connection
    if (mysqli_connect_errno())
        {
            echo "Failed to connect to MySQL: " . mysqli_connect_error();
        }

    $tAOptions='';  $cNOptions=''; $pNameoptions='';
    /*Task Drop Down*/

    $tAsk = "select * from ce_task order by tas_id asc";
    $rowtAsk = mysqli_query($dbc, $tAsk);

    while ($tARow = mysqli_fetch_array($rowtAsk)) 
    {
        $tAOptions .="<option value=\"".$tARow['tas_name']."\">" . $tARow['tas_name'] . "</option>";
    }
    $tADropDown =   "<select name='Task' id='Task'>
                    <option selected='selected' disabled='disabled' value=''>Select Task</option>
                    " . $tAOptions . "
                    </select></br>";


    /*Clent Name*/

    $cName = "select distinct(client_name) from ce_client order by client_id asc";
    $rowcName = mysqli_query($dbc, $cName);

    while ($CNRow = mysqli_fetch_array($rowcName)) 
    {
        $cNOptions .="<option value=\"".$CNRow['client_name']."\">" . $CNRow['client_name'] . "</option>";
    }
    $clentName  =   "<select name='clentName' id='clentName' onChange='getclientName(this)'>
                    <option selected='selected' disabled='disabled' value=''>Select Client Name</option>
                    " . $cNOptions . "
                    </select></br>";
    /*Project Name*/
    $projectName = "<select name='projectName' id='projectName'>
                                <option class='toggle_control' selected='selected' disabled='disabled' value=''>Select Type</option>
                                " . $pNameoptions . "
                            </select></br>";
?>
<script src="http://code.jquery.com/jquery-1.6.1.js" type="text/javascript"></script>
<script type="text/javascript">
    function getclientName(sel) {
        var pid = $(sel).val();
        alert(pid);
        $.ajax({
        type: "POST",
        url: "getprojectName.php",
        data:"client_name="+pid,

        success: function(data){
                alert(data);
                 $("#projectName").html(data);
                //$("#selectedComponentType").html(data);
        }
        });
    }

    $(function() {
        var $timeTB = $("#time_tb"),
            $firstTRCopy = $timeTB.children('tr').first().clone();
        $("#addRows").click(function() {
            $timeTB.append($firstTRCopy.clone());
        });
    });
</script>
<table id="time_table">
    <thead>
        <tr>
            <th>Client Name</th>
            <th>Project Name</th>
            <th>Task</th>
        </tr>
    </thead>
    <tbody id="time_tb">
        <tr>
            <td><?php echo $clentName; ?></td>
            <td><?php echo $projectName; ?></td> 
            <td><?php echo $tADropDown;?></td>
        </tr>
    </tbody>
</table>
<input type="button" value="+" id="addRows" />

getprojectName.php

$dbc = mysqli_connect("localhost","root","escape123","tsheet");

    // Check connection
    if (mysqli_connect_errno())
      {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
      }

    $pNameoptions = '';
    echo $cliName = $_POST['client_name'];

    $pNameQuery = "select * from ce_client where client_name='$cliName'";
    $pNamerow = mysqli_query($dbc, $pNameQuery);

    while ($pNRow = mysqli_fetch_array($pNamerow)) {
         $pNameoptions .="<option value=\"".$pNRow['client_project']."\">" . $pNRow['client_project'] . "</option>";
    }

    $projectName = "<select name='projectName' id='projectName'>
                                <option class='toggle_control' selected='selected' disabled='disabled' value=''>Select Type</option>
                                " . $pNameoptions . "
                            </select></br>";
Member Avatar for diafol

Many things here rpv. Here's the one that stood out:

WHy is your ce_client (project name) table holding client_name instead of client_id as a foreign key?

I started fixing your code until I got to the last snippet, then I saw that and stopped.

Thanks

I have all the thing in one table.

if possible can you please suggest your idea.

Member Avatar for diafol

Can you show the schema of your tables?

CREATE TABLE IF NOT EXISTS ce_client (
client_id int(11) NOT NULL,
client_name varchar(150) NOT NULL,
client_project varchar(150) NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=285 DEFAULT CHARSET=latin1;

values
INSERT INTO ce_client (client_id, client_name, client_project) VALUES
(14, 'ANE', '001 Implementation'),
(15, 'ANE', '002 Non-Billable'),
(16, 'XXX', '001 Client Holiday'),
(17, 'XXX', '002 001 Production Support'),
(18, 'XXX', '003 Implementation'),
(19, 'XXX', '004 Non-Billable'),

Member Avatar for diafol

OK, I see a few issues here already. What about ce_task?

Its another table it was not dependend one.1 and 2 are dependent.

CREATE TABLE IF NOT EXISTS ce_task (
tas_id int(11) NOT NULL,
tas_name varchar(150) NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=23 DEFAULT CHARSET=latin1;

values

INSERT INTO ce_task (tas_id, tas_name) VALUES
(1, 'Adm - Administrative\r\n'),
(2, 'Bill - Billing\r\n'),
(3, 'CNC001 - CNC\r\n'),
(4, 'DBA - VCP - DBA\r\n'),
(5, 'DBA001 - Development\r\n'),
(6, 'FUN - VCP - functional\r\n'),
(7, 'FUN001 - Functional\r\n'),
(8, 'HOL001 - Holiday\r\n'),
(9, 'IMP - Implementation\r\n'),
(10, 'Mktg - Marketing\r\n'),
(11, 'Mtgs - Meetings\r\n'),
(12, 'NewBiz - New Business\r\n'),
(13, 'PMT001 - Project Management\r\n'),
(14, 'Pres - Presentations\r\n'),
(15, 'Prop - Proposals\r\n'),
(16, 'QA001 - Quality Assurance\r\n'),
(17, 'Rsrch - Research\r\n'),
(18, 'TCH001 - Technical\r\n'),
(19, 'TES001 - Tech Support\r\n'),
(20, 'Trvl - Travel\r\n'),
(21, 'UNA001 - Unassigned\r\n'),
(22, 'VCP - VCP - Technical\r\n');

Member Avatar for diafol

THis doesn't make much sense to me.

Your client / project table has client_id, client_name and client_project

To what does the client_id refer? The client or the project?

To my mind you need a client table (client_id, client_name), and a projects table (having a project_id, project_name, client_id FK).

The 'client_project' field looks as though it is made from more than one discrete bits of data, which breaks 1NF (1st normal form).

Tasks table is beyond me - I can't see where or how that fits in with anything.

thanks.

Be a part of the DaniWeb community

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