ds3422222 2017-02-02 20:38
浏览 56
已采纳

如何刷新单独的页面以获取PHP会话变量?

Before I start, I need you to know that my methods aren't the best. I'm a beginner to JavaScript and php and I'm sure that what I'm trying to accomplish can be accomplished by much better and simpler means.

I think my problem lies with session variables and printing pages. I have a page where a user creates tables from dropdown lists. I need to print the tables but on three different pages and in three different formats. I have the three pages linked to print using an the "frames["iframe].print" function.

Here are my frames:

<iframe src="customer_detail.php" style="display:none;"name="framed"></iframe>
<iframe src="advisor_copy.php" style="display:none;" name="framez"></iframe>
<iframe src="customer_copy.php" style="display:none;" name="frame"></iframe>

And my buttons:

<span id = "cCopy"><button class = "button" onclick= "frames['frame'].print();"/> Customer Copy</button></span>
<span id = "aCopy"><button class = "button" onclick="frames['framez'].print();" value="Advisor_Copy">Advisor Copy</button>    </span>
<span id = "cCopy"><button class ="button" onclick= "frames['framed'].print();" value="customer_copy">Customer Detail </button></span>

From what I understand, the page needs to be refreshed in order for the session variables to update. My hack solution which works is to refresh the 3 pages I'm printing(customer_detail, customer_copy and advisor_copy) every 1 second. What I'd much rather have is the ability to refresh the page I'm printing when I click one of the three buttons so I can update the session variables being called on those pages.

I've tried refreshing the iframe with an onclick event but it doesn't do exactly what I need it to. I've searched around for various solutions and none of them have really worked. If I need to restructure my code then I will. I think what I really should do is pass the variables I need to the other pages via an AJAX call. I'd like to know if there's a solution which will allow my code to function without having to more or less start from scratch.

  • 写回答

2条回答 默认 最新

  • duanjieyi6582 2017-02-02 21:03
    关注

    Ok. This is a ugly workaround without rewrite all.

    <span id = "cCopy"><button class = "button"><a href="customer_detail.php" target="framed">Customer Copy</a></button>    </span>
    <span id = "aCopy"><button class = "button" value="Advisor_Copy"><a href="advisor_copy.php" target="framez">Advisor Copy</a></button>    </span>
    <span id = "cCopy"><button class ="button" value="customer_copy"><a href="customer_copy.php" target="frame">Customer Detail</a> </button></span>
    
    <br>
    <iframe name="framed"></iframe>
    <iframe name="framez"></iframe>
    <iframe name="frame"></iframe>
    

    WORKING EXAMPLE


    UPDATE

    ...and on single PHP pages, on body tag put... <body onload="window.print()">

    UPDATE 2

    and if you want to remove underline button text add

    style="text-decoration:none;" 
    

    on each <a tag

    UPDATE 3 (it is necessary to carry some tests, due to the browsers security locks)

    Your index.html:

    <span id = "cCopy"><button onclick="printIframe('framed')" class = "button">Customer Copy</button></span>
    <span id = "aCopy"><button onclick="printIframe('framez')" class = "button" value="Advisor_Copy">
    Advisor Copy</button></span>
    <span id = "cCopy"><button onclick="printIframe('frame')" class ="button" value="customer_copy">Customer Detail</button></span>
    
    <br>
    <div id='frames'>
    </div>
    
    <script>
    var iframes = {}; //create a 'container' for iframe
    iframes['framed'] = 'customer_detail.php'; // your link
    iframes['framez'] = 'advisor_copy.php'; // your link
    iframes['frame'] = 'customer_copy.php'; // your link
    
    // if you want to add another iframe create a new line with  iframes['myIframeName'] = 'MyIframeUrlLink'; // your link
    
    for (var k in iframes){
    createIframe(k, iframes[k]);
    }
    
    
    function printIframe(id) //this call a javascript print function inside your PHP - need for security reason
    {
        var iframe = document.frames ? document.frames[id] : document.getElementById(id);
        var ifWin = iframe.contentWindow || iframe;
        iframe.focus();
        ifWin.printPage();
        return false;
    }
    //this create iframes
    function createIframe(name, src) {
        var i = document.createElement("iframe");
        i.name = name;
        i.id = name;
        i.src = src;
        i.style= "display: none";
        document.getElementById("frames").appendChild(i);
    };
    </script>
    

    In your PHP Pages, remove my onload="window.print()" and at the end of body tag, just before </body> to be clear, add:

    <script>
        function printPage() { print(); }
    </script>
    

    like this:

    </body>
    <script>
        function printPage() { print(); }
    </script>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧