UPDATE PAGE WITH AJAX (Updating a param of function)

Hi
I have this page:

    <?php
include 'assets/header.php';
?>

<!-- MAIN APP -->
<a class="button" onclick="btnclick('cursos')">Cursos</a>

<div id="main">
<?php
include 'routing.php';
?>
</div>

<?php
include 'assets/footer.php';
?>

I use ROUTING.PHP to get the url and include a file:

function routing($view) {
    if ($view == '') {
        include('views/home.php');
    } else {
        if(file_exists('views/'.$view.'.php')) {
            include('views/'.$view.'.php');
        } else {
            include('views/not-found.php');
        }
        
    } 
} 

routing($_GET['filename']); 

I need to create a menu, when i click a buton UPDATE the routing to change the param routing($_GET[‘filename’]); without reloading, using AJAX, possible?

<a class="button" onclick="btnclick('cursos')">Cursos</a>

Welcome to the site!

Not really sure if you understand PHP and AJAX and Javascript/JQuery… I will give you some info on these.
PHP is SERVER-SIDE ONLY. Very important to know. You will never see any PHP code in a webpage except to display code as you did above. It is NOT executable in a browser. AJax is a two part process, the Javascript or JQuery call which are all CLIENT-SIDE and the PHP file called which is server-side.
Therefore, if you use Javascript/JQuery in the client to call a process as you showed in the Cursos section, it must go to a JS/JQ routine, which can call or recall the routing.php file. But, you would need to pass the new name to the AJax so it can pass it to the PHP so the result can come back. But, this causes issues with redisplaying the data. Since you have set it up in PHP with three sections, header, view, footer, it would probably be more efficient to just use standard posting and have the button refresh the page with the correct view. It would be better that. If you have a very complicated page and need to just refresh one section with the new page, you can do that. Perhaps you should tell us why you are going thru all these back and forth between server and client.

You could just load the second file into a DIV. It would just take a simple JQuery script. Here is a sample from a site I just found: This first part just shows a link without a HREF so it is handled by the JQuery code and contains a DIV with your original code.
< a id=“cursos”>Cursos< /a>
< div id=“main”>This is original stuff you want displayed< /div>
Next you would have the other file to include this layout to allow you to replace it into the first:
< div id=“main”>Different stuff to replace into the first display< /div>
And a small JQuery script:
< script type=“text/javascript” language=“javascript”>
$(document).ready(function() {
$(’#cursos’).click(function(){
$(’#main’).load(‘views/view-name.php #main’, function() {
});
});
});

You could just alter the data loaded into the DIV so that it places the correct name in the link for the file-name that needs to be loaded. If you have many different buttons, you could just use a simple drop-down to list them and have the drop-down trigger a refresh of the page with the correct view in place.

Not sure if this all will confuse you or help. Hope it helps! If not, give us a better description of what you are attempting to do. Good luck!

1 Like

Really helpfull, i just fixed the code and now is working, like you say i send the param via ajax, process the data in the php file and get the response of the file with my content, nxt use this content (data) to replace the html with javascript. Thanks, i understand now the basics, will keep lerning.

Nice you solved it! Always exciting when you correct a programming puzzle!
( But, be warned you will always keep learning in programming! I have been in programming when it all started and learn new things every day! Keep learning! )

Sponsor our Newsletter | Privacy Policy | Terms of Service