0

This is my PHP code. My backwardCallFunction and forwardCallFunction do not work. The number displayed in:

<div class= 'call-count'>Call Count:  <?php echo $_SESSION['callCount'];?></div> 

Still the same. Anyone can help me solve the problem?

<?php
session_start();
$_SESSION['callCount'] = 0;

function forwardCallFunction()
{
    $_SESSION['callCount']++;
}

function backwardCallFunction()
{
    $_SESSION['callCount']--;
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="style.css">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet"
          integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
    <link rel="stylesheet"
          href="https://fonts.sandbox.google.com/css2?family=Material+Symbols+Rounded:opsz,wght,FILL,GRAD@20..48,100..700,0..1,-50..200"/>
    <title>Document</title>
</head>
<body>
<div class='home-middle-subcontent'>
    <div class='to-left' onclick="<?php backwardCallFunction(); ?>"><span class="material-symbols-rounded">keyboard_double_arrow_left</span>
    </div>
    <div class='call-count'>Call Count: <?php echo $_SESSION['callCount']; ?></div>
    <div class='to-right' onclick="<?php $_SESSION['callCount']++; ?>"><span class="material-symbols-rounded">keyboard_double_arrow_right</span>
    </div>
</div>
</body>
</html>
Karl Hill
  • 12,937
  • 5
  • 58
  • 95
  • 1
    You're resetting the count to 0 every time the page is loaded. – Barmar May 06 '22 at 00:44
  • You can't call PHP functions from `onclick`. PHP runs on the server, `onclick` runs JavaScript on the client. You need to use `AJAX` to call the server from JavaScript. – Barmar May 06 '22 at 00:45
  • ... or fall back to the old Create page -> Read Uri -> Create page cycle @Barmar. I think Abdullah Azzam needs some very basic directions to start with – Eineki May 06 '22 at 01:15

1 Answers1

0

Change

<?php
session_start();
$_SESSION['callCount']=0;

to this

<?php
session_start();
if (!isset($_SESSION['callCount']) {
    $_SESSION['callCount']=0;
}

Otherwise you will reset the counter every time you access the page.

Once done, you need to rework the html part of your script (javascript and php are not communicating the way you try to do).

Change

<div class='to-left' onclick="<?php backwardCallFunction(); ?>"><span class="material-symbols-rounded">keyboard_double_arrow_left</span>
</div>

to

<a class="to-left" href="?to-left"><span class="material-symbols-rounded">keyboard_double_arrow_left</span></a>

and

<div class='to-right' onclick="<?php $_SESSION['callCount']++; ?>"><span class="material-symbols-rounded">keyboard_double_arrow_right</span>
</div>

to

<a class="to-right" href="?to-right"><span class="material-symbols-rounded">keyboard_double_arrow_right</span></a>

Using the a tag and skinning it as a DIV with CSS is way simpler (and usual) than the opposite way.

Once you have done, you need to read the user action using the uri you get and perform the needed action:

<?php

function forwardCallFunction()
{
    $_SESSION['callCount']++;
}

function backwardCallFunction()
{
    $_SESSION['callCount']--;
}

session_start(); 
if (!isset($_SESSION['callCount']) {
    $_SESSION['callCount']=0;
}
$action = $_SERVER['QUERY_STRING'];
if ($action == 'to-left') backwardCallFunction();
if ($action == 'to-right') forwardCallFunction();
?><!DOCTYPE html>

The mechanism of php/javascript interaction is just this simple (ajax obfuscate a bit the process but the way it works is the same):

  • PHP create a page served to the client browser
  • the user interacts with Links and Buttons and send data to php via the url query string
  • PHP read the query string (as I have done or via the $_GET / $_POST / $_REQUEST superglobals)
  • the PHP script take every action needed to check the user input against input data tampering and user misbehavious (aka XSS attack and similar
  • PHP create a page served to the client browser and so on
Eineki
  • 14,773
  • 6
  • 50
  • 59