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>";