javascript – Ajax not moving to the next page-ThrowExceptions

Exception or error:

I am displaying images dynamically from the database in a PHP page. I get the ID of the clicked image using JavaScript. when I try to pass the value to the next page in JS using localStorage, it works fine. But when I try AJAX (because I want to send the variable in JSON so I can read it using PHP) it shows the index of the clicked image but it is not redirecting me to the next page?

this is the code:

<?php
    session_start();

    require "init.php";
    login();
?>
<html>

<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
    <script >


function myFunction(clicked) {
  document.getElementById('test').innerHTML = clicked;
  // this can help the user to store the value locally AND recall it when he needs it from JS
 // localStorage.setItem('index',clicked);
 // window.document.location='./test1.php'

 //using AJAX to pass the variable and read it using PHP in the next page
  var loc={};
  loc.id=clicked;
  $.ajax({
      url:"test1.php",
      method:"post",
      data:loc,
      success: function(res)
      {

      }
  })
}
</script>


<title> Expert System </title>
    <link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>


    <button id="1" onclick="myFunction(this.id)">This will show date when clicked.</button>

<p id="test"> </p>

<?php
// LOGIN USER
function login(){
    global $con;
    global $counter;
echo "<table border='1' align='center'     >";
    //$email = $mysqli->escape_string('');
    $query="SELECT * FROM users ";
    $result=mysqli_query($con,$query);
    if ( $result->num_rows == 0 ) // User doesn't exist
        echo "User with that ID doesn't exist!";
    else { // User exists
     $counter = 0;
     $emptyArray = [];

while($row = $result->fetch_assoc()) 
   {
       $emptyArray[$counter]= $row["ID"];
       if($counter == 0)
       {
           echo '<tr>';
       }
       echo '<td><img id=' . $row["ID"]. ' onClick="myFunction(this.id)" src="images/' . $row["category"]. '" width="250px"  height= "150px" alt="Avatar" >
       <h3>'.$emptyArray[$counter].'</h3> <p id = "GFG_DOWN" style =  
            "color:green; font-size: 20px; font-weight: bold;"> dfsfds
        </p> </td>';
       $counter++;
       if($counter == 3)
       {
            echo "</tr>";
            $counter = 0;
       }
  }
    }
}

mysqli_close($con);
?>

</table>

</body>
</html> 
How to solve:

Leave a Reply

Your email address will not be published. Required fields are marked *