Your architecture is a mess!
Let's start with parts of HTTP:
the client makes a request
the server receives this request
the server processes this request
... still processing ...
server responds with data
the client downloads the request
If it is a browser that is receiving HTML, the client renders
Therefore, its processing is free. And how do you do the processing? You said you want to " move from one PHP file to another PHP ", which in essence does not make exactly sense ... PHP files were not meant to talk to each other the way they you indicated. It even seems like you wanted a file to run, go back to the client, and then make a request shortly to retrieve the data obtained by the previous script. At least that's how I understood your speech.
Normally what you do is create a collection of functions in a file more backend and, in the file facing frontend, call those functions and display them correctly. To do this, you do not need to separate beautifully across all layers of the MVC.
For example, this separation of data collection from the database and then display was done in that other question :
original response from @Sveen used the principle of interface and bank separation
my response (which is a variation of the @Sveen response, as I indicated in the text) gives an easier maintenance air; I made a use of including files in function call
Since the only difference between the two responses was that I used a function, I'll focus on the original response, @Sveen's. The idea here was:
Isolating a line from a bank in a component
in frontend , first make the basic statement independent of the database
Then, in the middle of frontend , get the various lines and, for each one, call the line display
Adapting to your case might look something like this:
back.php :
<?php
function obter_linhas($nome) {
$con = new mysqli("host", "user", "pass", "db");
$res = $con->query("SELECT * FROM tabela WHERE coluna = '$nome'");
return $res->fetch_all();
}
?>
front.php :
<?php include_onde 'back.php' ?>
<html>
<body>
<h1>Hello, world!</h1>
<?php
foreach (obter_linhas($_GET['nome'] as $linha) {
echo "<li>Olha a primeira posição do array: ".$linha[0]."</li>";
}
?>
</body>
</html>
Ready. We did the visualization of the data independently of the obtaining. In a way, we separate the " front " and the " back ".
About the mess ... the apparent usage closely resembles something that would be an adaptive page. So, in that case, you would not need to submit the page for a full refill. An asynchronous recharge of the minimally necessary part would be enough. For example, you could separate a <div>
for your results and, when you retrieve the values, write that <div>
(including discarding the previous values).
Using this type of thinking, you can have interfaces that display new information without requiring a full page load:
Forthis,youwouldneedtosendthedata(thedataitself,notthedisplayofthem,usuallyitisusuallytransferredinjson
orxml
thatdata)fromaendpointPHP.Ontheclientside,JavaScriptwouldtakecareofreceivingthisdata,interpretingitandthendisplayingitinthemostappropriatewaypossible.
YouwillfindmoreaboutthispartialdatarequestbysearchingAJAX.Bytheway,thisacronymmeans(ormeantatthebeginning):
SynchronousJavaScriptandXML
Freetranslation:
AsynchronousJavaScriptandXML
Here,asynchronousmeansthatthescriptwillcallanendpointontheserveranditwillrespondwhengiven,havingnoguaranteewhenthisoccurs.JavaScriptistheprogramminglanguagethatmakesthecallandthenhandlestheresponseobtained.AndXMLnowadaysisunderstoodonlyasadatatransfer,itdoesnothavetobethedatatransferXMLformat.
AnotheruseofAJAXyoucanseehere,inGitLab:
Here, when prompting to display a tab, GitLab starts by doing the asynchronous request, then placing the placeholder of the balls spinning so I think it's working. When the request is finally terminated, JavaScript intercepts the data and tries to display it to me in the best possible way.