2015-02-08 84 views
2

我有一個JSON從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

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(); 
    } 

添加PHP函數謝謝。

+1

我覺得它吸引的時候,你調用'.modal(「秀」)',並將其複製到一個單獨的DIV的HTML,所以你的HTML的變化沒有任何效果,因爲它們發生後的模式已經打開。嘗試將'$('#view-contact-activity')。modal('show');'移到ajax成功回調的底部。 – 2015-02-08 04:47:18

+0

@MattBrowne我試過了,沒有改變。我關閉ajax請求後放置。 – EDarrien 2015-02-08 04:56:40

回答

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); 
     } 
    }); 
}); 
+0

試過了。一無所有。當我輸入(response.id或response.activity_type)時,(id)突出顯示,但activity_type不顯示。不知道這是否有所作爲。 – EDarrien 2015-02-08 05:03:24

+0

當你把console.log(response);你的.Success之後? – Scottie 2015-02-08 05:04:41

+0

in firebug(html)我正確地得到數組。 [{「id」:「12」,「activity_type」:「meeting」,「activity_title」:「meeting」,「activity_details」:「ds」,「activity_status」:「」} – EDarrien 2015-02-08 05:13:30

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); 
      } 
     }); 
    }); 
}); 
+0

太棒了!真高興你做到了! – Scottie 2015-02-13 15:00:35