Php Jquery-打开包含当前页面内容的新窗口

Php Jquery-打开包含当前页面内容的新窗口,php,jquery,Php,Jquery,我试图创建一个“打印”按钮来打开一个新窗口,并在其中显示一个PHP变量 下面的代码在PHP脚本中循环的次数与票证的循环次数一样多,但是我似乎无法在窗口打开时获得要显示的正确数字(打印链接中显示的数字是正确的,但当新窗口打开时它是不正确的) jQuery(函数($){ $('a.new-window')。单击(函数(){ var recipe=window.open(“”,'PrintWindow','width=600,height=600'); var html='Print Your Ti

我试图创建一个“打印”按钮来打开一个新窗口,并在其中显示一个PHP变量

下面的代码在PHP脚本中循环的次数与票证的循环次数一样多,但是我似乎无法在窗口打开时获得要显示的正确数字(打印链接中显示的数字是正确的,但当新窗口打开时它是不正确的)


jQuery(函数($){
$('a.new-window')。单击(函数(){
var recipe=window.open(“”,'PrintWindow','width=600,height=600');
var html='Print Your Ticket'+$('').append($('#Ticket').clone()).html()+'';
recipe.document.open();
配方。文档。编写(html);
recipe.document.close();
返回false;
});
});

为什么不试试这样的方法:

<a href="#" class="new-window">Print <?php echo $EM_Booking->get_spaces() ?>
    <div class="ticket" style="display:none">
        <?php echo $EM_Booking->get_spaces() ?>
    </div>
</a> 


<script>
jQuery(function ($) {

    $('a.new-window').click(function () {
        var btn = $(this),
            ticket = btn.find('.ticket').html(),            
            recipe =  window.open('','PrintWindow','width=600,height=600'),
            html = '<html><head><title>Print Your Ticket</title></head><body><div id="myprintticket">' + ticket + '</div></body></html>';
        recipe.document.open();
        recipe.document.write(html);
        recipe.document.close();
        return false;
    });

});
</script>

jQuery(函数($){
$('a.new-window')。单击(函数(){
var btn=$(此),
ticket=btn.find('.ticket').html(),
配方=窗口。打开(“”,'PrintWindow','width=600,height=600'),
html='打印您的票证'+票证+'';
recipe.document.open();
配方。文档。编写(html);
recipe.document.close();
返回false;
});
});

但更好的解决方案是为按钮提供一个唯一的ID,然后单击服务器上传递该ID的现有(php生成的)页面,例如,
/getBooking.php?ID=123
,该页面将输出所需的任何内容。

如果循环多次,您将有许多#ticket元素。html元素的ID必须是唯一的。您好,这非常有效!非常感谢您提供了这样一个快速的解决方案:)这只是另一个快速的问题-当我将任何HTML(ie)放入其中一个PHP回音时,它会破坏脚本并显示“未定义”,我该如何清理内容以避免出现这种情况?@Meakin我不确定我是否理解这个问题。请您发布一些代码,例如,使用破坏您脚本的html?(或者可能开始一个新问题,因为这个问题似乎与当前问题无关)
<a href="#" class="new-window">Print <?php echo $EM_Booking->get_spaces() ?>
    <div class="ticket" style="display:none">
        <?php echo $EM_Booking->get_spaces() ?>
    </div>
</a> 


<script>
jQuery(function ($) {

    $('a.new-window').click(function () {
        var btn = $(this),
            ticket = btn.find('.ticket').html(),            
            recipe =  window.open('','PrintWindow','width=600,height=600'),
            html = '<html><head><title>Print Your Ticket</title></head><body><div id="myprintticket">' + ticket + '</div></body></html>';
        recipe.document.open();
        recipe.document.write(html);
        recipe.document.close();
        return false;
    });

});
</script>