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

如何刷新单独的页面以获取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.

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>

            function refreshPage() {

            $("#widgetbox").fadeOut(750, function() {

            <!-- empty the current content and then fetch new data -->
            $("#widgetbox").empty
            <!-- load the getPage.php file which will replace all the      
            $("#widgetbox").load("getPage.php", function() {
                $("#widgetbox").fadeIn();
            });
            });

            };

            <!-- initiate the function above -->
            $(function(){
            refreshPage();

        <!-- set the interval for refreshing, default set to 1.0 seconds -->
        var int = setInterval("refreshPage()", 1000);

    });

study this http://websitetutorials.net/jquery/rotate-product-listings-php-and-jquery and modify it