使用ajax请求新页面并在新页面中打开它

时间:2011-07-02 08:59:20

标签: php jquery codeigniter

我在尝试使用JQuery Ajax从CodeIgniter请求新页面并在新的空白页面中打开它时遇到问题

以下是当前代码正常工作的示例方案,

1。)用户通过复选框选择客户的收据

<input type='checkbox' class='customer_id' value='<?php echo $entry->customer_id;?>'>

2.。)Javascript读取客户ID然后将这些数据发送到CodeIgniter。一旦收到返回数据,就会触发回调函数来显示目标div

var data;
var counter = 1;
$(.customer_id:checked).each(function(){
    data['customer_' + counter] = $(this).val();
})    
$.post('finance/getReceipt', data, function(){
    $('#some_target_div').html(data);
}

3.。)CodeIgniter从db检索客户数据并生成页面

function getReceipt(){
    $counter = 1;
    while ($this->input->post('customer_' + $counter)){
        $where['customer_id'] = $this->input->post('customer_' + $counter);
        $data += $this->getCustomerReciept($where);
    }
    $this->load->page('finance/receipt', $data);
}

但现在我需要转换此代码,使其打开一个带打印按钮的新空白页

这样做的目的是因为我想生成一个格式化的收据并在带有打印按钮的新空白页面中显示。我知道有几个插件我可以打印div但这是我的客户的请求。所以,这对我来说不是一个解决方案。

任何建议或评论都会很棒。提前谢谢。

1 个答案:

答案 0 :(得分:2)

我认为最简单的方法是创建辅助form以将数据发布到新页面而不是使用$.post所以,这将是代码:

var data;
var counter = 1;
$(.customer_id:checked).each(function(){
    data['customer_' + counter] = $(this).val();
});
$('<form action="finance/getReceipt" method="POST" target="_blank" style="display:none">' +
'<input type="hidden" name="data1" value="value-data1" />'
).appendTo("body").submit().remove();

只需将您的data1参数更改为您想要的参数,或者根据需要添加更多参数。 就是这样。神奇之处在于为表单使用_blank目标。

希望这会有所帮助。干杯

PS:另一种方法是使用window.open创建一个新窗口,并使用javascript分配其内容。