I'm developing a website and I want to change the image of a page when NEXT button is clicked. I retrieve the images from my database and of course I do not want to retrieve repetitive images.
This is my NEXT button:
<input type="submit" value="Next" onclick="synchronousAJAX()" style="position: absolute; left: 1085px; top: 521px; WIDTH: 100px; font-size: 24pt; background-color: #CCCCCC;"/>
I use synchronousAJAX() function to count the numbers the NEXT button is clicked and send it to my PHP:
counter=0;
function synchronousAJAX(){
counter++;
xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET","show.php?no="+counter,false);
xmlhttp.send(null);
if(xmlhttp.readyState ==4 && xmlhttp.status == 200){
document.getElementById("repeat").innerHTML =xmlhttp.responseText;
}
else{
window.alert("Error: "+ xmlhttp.statusText);
}
}
Then, in my PHP page I connect to my database and retrieve the nth row of my database in which n is the number NEXT button is clicked.After I retrive the image I redirect the page to the page that my next button is located and echo the image. I do it as many times as I have image in my database.
<?php
$n = $_GET['no'];
$db = mysql_connect("127.0.0.1", "root", "") or die(mysql_error());
mysql_select_db("fyp", $db) or die(mysql_error());
if ($n=="1")
{
$data = mysql_query("SELECT * FROM items limit 0,1 ") or die(mysql_error()); //Puts it into an array
if($info = mysql_fetch_array( $data ))
{
session_start();
$_SESSION["Photo1"] = $info['Pic'];
$_SESSION["Name1"] = $info['Name'] ;
header("location:pasge.php");
}
}
else if ($n=="2")
{
$data = mysql_query("SELECT * FROM items limit 1,1 ") or die(mysql_error());
if($info = mysql_fetch_array( $data ))
{
session_start();
$_SESSION["Photo1"] = $info['Pic'];
$_SESSION["Name1"] = $info['Name'] ;
header("location:pasge.php");
}
}
?>
<span> <?php if(isset($_SESSION["Photo1"])) Echo "<img src=img/".$_SESSION["Photo1"] ."> <br>"; ?> </span>
But it seems that my counter does not work properly. Is there anyone who could help me with the issue? I am also open to other new ideas. Thank you in advance.