2017-06-26 58 views
-1

项目代码改变时,它会得到当前选中的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>&nbsp;</p> 
 
<p>&nbsp; </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>&nbsp; </p> 
 
<p>i am trying to list the record as below query <br/><br/> 
 

 
" select * from user_timesheet where id=3 " </p> 
 
<p>&nbsp;</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>

+0

问题是不明确的。请首先阅读Stack Overflow问题指南。 –

+0

什么是问题,是你解决这个错误的问题。“$ row ['acode']。”“;}?>? –

回答

0

您必须使用value财产

document.getElementById("acode").value = response; 
+0

谢谢.......... –

0

试试这个代码:

$('acode').val(response); 
+0

谢谢你的工作 –

+0

Please upvote and accept相同的见此https://stackoverflow.com/help/someone-answers – Rahul