是否有可能在ajax-php中以数组形式获得响应

时间:2015-05-06 10:34:31

标签: php jquery

我是ajax的新手,我试图获得所选项目的价格和总依赖,我试图在我的代码中执行此操作。但是出现了一个问题,是否有可能在ajax中以数组形式获得响应php。因为取决于项目,价格和总数将显示在受尊重的输入框中。有可能吗?

process.php

    <html>
    <head>
    <script src="jquery-1.11.0.js"></script>

    <script>
    $(document).ready(function(){
    $('#item').change(function(){

    var act = $('#myform').attr('action');
    var data = $('#myform').serialize();
    $.post('process.php',data, function(info){$('#price').val(info);});

    });

    });

    </script>

    </head>
    <body>

      <div class="col_w450 float_l">
                   <div id='pre'></div>

                        <h4><u>Ajax Practice</u></h4>

                        <form method="post" name="contact" id="myform" action="process.php" >

                            <label for="author">Item:</label> 
                            <select name="item"id="item">
                            <option value=''>Select</option>
                            <option value='Course1'>Course1</option>
                            <option value='Course2'>Course2</option>
                            </select>

                            <div class="cleaner_h10"></div>
                            <br>
                            <label for="email">Price:</label> 
                            <input type="text" id="price" name="price" class="validate-email required input_field" />
                            <div class="cleaner_h10"></div>
                            <br>
                            <label for="text">Quantity:</label> 

                            <select name="qty"id="qty">
                            <option value='1'>1</option>
                            <option value='2'>2</option>
                            <option value='3'>3</option>
                            <option value='4'>4</option>
                            <option value='5'>5</option>
                            </select>
                            <div class="cleaner_h10"></div>
                            <br>
                            <label for="email">Total:</label> 
                            <input type="text" id="total" name="total" class="validate-email required input_field" />
                            <div class="cleaner_h10"></div>
                            <br>

                        </form>

                    </div> 
                </div>
                </body>
                </html>

的index.php

<script>
$(document).ready(function(){
$('#item').change(function(){

var act = $('#myform').attr('action');
var data = $('#myform').serialize();
$.post('process.php',$('#myform').serialize(), function(data){

var res = $.parseJSON(data);
var response_data=JSON.stringify(res.result);
var arr=[]; 
arr = response_data.split(","); 


 alert(arr["price"]);


//$('#price')esutl.val(info);
});

});

});

</script>

修改2

try..except

1 个答案:

答案 0 :(得分:0)

用于编码数组,如process.php

$data['result'] = $arr;
echo json_encode($data);
exit;

在ajax的成功函数中尝试像parseJSON一样使用

success:function(data){
    var res = $.parseJSON(data);
    alert(res.result)
}