1

I have a button which successfully triggers a "load more" on click. Once it is clicked, a SESSION variable is set, so that when the user reloads the page, the new posts should appear loaded already, so that the user does not need to click again "load more".

I would thus like to render the same "load more" Javascript function in a PHP IF statement, according if a SESSION or COOKIE is set or not:

<?php
if ((isset($_SESSION['isloaded'])) || (isset($_COOKIE['isloaded']))){
echo '<script>loadmore(\'posts\');</script>';
}
else {
echo '<a href="#" onClick="return false" onmousedown="javascript:loadmore(\'posts\');" class="btn btn-primary" id="load-more-btn">Load More</a>';
}
?>

However the Javascript does not get triggered once the page is rendered. What am I doing wrong?

rainerbrunotte
  • 907
  • 1
  • 17
  • 37
  • 1
    Where in relation to this code snippet is the `loadmore` function defined? it should be defined before you try to call it. Check the browser console for errors. –  Apr 05 '16 at 11:01
  • http://www.w3schools.com/jsref/event_onload.asp – JimmyB Apr 05 '16 at 11:02
  • can you add the javascript part of your code – sujivasagam Apr 05 '16 at 11:02
  • check your browser's console for any errors – Mohan Apr 05 '16 at 11:06
  • I m guessing OP understands the difference between server-client side. And i m also guessing his problem is that he hasnt put `session_start();` at the very top of his .php files... Are my guesses correct? – Sharky Apr 05 '16 at 11:32
  • 1
    Thanks @Austin, it was indeed simply the fact that my Javascript was loaded at the end of the document, and that's why it did not work! – rainerbrunotte Apr 05 '16 at 11:36
  • 1
    Not a duplicate question, only the title is a bit misleading I guess – Pimmol Apr 05 '16 at 11:39

2 Answers2

3

As @Austin and @Angry Coder said, check your console for errors.

Also, make sure function loadmore() is defined before it's called. So either place the function declaration above the loadmore('posts'); call or add the call on a onload or something similar.

An other thing, maybe for clearness you can write your code like (but it's an opinion):

<?php  if ((isset($_SESSION['isloaded'])) || (isset($_COOKIE['isloaded']))) { ?>
        <script>loadmore('posts');</script>
<?php } else { ?>
        <a href="#" onClick="return false" onmousedown="javascript:loadmore('posts');" class="btn btn-primary" id="load-more-btn">Load More</a>
 <?php } ?>
Pimmol
  • 1,871
  • 1
  • 8
  • 14
0

To have your JavaScript run automatically, you can use the onLoad event, like

<body onload="loadmore('posts')">
...
</body>

or maybe

<body onload="conditionally_loadmore('posts')">
<?php
if ((isset($_SESSION['isloaded'])) || (isset($_COOKIE['isloaded']))){

    // Only have the function do something if we really want to.
    echo '<script>function conditionally_loadmore(s) {';
    echo ' loadmore(s); ';
    echo '}</script>';
}
else {

    echo '<script>function conditionally_loadmore(s) {';
    echo ' // Do nothing. ';
    echo '}</script>';

    echo '<a href="#" onClick="return false" onmousedown="javascript:loadmore(\'posts\');" class="btn btn-primary" id="load-more-btn">Load More</a>';
}
?>

or, as @Astaroth suggested:

<body
    <?php
      if ((isset($_SESSION['isloaded'])) || (isset($_COOKIE['isloaded']))) {
        echo 'onload="loadmore(\'posts\')"';
      }
    ?>
>...
JimmyB
  • 12,101
  • 2
  • 28
  • 44
  • 1
    I think this is what the OP needs, just clarify that he should add the `onload="loadmore('posts')"` with something like `= ($cookieConditions) ? 'onload="loadmore(\'posts\')"' : ''; ?>` so the onload is just inserted when the "cookie conditions" are met. Just in case the OP does not see who to apply your answer. – Astaroth Apr 05 '16 at 11:14