The problem is this: There are 2 buttons on one page, when you click on each of their buttons, there is an ajax request and output, let's say it looks like this now

$(".btn1").click(function() { $.ajax({ type: "POST", data: "categ=" + categ, url: "serverscript/page1.php", success: function(data) { $(".block1").html(data); } }); 

page1.php

 include './config.php'; $categ=$_REQUEST['categ']; $STH = $DBH->query('SELECT * from table1 where categ= "' . $categ. '" '); $STH->setFetchMode(PDO::FETCH_ASSOC); while($row = $STH->fetch()) { echo $row{'name'} } 

And about the same at 2 buttons

  $(".btn2").click(function() { $.ajax({ type: "POST", data: "categ2=" + categ, url: "serverscript/page1.php", success: function(data) { $(".block2").html(data); } }); 

page2.php

 include './config.php'; $categ2=$_REQUEST['categ2']; $STH = $DBH->query('SELECT * from table1 where categ2= "' . $categ2. '" '); $STH->setFetchMode(PDO::FETCH_ASSOC); while($row = $STH->fetch()) { echo $row{'name'} } 

Something like this, the question is how can I create 1 php file and do an ajax request for a specific piece of code, like wrapping something in a function. In order not to do 2 files page1 and page2, but to stir up 1 where these two pieces are. I hope you understand me

  • you need the category table with categories in the database, and the category_id column in the table pointing to the table with categories .... and then the query is reduced to the query SELECT * from table1 where category_id = $id; .... and if the categories are added, it is not necessary to write caeg2 , categ4 and other nonsense ..... but also interestingly - what is the point of using PDO without prepared variables? - Alexey Shimansky
  • @ Alexey Shimansky, yes no, this is an absolutely abstract code, I invented it on the go, the crux of the matter is how to merge everything into one file and access a specific function or piece of code - zkolya
  • In this case, you have to write a whole article about classes and routing .... - Alexey Shimansky

0