項目代碼改變時,它會得到當前選中的pcode(Project code)id值並在另一個選擇框(#acode)中列出相應的acode(Activity code)使用ajax。如何從ajax中選擇選擇框中的值
但是,當我嘗試列出從MySQL的特定值的值,pcode是列出並使如果條件檢查與mysql記錄匹配的pcode是選擇,但一旦更改pcode mysql acode而不是選擇acode它將列出所有acodes。
請幫助,我想要做什麼。
function check(t){
var id = t.id;
\t var val = t.options[t.selectedIndex].value;
\t $.ajax({
\t type: 'post',
\t url: 'code_verify.php',
\t data: {
\t project_code:val
\t },
\t \t success: function (response) {
\t \t \t document.getElementById("acode").innerHTML=response;
\t \t }
\t });
} \t
<p>My Database Name : timesheet</p>
<p>Table: projectcode</p>
<table width="250" border="1" cellspacing="0" cellpadding="0">
<tr>
<td align="center">id</td>
<td align="center">project</td>
<td align="center">pcode</td>Pcode
</tr>
<tr>
<td align="center">1</td>
<td>Library Time</td>
<td align="center">LB</td>
</tr>
<tr>
<td align="center">2</td>
<td>Leave</td>
<td align="center">L</td>
</tr>
</table>
<p>Table: activitycode</p>
<table width="250" border="1" cellspacing="0" cellpadding="0">
<tr>
<td align="center">id</td>
<td align="center">pcode</td>
<td align="center">activity</td>
<td align="center">acode</td>
Pcode </tr>
<tr>
<td align="center">1</td>
<td align="center">1</td>
<td align="center">Bench</td>
<td align="center">B</td>
</tr>
<tr>
<td align="center">2</td>
<td align="center">2</td>
<td align="center">Sick Leave</td>
<td align="center">Sl</td>
</tr>
<tr>
<td align="center">3</td>
<td align="center">2</td>
<td align="center">Causal Leave</td>
<td align="center">CL</td>
</tr>
<tr>
<td align="center">4</td>
<td align="center">2</td>
<td align="center">Loss of Pay</td>
<td align="center">LP</td>
</tr>
</table>
<p>Table: user_timesheet</p>
<table width="250" border="1" cellspacing="0" cellpadding="0">
<tr>
<td align="center">id</td>
<td align="center">userid</td>
<td align="center">pcode</td>
<td align="center">acode</td>
</tr>
<tr>
<td align="center">1</td>
<td align="center">1</td>
<td align="center">1</td>
<td align="center">1</td>
</tr>
<tr>
<td align="center">2</td>
<td align="center">1</td>
<td align="center">2</td>
<td align="center">2</td>
</tr>
<tr>
<td align="center" bgcolor="#666666">3</td>
<td align="center" bgcolor="#666666">1</td>
<td align="center" bgcolor="#666666">2</td>
<td align="center" bgcolor="#666666">3</td>
</tr>
<tr>
<td align="center">4</td>
<td align="center">1</td>
<td align="center">2</td>
<td align="center">4</td>
</tr>
</table>
<p></p>
<p> </p>
<p> </p>
<table width="250" border="1" cellspacing="0" cellpadding="0">
<tr>
<td>
<select name="pcode_1" id="1" class="pcode" onChange="check(this);">
<?php
$sql=mysql_query("select * from user_timesheet where id=3");
while($row=mysql_fetch_array($sql)){
$leave=mysql_query("SELECT * FROM projectcode"); while($leave1=mysql_fetch_array($leave)) { ?>
<option value="<?=$leave1['id']?>" <?php if($leave1['id']==$row['pcode']){ echo "selected";}?>> <?=$leave1['pcode']?> </option>
<?php } } ?>
</select></td>
<td><select name="acode" id="acode"></select></td>
</tr>
</table>
<?php
$project_code = $_POST['project_code'];
$find=mysql_query("select * from activitycode where pcode=$project_code");
while($row=mysql_fetch_array($find))
{
echo "<option value='$row[id]'>".$row['acode']."</option>";
}
?>
<p> </p>
<p>i am trying to list the record as below query <br/><br/>
" select * from user_timesheet where id=3 " </p>
<p> </p>
<strong><u> Output: (currently coming)</u></strong><br/><br/>
//While change Project code , ajax(using project code ID) return all activity code corresponding to pcode from activitycode table,
<br/><br/>
Then list record with user_timesheet where id=3, need to select automatic corrponding ID, pcode and acode wanna to select.
<table width="250" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>Project Code :
<select name="pcode_1" id="1" class="pcode" onChange="check(this);">
<option value="1">Library Time </option>
<option value="2" selected="selected">Leave</option>
</select></td>
<td>Activity Code:
<select name="acode" id="acode">
<option value="1">Sick Leave</option>
<option value="2">Causal Leave</option>
<option value="3">Loss of Pay</option>
</select></td>
</tr>
</table>
<br/><br/>
<strong><u> Expecting Output: </u></strong><br/><br/>
<table width="250" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>Project Code :
<select name="pcode_1" id="1" onChange="check(this);">
<option value="1">Library Time </option>
<option value="2" selected="selected">Leave</option>
</select></td>
<td>
Activity Code:
<select name="acode" id="acode">
<option value="1">Sick Leave</option>
<option value="2" selected="selected">Causal Leave</option>
<option value="3">Loss of Pay</option>
</select></td>
</tr>
</table>
問題是不明確的。請首先閱讀Stack Overflow問題指南。 –
什麼是問題,是你解決這個錯誤的問題。「$ row ['acode']。」「;}?>? –