Pass variable via GET without action

Asked

Viewed 117 times

2

I plan to pass a variable to another page, I know I can use the method GET, meanwhile if here <form method="GET" action="comentario.php"> is comentario.php it does not execute the commands to insert into the database (that code is on the same page), to insert would be <form method="GET" action=""> but the variable is obviously no longer recognized on the other page, (in this case the variable is $lang=$_GET['lang'];)
My question is how do I get the variable to another page? By the way I’m not using Sessions.
Home page:

<form method="GET" action="">
    <table width="80%" align="center" cellpadding="8">
        <br><br>
        <tr>
            <td colspan="1" class="logo">
                <img src="imagens/logo.png" height="40" width="150">
            </td>
            <td class="idioma" align="right">
                <?php $lang=isset($_GET['lang']) ? $_GET["lang"] : false; ?>
                <select id="lang" name="lang" onchange="submitForm()">
                    <?php
                    switch ($lang) {
                        case 'en':
                        $lng=1;
                        break;
                        case 'fr':
                        $lng=2;
                        break;
                        case 'sp':
                        $lng=3;
                        break;
                        case 'de':
                        $lng=4;
                        break;
                        default:
                        $lng=0;
                    }
                    $questao=array('O que achou do nosso serviço?','ingles','frances','espanhol','alemao');

                    ?>
                    <option value="pt"<?php if($lang != 'fr' && $lang != 'en' && $lang != 'sp' && $lang != 'de'){ echo " selected=\"selected\"";} ?>>Português</option>
                    <option value="en"<?php if($lang == 'en'){ echo " selected=\"selected\"";} ?>>English</option>
                    <option value="fr"<?php if($lang == 'fr'){ echo " selected=\"selected\"";} ?>>Français</option>
                    <option value="sp"<?php if($lang == 'sp'){ echo " selected=\"selected\"";} ?>>Español</option>
                    <option value="de"<?php if($lang == 'de'){ echo " selected=\"selected\"";} ?>>Deutsch</option>
                </select>
            </td>
        </tr>
        <tr>
            <td colspan="3" class="pergunta" align="center">
                <hr>
                <?php echo $questao[$lng]; ?>
                <hr>
            </td>
        </tr>
        <tr>
            <td colspan="3" class="selecao">
                <div class="cores">
                    <input type="submit" name="verde" class="verde" value="verde">
                    <input type="submit" name="amarelo" class="amarelo" value="amarelo">
                    <input type="submit" name="vermelho" class="vermelho" value="vermelho">

                    <?php
                    date_default_timezone_set("Europe/Lisbon"); //definir zona
                    $data=date("Y-m-d"); //definir data
                    $hora = date("H:i:s"); //definir hora

                    if (isset($_GET['verde'])) {
                        $classificacao=$_GET['verde'];
                    }
                    elseif (isset($_GET['amarelo'])) {
                        $classificacao=$_GET['amarelo'];
                    }
                    elseif (isset($_GET['vermelho'])) {
                        $classificacao=$_GET['vermelho'];
                    }


                    if (isset($classificacao)) {
                        $inserir=mysqli_query($link,"INSERT INTO questionario (pergunta_id, pergunta, data, hora, idioma, origem, classificacao) VALUES ('','".$questao[$lng]."','$data','$hora','$lang', '','$classificacao')");
                        if (!$inserir) 
                        {
                            echo "Erro ao inserir na tabela.";
                        }
                        else
                        {
                            header('location: comentario.php');
                        }
                    }
                    ?>
                </div>


As you can see I didn’t insert anything into action, only then it works to enter the data in the database.
The other page I want to pass the variable $lang=$_GET['lang'];

<td colspan="3" class="pergunta" align="center">
            <hr>
            <?php $lang=isset($_GET['lang']) ? $_GET["lang"] : false; 
            $lang=$_GET['lang'];

            if ($lang=='pt') {
                echo "Deixe aqui o seu comentário";
            }elseif ($lang == 'en')
            {
                echo "ENGLISH";
            }elseif ($lang=='sp')
            {
                echo "espanhol";
            }elseif ($lang=='de') {
                echo "alemao";
            }elseif ($lang=='fr') {
                echo "frances";
            }
            ?>

            <hr>
        </td>


If the action contains comentario.php (name of this page) this code works, otherwise it says Notice: Undefined index: lang in

  • It was very confusing. The variable in question is $_GET['lang'], right? What would be the input code? What is its relationship to the problem? What file is it in? What does the file do comentario.php? You want to pass the variable from where to where and what you need to do with it?

  • I’m sorry Andersion! Yes the variable in question is this. I’ll change my question for you to take a look

  • Substitute header('location: comentario.php'); for header("location: comentario.php?lang={$lang}"); and remove $lang=$_GET['lang'];.

  • Valdeir, thanks already, I tried but so does not appear the text, for example if the language was Portuguese on the main page, next should appear "Leave here your comment", simply does not appear anything...

1 answer

1


I don’t know if I understand, but to pass values to other pages you can use the URL, then use $_RESQUEST to get the value.

Example:

você pode montar uma url no Href
<a href='comentario.php?lang=pt-br'>Acessar</a>
Url ficaria +/- assim
    http://pagina.com/comentario.php?lang=pt-br

No php você pode pegar o valor assim
    $lang= $_REQUEST["lang"];

This is a very simple example, it certainly has other forms

  • Rafa I will mark your answer as correct, because I used your example, thank you! To solve fully I used for example: if ($lang != 'fr' && $lang != 'en' && $lang != 'sp' && $lang != 'de') {&#xA; header('location: comentario.php?lang=pt');&#xA; }elseif ($lang == 'en') {&#xA; header('location: comentario.php?lang=en');&#xA; &#xA; } etc.

  • Ball show. I’m glad I helped.

Browser other questions tagged

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