doudeng2057
2013-04-21 21:13
浏览 49
已采纳

jQuery - 如何将数组从PHP发送到html文本框

I want to autofill the user form when a user is selected. How can I pass through an array from php to javacript/jquery and then put the values into the correct textboxes?

$("#client").change(function() {
     $.get("../jquery/update_client.php?id=" + $("#client").val(),function(data){
          $("#first").val(data);
     });
});

PHP file:

if(isset($_GET['id'])){
include('../db_connect.php');
$id = mysqli_real_escape_string($mysqli, $_GET['id']);
$query = "SELECT * FROM users WHERE iduser=$id LIMIT 1";
$result = $mysqli->query($query);
$row = $result->fetch_array();
echo $row[];
}

How can I capture the array in javacript/jquery and then parse the information to go into the right boxes?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • dsgdsf12312 2013-04-21 21:16
    已采纳

    In PHP;

    echo json_encode($row);
    

    In JavaScript you will now get a map (array) back as data. This map you can then loop to fill your textbox(es).

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dousi8237 2013-04-21 21:27

    Maybe if you output JSON it's better to use getJSON method with a simple for loop to set corresponding fields values:

    $.getJSON("../jquery/update_client.php?id=" + $("#client").val(), function(data) {
        for (var el in data) {
            $('[name="' + el + '"]').val(data[el]);
        };
    });
    

    http://jsfiddle.net/2LuuC/

    评论
    解决 无用
    打赏 举报
  • dongmeng2509 2013-04-21 21:57

    well in php output the data as json. that is use

    echo (json_encode($data));
    

    then as dfsq said

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题