0

I am a beginner in php and javascript I am using a session variable to hold a counter on next button click i want to increment the counter and on prev button click i want to decrement the counter and i want to reload the page later , the problem is with self invocation of the javascript function without being called by the button click. This makes both f1 and f2 to be called nullifying the counter value. I want to prevent self invoking.

  <button type="button" onClick="f1();" id="prev"><< Prev</button>
  <button type="button" onClick="f2();" id="next">Next >></button>

<script>
function f1(){
<?php $_SESSION['counter']= $_SESSION['counter']-1?>
window.location.reload();
} 

 function f2(){
  <?php $_SESSION['counter']= $_SESSION['counter']+1?>
  window.location.reload();
} 

</script>

Thank you.

8
  • 2
    ?? What makes you think the functions are "self invoking"? Commented Apr 4, 2018 at 14:15
  • 4
    PHP will run when page renders, not when the javascript functions are called. Commented Apr 4, 2018 at 14:15
  • Why are you trying to call PHP from JS? ... Commented Apr 4, 2018 at 14:16
  • both f1() and f2() are being called on reload , not on the button click. i concluded as self invoking since the counter value is being nullified. Commented Apr 4, 2018 at 14:18
  • Unless you need to access that value somewhere else in your PHP, take a look at sessionStorage instead, you can achieve the same result with pure Javascript. Commented Apr 4, 2018 at 14:23

1 Answer 1

1

It is not possible to run PHP code inside of a JS function. (To be precise: It is possible, as you have seen and read in the comments, but - in this case - does not have the effect you want it to have.) It cannot be possible because JS is run on the client, but PHP is run on the server (so, before the user even sees the page).

Instead of that, you could either use AJAX to transfer the change to the server, or use GET/POST requests using a link or a formular, which is slower, but easier:

<a href="?action=prev">&lt;&lt; Prev</a>
<a href="?action=next">Next &gt;&gt;</a>

To use that information in PHP, use some code as this:

<?php
if(isset($_GET["action"])) {
    switch($_GET["action"]) {
        case "prev":
            $_SESSION["counter"]--;
            break;
        case "next":
            $_SESSION["counter"]++;
            break;
    }
}

By the way: Escape "<" and ">" in HTML code, it's good style and prevents you from running into problems.

An alternative way would be saving the value in the client using sessionStorage instead, as @Skwal pointed out, if you do not need the value on the server or want to pass it each time using AJAX or GET/POST.

Sign up to request clarification or add additional context in comments.

2 Comments

To be more specific , i need the updated variable to get an image path from the database, so on next i will query with the new counter value <img src="<?php if($j<=$i&&isset($certificate)) echo $certificate[(int)$_SESSION['counter']][4] //path;?>"
In that case, the above code should work. As you click the links, $_SESSION["counter"] is either incremented or decremented, depending on what link is used. A check for $_SESSION["counter"] to be at least zero and at most the length of your array would be useful, but that's secondary.

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.