将json放入模态中

时间:2015-02-08 04:42:12

标签: javascript jquery json twitter-bootstrap

我有一个来自pho函数的JSON返回数据,如下所示:

[{"id":"15","activity_type":"call","activity_title":"Call to check "}]

这里是发起请求的脚本(actvitiy.js)(编辑)

$(document).on("click", ".view_contact_activity", function () {

        var this_activity_id = $(this).closest('.feeds').find('#this_activity_id').val();
        $('#view-contact-activity').modal('show');
    $.ajax({

        url: '../includes/functions/contact-functions.php',
        data: {view_activity_id:this_activity_id},
        dataType:'json',
        Success: function(response){
        $('#activity_id').val(response[0].id);
        $('#activity_type').val(response[0].activity_type);

        }
            });
});

我需要显示值的模态:

<div class="modal fade" id="view-contact-activity" tabindex="-1" role="basic" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
             <div class="modal-body">
              <div class="portlet-body form">
              <form class="form-horizontal" role="form" id="view-contact-activity-form" method="post">

                  <div class="form-group">
                   <label class="col-sm-3 control-label col-lg-3"> Activity Title</label>
                      <input type="text" name="activity_id" id="activity_id" value="">
                   <label class="col-sm-3 control-label col-lg-3"> Activity Type</label>
                        <input type="text" name="activity_type" id="activity_type" value="">                                                
                  <div class="modal-footer">
                      <div class="col-lg-offset-2 col-lg-10">
                          <button type="submit" name="create-new-account" class="btn btn-danger" id="edit">Edit Activity</button>
                      </div>
                  </div>
              </form>
          </div>
          </div>
        </div>
    </div>
</div>

模态显示但没有数据传递到模态。任何想法我可能在这里做错了。

编辑:添加返回JSON的PHP函数

function view_activity(){

        global $connection;

        $activity_id = $_POST['view_activity_id'];
        $get = "SELECT * FROM contact_activities WHERE activity_id = '$activity_id' "
                                    or die("Error: ".mysqli_error($connection));
        $query = mysqli_query($connection, $get);
        $activitiy_field = array();

        while ($activity_array = mysqli_fetch_array($query)){

            $activity = array(
                'id' => $activity_array['activity_id'],
                'activity_type' => $activity_array['activity_type'],
                'activity_title'=>$activity_array['activity_title'],
                'activity_details'=>$activity_array['activity_details'],
                'activity_status'=>$activity_array['activity_status'],
                'activity_details'=>$activity_array['activity_details'],
                'activity_details'=>$activity_array['activity_details'],

                );

        $activitiy_field[] = $activity;

        }
                echo json_encode($activitiy_field);
    }

    if (isset($_POST['view_activity_id'])) {
        view_activity();
    }

谢谢。

2 个答案:

答案 0 :(得分:1)

使用.val()代替.html()。

$('#activity_id').val(response.id);
$('#activity_type').val(response.activity_type); 

修改:你的javascript应该是这样的:

$(document).on("click", ".view_contact_activity", function () {

    var this_activity_id = $(this).closest('.feeds').find('#this_activity_id').val();

    $('#view-contact-activity').modal('show');

    $.ajax({
        url: '../includes/functions/contact-functions.php',
        data: {view_activity_id:this_activity_id},
        dataType:'json',
        Success: function(response){
            $('#activity_id').val(response[0].id);
            $('#activity_type').val(response[0].activity_type); 
        }
    });
});

答案 1 :(得分:0)

挖掘多个网站后。我发现了一个类似的问题,可以提供帮助。

$(document).on("click", ".view_contact_activity", function () {

        var this_activity_id = $(this).closest('.feeds').find('.id #this_activity_id').val();
    $('#view-contact-activity').modal('show')

    $('#view-contact-activity').on('show.bs.modal', function() { 
        $modal = $(this);
        $.ajax({
           url: '../includes/functions/contact-functions.php',
           data: {view_activity_id:this_activity_id},
           dataType:'json',
           success: function(response){
               // Find the elements in the modal
               $modal.find('#activity_id').val(response[0].id);
               $modal.find('#activity_type').val(response[0].activity_type);
           }
        });
    });
});