otis otis - 6 months ago 13
PHP Question

Auto reload included page after changing $_GET

I need to reload included page in after user clicks the specific link, which changes the tournament id.

info.php

<?php
$tournament = $_GET['tournament'];
$user = $_SESSION['user_id'];
$sql=$conn->query("SELECT *, SUM(points) as total FROM results WHERE tournament_id = '$tournament' and user_id = '$user' ");
$data=mysqli_fetch_object($sql);
echo 'Logged user '. $_SESSION['name'] .', score '. $data->total . 'in tournament '. $data->tournament_name; ?>


in main layout I have:

<div id="info">
<?php include "info.php"; ?>
</div>

<div id="main">Select tornament:
<a href="competition.html?tournament=1">Tournament 1</a><br />
<a href="competition.html?tournament=2">Tournament 2</a><br />
</div>


I need to automatically refresh div where page "info.php" loads to get logged user's points from chosen tournament.

Answer

You cannot re-execute an included PHP script once the page is generated and loaded. So you have two solutions:

  1. Use AJAX to request the info.php from the server and then insert the received content in the DIV using JavaScript:

    document.getElementById("info").innerHTML=ReceivedContent;

  2. Use IFRAME instead of DIV and reload it when needed.

HTML:

<IFRAME id="info" src="info.php"></IFRAME>

JavaScript:

document.getElementById("info").contentWindow.location.reload();