-1

I'm not sure if I labelled my title correctly, but I was told that the code below wouldn't work because the browser window never reloads upon submission of the form that updates content (it is inside a shadowbox).

<?php 
    if(isset($_SESSION['contentupdated'])) { 
       unset($_SESSION['contentupdated']); 
       echo 'window.location.reload();'; 
    } 
?>

How would I write PHP code based on the above code that "polls" the site to see if the variable $_SESSION['contentupdated'] is set? Below, I will paste my full shadowbox code that I was attempting to use as well. The updated code will replace the PHP code within the JavaScript code. That way upon closing the shadowbox, the website will poll the site to see if there is an updated code (that is my goal at least).

I was told I'd have to poll the site to check for updates but I have searched Google and can't find a solution for this as all the results come up as "How to create a website poll".

<script>
    Shadowbox.init({ 
        onOpen: onShOpen, 
        onClose: onShClose, 
    }); 

    function onShOpen() { 
        document.body.style.overflow = "hidden"; 
        return true; 
    } 

    function onShClose() { 
        document.body.style.overflow = "auto"; 
        <?php 
            if(isset($_SESSION['contentupdated'])) {
                unset($_SESSION['contentupdated']); 
                echo 'window.location.reload();'; 
            } 
        ?>
        return true; 
    }
</script>

Any suggestions?

w5m
  • 2,259
  • 3
  • 29
  • 42
kdjernigan
  • 299
  • 2
  • 5
  • 13

2 Answers2

-1

Why do you use javascript to reload the page. you can do it in php using header location:

<?php if(isset($_SESSION['contentupdated'])) { unset($_SESSION['contentupdated']); header('Location:yourpage.php');die(); } ?>
Adidi
  • 4,821
  • 3
  • 19
  • 28
  • the problem is, I was told that session variables are set on page load, so the problem occurs when the page doesn't reload upon update (the shadowbox just closes) back to the page behind it. So I was told I'd need to poll the site for updates to the variable – kdjernigan Mar 22 '13 at 17:29
  • Well. yes! you should put this code before you write something to the browser(using echo or when html is starting). session variables are using cookies - so they can be set anywhere in the php page as long you didn't output nothing to the browser (then you will get header already sent - cause cookie are transfer in headers) – Adidi Mar 22 '13 at 17:37
  • the page is loaded already, and the user clicks the section they want to update, a shadowbox opens with the editcontent.php page and they edit the content, click update, and then close the box. The page behind shadowbox never reloads. I need the page behind, upon close of shadowbox, to verify to see if the variable $_SESSION['contentupdated'] is set. Which is why the standard code i have above doesnt work. I need a server friendly way to checking this. – kdjernigan Mar 22 '13 at 17:41
-1

Just listing things that come to mind, Not a great answer...

Long Poll with PHP: How do I implement basic "Long Polling"?

PHP and Comet: Using comet with PHP?

HTML5 Web Sockets: http://www.html5rocks.com/en/tutorials/websockets/basics/

Non PHP solutions using other technology such as NodeJS.

UPDATED: reason I added this was OP seemed to be lacking some of the vocabulary to effectively search and answer his question.

Community
  • 1
  • 1
ficuscr
  • 6,634
  • 2
  • 28
  • 47
  • Sounding more like just needing an triggered Xhr request as I read the new comments... – ficuscr Mar 22 '13 at 17:48
  • do you know how to code this? I read up on it; however, can't figure out how I'd code this into my script because everything I see is based on div triggers – kdjernigan Mar 22 '13 at 18:35
  • Whatever you can hook into on the shadowbox... A *close* event? OK, like what you have but your code as is makes no sense. You can't fire that PHP code in client code on a callback. You'll need to make an AJAX request back to the server: http://api.jquery.com/jQuery.ajax/ – ficuscr Mar 22 '13 at 18:39
  • that makes sense. Thats why I'm on here. I don't know how to code ajax :/ Guess I'll keep looking and waiting – kdjernigan Mar 22 '13 at 18:44
  • It can be a tough concept to wrap your head around. Fortunately there are many tutorials available on the web, from articles with code examples to videos. You are on the right track at least - spend sometime studying and I am sure you'll accomplish what you are after. This looks like it might be a good one: http://www.developphp.com/view.php?tid=1143 – ficuscr Mar 22 '13 at 18:47