Alternatives to the page request method by POST

Asked

Viewed 117 times

1

I am developing a web application for my work. It consists of a Dashboard that will present company information to managers. In this context, I am trying to optimize the page by searching for the appropriate content according to the modality I seek. Thus, I made a menu containing the three modes of teaching that the company works: college, undergraduate and graduate. I was making the menu buttons as forms that would redirect to a page that would have a function that checks the mode sent via POST. It is functional, however whenever I try to go to another page it accuses that form data may get lost and becomes an uncomfortable thing. This way, I would like tips and suggestions on how to re/do to reach the final result I crave without having this form message.

Function that returns page

function get_page_modalidade($modalidade = "home"){
    if($modalidade == COLEGIO){
        return print "include './parts/menu-detalhe.php'" ;
    } elseif ($modalidade == "home") {
        return print "include './parts/menu-detalhe.php'" ;
    } elseif ($modalidade == GRAD) {
        return print "include './parts/menu-detalhe.php'" ;
    } elseif ($modalidade == POS) {
        return print "include './parts/menu-detalhe.php'" ;
    } else{
        return "outro";
    }
}

Function call

<?php  get_page_modalidade($_POST['modalidade']);?>

Menu button

<li>
    <form action="index.php" method="post">
        <input type="hidden" name="modalidade" value="home">
        <input type="submit" name="home" value="Dashboard" class="btn-block btn btn-focus btn-menu">
    </form>

  • Enter the code you have....

  • Have some special reason to be doing by POST?

  • @Randrade Yes. I use the link to identify the name of the page. When I use GET it displays parameters that may give error.

  • @Magichat I just updated with the main codes.

  • I’m beginning to understand, speaks a business, after you do the checking what happens, which continuation of the code ?

  • Also I did not understand why to use post for a simple link, what will happen if the user adds the page in the favorites? favourites do not save data uploaded via post.

  • @Magichat the intention is to do something similar to what Wordpress does with the get_template_part() function. I want that when I click on the menu link I do not have to redirect to a page for each item, but rather load the modules according to the request. In a common template, when I link with the <a> tag it redirects to a specific page. If I have 10 items on the menu, I’ll have to make 10 pages. The way I want to do it is to redirect to a single page and a function load the modules that each predefined mode has.

  • @Washingtondacosta I wanted to fetch the content of the pages according to the mode of teaching. A button with the "school" mode will pass via POST to the page in PHP that analyzes the modality and searches the correct module, without having to create a page for each module.

Show 3 more comments

1 answer

0


Good afternoon Rafael, the alternative I recommend to the HTTP POST method is HTTP GET, responding more directly to the title of your question.

From what I understand, your difficulty is not to manage the modules but to avoid the browser warning about revisiting a URL in HTTP POST method, which is when whenever I try to go to another page it accuses that form data may get lost and becomes an uncomfortable thing. as you explained.

I’ll use a similar example: I have an e-commerce site and have a POST method form whose action is index.php/busca, this action expects some form data, processes them and after finding the products, makes a redirecting GET TO another URL, this way I can avoid this same situation you describe, because if the user clicks on the browser back, it will not go back to the search action.

I hope I helped, ah a tip, replace your block if..elseif of the modality by a switch since the conditions are all the same ;-) The code is cleaner.

  • Fernando, I thought about doing it this way, but I’m afraid of having inconsistencies, since I use the link to check if the page is the "home", the "graduation", etc. It seems a good way to do it. I’ll test, make the appropriate changes and advise if it works.

  • It worked. Although it was a bit risky, I created a link with the <a> tag and addressed href to a link with the get instruction included. How to localhost/pagina.php? instrucao=novapagina .

  • Cool. In general I always worry and validate as much as possible the Urls of form actions, to avoid abuses, SQL Injection, etc. And always an action after doing its logic should redirect to another URL where the user ends up stopping, and this is never HTTP POST.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.