如何从ajax返回中选择选择框中的值

时间:2017-06-26 09:31:30

标签: php jquery html mysql

项目代码更改后,将获取当前所选的pcode(项目代码)id值,并使用ajax在另一个选择框(#acode)中列出相应的acode(活动代码)列表。

但是当我尝试从mysql列出特定值的值时,pcode列出并生成如果条件检查与mysql记录匹配,如果匹配pcode是选择,但一旦更改pcode mysql acode而不是选择acode它将列出所有acodes。

请帮忙,我想做什么。

function check(t){
    var id = t.id;
	var val = t.options[t.selectedIndex].value;
	 $.ajax({
	 type: 'post',
	 url: 'code_verify.php',
	 data: {
	  project_code:val
	 },
		 success: function (response) {
			document.getElementById("acode").innerHTML=response; 
		 }
	 });
}	
<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>

2 个答案:

答案 0 :(得分:0)

您必须使用value属性

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

答案 1 :(得分:0)

试试这段代码:

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