PHP 使用 getJSON 实例 ajax
Client 端实现:
<script type="text/javascript">
function one()
{
var keyid = $("#keyid").find("option:selected").val();
var url ="http://localhost/index.php?keyid="+keyid;
var node ="<option value=\'0\'>选择……</option>";
$.getJSON(url,function(data){
for(i=0;i<data.length;i++)
{
node+="<option value=\'"+data[i].id+"\'>"+data[i].name+"</option>";
}
$("#keywords").html(node);
});
}
</script>
<html>
<tr>
<th>关键字ID:</th>
<td>
<select name="keyid" id="keyid" onchange="one()">
<option value='0'>选择……</option>
<option value='1'>111</option>
<option value='2'>222</option>
</select>
</td>
</tr>
<tr>
<th>关键字:</th>
<td>
<select name="keywords" id="keywords">
<option value='0'>选择……</option>
</select>
</td>
</tr>
</html>
Server端实现:
public static function returnkeywords()
{
try
{
$keyid = $_REQUEST ['keyid'];
$result=array();
$i=0;
if ($keyid)
{
$list=getkeyword_by_id($keyid);
foreach ($list as $row)
{
$data['id']=$row['id'];
$data['keywords']=mb_convert_encoding($row['keywords'],'utf-8','gbk');
$result[$i]=$data;
$i=$i+1;
}
}
$res=json_encode($result);
echo $res;
}
catch (Exception $e)
{
}
}