如何在编辑弹出窗口时获取php中的ajax响应

时间:2016-05-02 06:51:40

标签: javascript php jquery ajax

我的要求是当我点击编辑按钮然后使用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

2 个答案:

答案 0 :(得分:1)

嗯,可能有两种情况,即

场景1,弹出式文件中有一系列值。文件

<强> 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中的php数组或json数组,但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);

希望它可以帮助您理清情况:)

答案 1 :(得分: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);
    ?>