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?

7
  • 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. Commented Apr 5, 2016 at 11:01
  • w3schools.com/jsref/event_onload.asp Commented Apr 5, 2016 at 11:02
  • can you add the javascript part of your code Commented Apr 5, 2016 at 11:02
  • 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! Commented Apr 5, 2016 at 11:36
  • 1
    Not a duplicate question, only the title is a bit misleading I guess Commented Apr 5, 2016 at 11:39

2 Answers 2

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 } ?>
Sign up to request clarification or add additional context in comments.

Comments

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\')"';
      }
    ?>
>...

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.