2016-05-02 14 views
1

我的要求是,當我點擊編輯按鈕,然後顯示彈出使用AJAX如何獲得在PHP Ajax響應,而編輯彈出

選擇所有值我送這樣我的迴應:

$data = array('cdid' => $model->cdid, 'cid' => $model->cid, 'icdcode' => $model->icdcode, 'category' => $model->category); 
echo json_encode($data); 

我得到的迴應也是這樣的警告框

{"cdid":2,"cid":6,"icdcode":"6","category":2} 

但問題是如何使用這些數據顯示在彈出窗口上選擇checbox或文本框的值,下拉列表值。 enter image description here

+0

操縱'form'根據返回的值... – Rayon

+0

你能舉幾個例子... –

+0

你能分享一個撥弄着玩? – Rayon

回答

1

好,可以有兩種情況,即

方案1,你有你的彈出窗口的file.php值數組,

PHP:

/* If you have json in your popup's file.php 
    You needs to convert the json into php array. 
*/ 
$values_array = json_decode($json_string, true); 

/* Initializing default values to the variables that will be used to make selections*/ 

$check_radio = $check_select = ""; 
if(is_array($values_array)){ 
    // supposing, the category is for radio buttons 
    $check_radio = $values_array['category']; 
    // supposing, the icdcode is for select box 
    $check_select = $values_array['icdcode']; 
} 

HTML:

<input type="radio" name="category" value="ICD-10" <?php echo ($check_radio=='ICD-10') ? " checked='checked'":'';?>> ICD-10<br> 
<input type="radio" name="category" value="ICD-9" <?php echo ($check_radio=='ICD-9') ? " checked='checked'":'';?>> ICD-9<br> 

<select id="icd-code" name="icd-code" multiple> 
    <option value="1001" <?php echo ($check_select=='1001') ? " selected='selected'":'';?>>1001</option> 
    <option value="1002" <?php echo ($check_select=='1002') ? " selected='selected'":'';?>>1002</option> 
</select> 

方案2,您不必在彈出的file.php值或JSON數組的PHP數組,但使用Javascript JSON對象:

// Assuming that you have JSON data like this 
// var data = {"cdid":2,"cid":6,"icdcode":"6","category":2}; 

// So, you will need to Parse Json like this: 
// https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse 
var parsedData = JSON.parse(data); 

// Making Checked the Radio buttons by name `category` and value 
$("input[name=category][value='"+parsedData.category+"']").prop("checked",true); 

//Making select options selected by option value 
$('#icd-code option[value=' + parsedData.icdcode + ']').attr('selected', true); 

希望,它會幫助你理清這種情況:)

+0

謝謝@hmd你的ans對我有幫助.. –

+0

@RahulPawar如果你認爲它對你或者某種正確的答案有幫助,你可以標記是正確的:) – hmd

0

嘿,你可以使用下面的代碼來獲得在模型箱Ajax響應

的index.php

<!DOCTYPE html> 
     <html lang="en"> 
     <head> 
      <title>Bootstrap Example</title> 
      <meta charset="utf-8"> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"> 
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script> 
      <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script> 

     </head> 
     <script type="text/javascript"> 
     $(document).ready(function() { 

      $("#button").click(function(){ 

      $.ajax({ 
       url: "ajax.php", 
       success: 
       function(result){ 

       $("#div1").html(result); 

      }}); 
     }); 
     }); 

     </script> 
     </head> 
     <body> 

     <div class="container"> 
      <h2>Modal Example</h2> 
      <!-- Trigger the modal with a button --> 
      <button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">Open Modal</button> 

      <!-- Modal --> 
      <div class="modal fade" id="myModal" role="dialog"> 
      <div class="modal-dialog"> 

       <!-- Modal content--> 
       <div class="modal-content"> 
       <div class="modal-header"> 
        <button type="button" class="close" data-dismiss="modal">&times;</button> 
        <h4 class="modal-title">Modal Header</h4> 
       </div> 
       <div class="modal-body"> 
     <button id="button">submit</button> 

     <div id="div1"></div> 
       </div> 
       <div class="modal-footer"> 
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> 
       </div> 
       </div> 

      </div> 
      </div> 

     </div> 

     </body> 
     </html> 

ajax.php

<?php 

    $array = array("ram", "ram", "ram", "vish"); 

    echo json_encode($array,JSON_FORCE_OBJECT); 
    ?>