Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从php返回的对象填充表单字段的Ajax调用_Php_Jquery_Ajax - Fatal编程技术网

从php返回的对象填充表单字段的Ajax调用

从php返回的对象填充表单字段的Ajax调用,php,jquery,ajax,Php,Jquery,Ajax,首先,我使用$.getJSON()使用GET请求发送用户ID Javascrit部分: $.getJSON("this_file.php?id=1", null, function(data){ $('input#id').val(data.id); $('input#username').val(data.username); $('input#first_name').val(data.first_nam

首先,我使用
$.getJSON()
使用GET请求发送用户ID

Javascrit部分:

        $.getJSON("this_file.php?id=1", null, function(data){
            $('input#id').val(data.id);
            $('input#username').val(data.username);
            $('input#first_name').val(data.first_name);
        });
当通过GET请求显示用户ID时,我运行一个函数来返回一个包含用户详细信息的对象

PHP部分(此_文件.PHP):


以下是要填充的表单字段

HTML格式:

<input type="text" value="" name="id" id="id">
<input type="text" value="" name="username" id="username">
<input type="text" value="" name="first_name" id="first_name">

在浏览器控制台中,GET请求的响应按预期返回:

{“id”:“1”,“username”:“bobu”,“first_name”:“bob”}


但是,这些值没有显示在表单上,如何实现这种方法?

我会使用javascript,但如果您不打算:

改变

<input type="text" value="" name="id" id="id">
<input type="text" value="" name="username" id="username">
<input type="text" value="" name="first_name" id="first_name">
或者试试:

<input type="text" value="<?php echo $id; ?>" name="id" id="id">
<input type="text" value="<?php echo $username; ?>" name="username" id="username">
<input type="text" value="<?php echo $first_name; ?>" name="first_name" id="first_name">

您在javascript控制台中是否收到任何错误消息?根本没有,它成功返回了预期的数据,它只是不更新表单字段您不需要使用输入#id,只需使用
$('#id').val(data.id)我已经听从了你的建议,但我相信这不是问题的原因。响应头中是否返回了content-type:application/json?如果不是,我会尝试在PHP端显式设置它。
<input type="text" value="<?php if($sentVar==1){echo $id;} ?>" name="id" id="id">
<input type="text" value="<?php if($sentVar==1){echo $username;} ?>" name="username" id="username">
<input type="text" value="<?php if($sentVar==1){echo $first_name;} ?>" name="first_name" id="first_name">
$sentVar=1;
<input type="text" value="<?php echo $id; ?>" name="id" id="id">
<input type="text" value="<?php echo $username; ?>" name="username" id="username">
<input type="text" value="<?php echo $first_name; ?>" name="first_name" id="first_name">