Problem passing value by URL

Asked

Viewed 74 times

0

I’m trying to create a more dynamic way to load the pages I’m using, loading files like CSS on just one page, and from that page, calling others. However, I cannot receive the value I am going through the URL. Follow the sending code:

<div id='navbar' class='navbar-collapse collapse'>
<ul class='nav navbar-nav'>
    <li class='dropdown'>
        <a href='#' class='dropdown-toggle' data-toggle='dropdown' role='button' aria-haspopup='true' aria-expanded='0false'>Ofícios<span class='caret'></span></a>
        <ul class='dropdown-menu'>
            <li class='dropdown'>
                <a href='#' class='dropdown-toggle' data-toggle='dropdown' role='button' aria-haspopup='true' aria-expanded='false'>Funcionário<span class='caret'></span></a>
                <ul class='dropdown-menu'>
                    <li><a href='funcionario_cadastrar.php?link=17'>Cadastrar</a></li>                      
                    <li><a href='funcionario_listar.php?link=18'>Listar</a></li>
                    <li><a href='funcionario_consultar.php?link=19'>Consultar</a></li>
                </ul>
            </li>
        </ul>
    </li>
</ul>

And the receipt:

$link = $_GET["link"];
print_r($_GET["link"]);
if(isset($_GET["link"])){
    /*
    $pagina[17] = "funcionario_cadastrar.php";
    $pagina[18] = "funcionario_listar.php";
    $pagina[19] = "funcionario_consultar.php";

    if(!empty($link)){
        if(file_exists($pagina[$link])){
            include $pagina[$link];
        }else{
            include "bem_vindo.php";
        }
    }else{
        include "bem_vindo.php";
    }
}else{
    include "bem_vindo.php";
    */
}
  • The isset() should be done directly on $_GET. When in doubt do print_r($_GET);

  • 2

    Here it works normally, if I access the page containing the link, while being redirected it will start the variable $link, now if I try to directly access the page that picks up the link gives an error, explain better what you are doing and if possible put the full code of the page with the link, it may be something else...

  • I edited the publication with the rest of the code.

  • Something comes up from print_r()?

  • No, I tried to put in and out of the if to see if there was a difference, but it continued showing nothing.

  • you have apache installed?

  • Yes, I use the Apache2.

  • The code seems to work, is it not sending to the wrong file? Leaves your file that gets only so, see if something appears. echo (isset($_GET["link"]) ? $_GET["link"] : 'nada');

  • This time the message appeared "nothing".

Show 4 more comments

2 answers

1

Try it this way. I ran some tests and it worked.

<?php
if(isset($_GET["link"]) && $_GET["link"] != '') {
    $link = $_GET["link"];

    switch ($link) {
        case 17:
            echo "funcionario_cadastrar.php"; //include "funcionario_cadastrar.php";
        break;
        case 18:
            echo "funcionario_listar.php"; //include "funcionario_listar.php";
        break;
        case 19:
            echo "funcionario_consultar.php"; //include "funcionario_consultar.php";
        break;
        default:
            echo "index.php"; //include "index.php";
        break;
    }
} else {
    echo "index.php"; //include "index.php";
}

1


<div id='navbar' class='navbar-collapse collapse'>
<ul class='nav navbar-nav'>
    <li class='dropdown'>
        <a href='#' class='dropdown-toggle' data-toggle='dropdown' role='button' aria-haspopup='true' aria-expanded='0false'>Ofícios<span class='caret'></span></a>
        <ul class='dropdown-menu'>
            <li class='dropdown'>
                <a href='#' class='dropdown-toggle' data-toggle='dropdown' role='button' aria-haspopup='true' aria-expanded='false'>Funcionário<span class='caret'></span></a>
                <ul class='dropdown-menu'>
                    <li><a href='index.php?link=17'>Cadastrar</a></li>                      
                    <li><a href='index.php?link=18'>Listar</a></li>
                    <li><a href='index.php?link=19'>Consultar</a></li>
                </ul>
            </li>
        </ul>
    </li>
</ul>

if( isset($_GET["link"])){

  $link = $_GET["link"];
  switch ($link) {
        case 17:
            include="tema17.php";
            break;
        case 18:
            include="tema18.php";
            break;
        case 19:
            include="tema19.php";
            break;
        default:
            include="temaDefault.php";
            break;
    }
}

This is what I do to include files according to the value of the variable, redirects to the same page that in your case can be an index.php and inside it you put this code and so you include as many as you want.

With this you create a single menu, header and footer.

  • Error: PHP Notice: Undefined variable: link on line switch ($link) {&#xA; case 18:&#xA; include "funcionario_listar.php";&#xA; break; &#xA; }

  • You have to declare it with some value before, your $_GET["link"] can fill this $link variable and only with it add the files as you want to do.

  • I just edited my example, follow that can work.

Browser other questions tagged

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