I can do it with three files :
- coords.php : this is the main page, where image is displayed and the click events are captured.
- coords_img.php : this file reads the coordinates x,y from database, generates the image with the red spots, and this image is returned to coords.php.
- coords_db.php : this file receives the x,y coordinate from a click event and store it in database, then it returns to coords.php to refresh the page.
Next three files work like a charm, you just create three files with the same names, copy-paste next codes in the files and open coords.php in your browser :
coords.php
<?php
session_start();
if ( ( ! isset($_SESSION["arr_x"]) ) ||
( ! isset($_SESSION["arr_y"]) ) )
{ $_SESSION["arr_x"] = array(10,620,50); // ◄■■ THIS IS OUR "DATABASE"
$_SESSION["arr_y"] = array(10,45,100); // ◄■■ (FOR TESTING PURPOSES).
}
?>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script type = "text/javascript">
//====================================================
$(function() // ◄■■ CLICK EVENT OF THE IMAGE.
{ $("#my_image").click( function(e)
{ var offset = $(this).offset();
$("#x").val( parseInt(e.pageX - offset.left) );
$("#y").val( parseInt(e.pageY - offset.top) );
$("#frm_xy").submit(); // ◄■■ GO TO "COORDS_DB.PHP".
} );
} );
//====================================================
</script>
</head>
<body>
<br/><br/>
<img id="my_image" src="coords_img.php"/> <!--◄■■ IMAGE WITH RED SPOTS-->
<form style="display:none" action="coords_db.php" <!--◄■■ HIDDEN FORM-->
method="post" id="frm_xy">
<input type="text" id="x" name="x"/>
<input type="text" id="y" name="y"/>
</form>
</body>
</html>
coords_img.php
<?php
ob_start();
session_start();
$image = imagecreatetruecolor(800, 600);
$red = imagecolorallocate($image, 250, 0, 0);
$a = $_SESSION["arr_x"]; // ◄■■ READ COORDINATES
$b = $_SESSION["arr_y"]; // ◄■■ FROM DATABASE.
for ( $i=0; $i < count($a); $i++ )
imagefilledellipse($image, $a[$i], $b[$i], 10, 10, $red);
header( 'Content-type: image/jpeg' );
ob_end_flush(); // ◄■■ I NEED THIS TO MAKE IT WORK,
ob_end_clean(); // ◄■■ WITHOUT IT I GET NO IMAGE.
imagepng( $image );
?>
coords_db.php
<?php
session_start();
if ( isset($_POST["x"]) && isset($_POST["y"]) )
{ array_push( $_SESSION["arr_x"], $_POST["x"] ); // ◄■■ STORE X AND Y
array_push( $_SESSION["arr_y"], $_POST["y"] ); // ◄■■ IN DATABASE.
}
header("location: coords.php"); // ◄■■ RETURN TO REFRESH PAGE.
?>
In my example codes I use two $_SESSION variables to simulate the database : $_SESSION["arr_x"]
and $_SESSION["arr_y"]
, they work fine. To make it work with a real database you will have to remove the two $_SESSION variables from the three files, then you add a connection to database and a "insert" query in coords_db.php, finally, you add another connection to database and a "select" query in coords_img.php.