简体   繁体   中英

How to get an array by JSON and how to set this return value to a modal form?

My Ajax code:

$("a#edit").click(function(){
    var id = $(this).closest('tr').attr('id');
    //alert(id);
    $.ajax({
        url: 'getdata.php',
        type: "POST",
        dataType:'JSON',
        data: {
            id: id,
        },
        success:function(result){
            alert(result);
        }
    });
});

My php code:

if ($_REQUEST['id'] != "") 
{
    $id=$_REQUEST['id'];
    $sql = "select * from visit_reports WHERE visit_planner_id='$id'";

    $query = sqlsrv_query( $link, $sql);
    while($data = sqlsrv_fetch_array($query,SQLSRV_FETCH_ASSOC))
    {
        print_r($data);
    }
}

In Firebug, the array I get:

Array
(
    [id] => 1.0000
    [visit_planner_id] => 230338
    [bi_staff_present_name] => BI staff present name
    [bi_staff_trial_function] => BI staff trial function
)

How can I use this array value into my specific input field of modal form?

In your PHP code, Why not print JSON instead of array?

echo json_encode($data);

Now, you can parse it easily from your ajax code.

success:function(result){
       var json = $.parseJSON(result);
       var id = json.id; // 1.0000
       var visit_planner_id= json.visit_planner_id; // 230338
       // so on..
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM