-2

I'm making a website for my school and my website is like a laptop store and the main thing in my webpage is a product filter. I have made the product filter, it works great, but the problem is that I added shortcuts that send you to a single product page by clicking one of the products in the product filter. You can click on a product when you just came in the website and haven't messed with the product filter and it sends you to the single product page, but once you search for something using the filter the filtrated product cant be clicked on and you have to refresh the page to select your product. This is happening, because when you come in the website, you see index.php, but once you use the product filter, you are seeing action.php.

here is a part of my code from index.php:

    <?php
          $sql="SELECT * FROM Laptop";
          $result=$conn->query($sql);
        if ($result-> num_rows > 0) {
          while($row=$result->fetch_assoc()){
          $Laptop_ID = $row['Laptop_ID'];
        ?>
         <?php echo "<a href='single_laptop.php?Laptop=" . $Laptop_ID ."'>" ?>
        <div class="col-md-3 mb-2" style="color: blue">
         <div class="card-deck">
          <div class="card boarder-secondary">
          <div class="card-img-overlay">
           <h6 class="text-light bg-success text-center rounded p-1">
           <?= $row['Nosaukums']; ?></h6>
          </div>
          <br>
           <img src="<?= $row['Bilde']; ?>" class="card-img-top">           
          <div class="card-body">
           <p>
           Procesors : <?= $row['Procesors']; ?><br>
           Videokarte : <?= $row['Videokarte']; ?><br>
           RAM : <?= $row['RAM']; ?><br>
           </p>
           <a href="#" class="btn btn-info btn-block"></a>
          </div>
         </div>
        </div> 
        </div>
       <?php } 
       }else {
        echo "nav rezultātu";
}?> 
    </div>
   </div>
  </div>

And here is my action.php:

<?php
 require 'dataB.php';
 
 if(isset($_POST['action'])){
   $sql = "SELECT * FROM Laptop WHERE Modelis !=''";
   
   if(isset($_POST['Modelis'])){
     $Modelis = implode("','", $_POST['Modelis']);
     $sql .="AND Modelis IN('".$Modelis."')";
   }
   if(isset($_POST['Tips'])){
     $Tips = implode("','", $_POST['Tips']);
     $sql .="AND Tips IN('".$Tips."')";
   }
   if(isset($_POST['RAM'])){
     $RAM = implode("','", $_POST['RAM']);
     $sql .="AND RAM IN('".$RAM."')";
   }
   if(isset($_POST['Procesors'])){
     $Procesors = implode("','", $_POST['Procesors']);
     $sql .="AND Procesors IN('".$Procesors."')";
   }
   if(isset($_POST['Videokarte'])){
     $Videokarte = implode("','", $_POST['Videokarte']);
     $sql .="AND Videokarte IN('".$Videokarte."')";
   }
   
   $result = $conn->query($sql);
   $output='';   
               
   if($result->num_rows>0){
   while($row=$result->fetch_assoc()){
    $output .='  
        <div class="col-md-3 mb-2" style="color: blue">
         <div class="card-deck">
          <div class="card boarder-secondary">
          <div class="card-img-overlay">
           <h6 class="text-light bg-success text-center rounded p-1">
           '.$row['Nosaukums'].'</h6>
          </div>
          <br>
           <img src="'.$row['Bilde'].'" class="card-img-top">           
          <div class="card-body">
           <p>
           Procesors : '.$row['Procesors'].'<br>
           Videokarte : '.$row['Videokarte'].'<br>
           RAM : '.$row['RAM'].'<br>
           </p>
           <a href="#" class="btn btn-info btn-block"></a>
          </div>
         </div>
        </div> 
        </div>';              
          }         
    } else { 
      $output = "<h3>No Products Found!<h3>";
    }
    echo $output;
  }  
?>

I need to figure out how to properly put the code from index.php, where it makes it possible to redirect me to single.laptop.php, to action.php, so I can redirect to single_laptop.php with no problems using the product filter.

  • What have you tried to resolve the problem? Where's the connection between shortcuts and AJAX? Also, be warned that your query is widely open for SQL injection. Have a look at prepared statements to avoid getting hacked – Nico Haase Mar 08 '22 at 13:40
  • **Warning:** You are wide open to [SQL Injections](https://php.net/manual/en/security.database.sql-injection.php) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). Never trust any kind of input! Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/32391315) – Dharman Mar 08 '22 at 14:46

1 Answers1

-3

$(document).keydown(function(e){
    var keycode=e.keyCode;
    if (keycode == 27)
     {
                  $("#change").trigger('click');
     }
});
  $("#change").click(function() {
  //do what you need
  if($("#radio:checked").length==0)
    {
        alert("abc");
        return false;
    }
});
<!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">
        <!-- Latest compiled and minified CSS -->
      <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css">

        <!-- Optional theme -->
      <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap-theme.min.css">

        <!-- Latest compiled and minified JQuery -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    </head>
    <body>
    Press ESC
     </body>
    </html>
  • 1
    Please add some explanation to your answer such that others can learn from it. Is there any reason you've posted two answers? – Nico Haase Mar 08 '22 at 13:41