Problems sending many variables through _GET

Asked

Viewed 206 times

2

I apologize if I am redoing this question, I searched a lot and found nothing of the type.

My problem is that I have to send many variables using $_GET using two pages

the following code is from pagina-cadastro.php

if($_GET['v1'] != '' || ($_GET['v2'] != '') || ($_GET['v3'] != '') || ($_GET['v4'] != '') || ($_GET['v5'] != '') || ($_GET['v6'] != '') || ($_GET['v7'] != '') || 
        ($_GET['v8'] != '') || ($_GET['v9'] != '') || ($_GET['v10'] != '')

$razaoSocial = $_GET['v1'];
        $nomeFantasia = $_GET['v2'];
        $cnpj = $_GET['v3'];
        $email = $_GET['v4'];
        $inscrEstadual = $_GET['v5'];
        $inscrMunicipal = $_GET['v6'];
        $cep = $_GET['v7'];
        $rua = $_GET['v8'];
        $numero = $_GET['v9'];
        $complemento = $_GET['v10'];
    } else {
        $razaoSocial = "";
        $nomeFantasia = "";
        $cnpj = "";
        $email = "";
        $inscrEstadual = "";
        $inscrMunicipal = "";
        $cep = "";
        $rua = "";
        $numero = "";
        $complemento = "";

and the page cadastrar.php is

"<script> window.location = 'pagina-cadastro.php?&v1=".$razaoSocial."&v2=".$nomeFantasia . "&v3=".$cnpj . "&v4=". $email ."&v5=". $inscrEstadual . "&v6=".$inscrMunicipal . "&v7=" .$cep . "&v8=" .$rua . "&v9=" .$numero . "&v10=" .$complemento

I posted only 10 variables, but I have to do 150. How can I optimize my time not to write one by one?

  • You need to leave these variables with these names (v1, v2...)? I couldn’t put a more informative name?

  • 1

    GET requests have a maximum character limit. With these short names plus values and connectors (?, = and &) plus dominion, path etc. surprise would be if it worked. @Luizfernandosanches, there is some reason not to do via POST?

  • There’s a typo in the code, missing key { to open the if.

  • 1

    As @Brunoaugusto Augusto has already commented, what is the need to use GET and not POST?

  • I found a better solution to my problem, I will validate the fields with javascript and only release the button when the information is correct, because of the performance, I thank everyone who promptly helped me, thank you very much

  • @Luizfernandosanches even if you do it with javascript you must do the same on the server, in your case in php, because you should not trust what the client sends, someone can modify the request and send any information instead

Show 1 more comment

5 answers

1

You could do something like this:

// coloque todos os valores necessários no array
$arr = array(
        'v2' => 'nomeFantasia',
        'v3' => 'cnpj'
    );

// percorre o array
foreach ($arr as $get => $nome) {
    // verifica se existe o índice desejado no
    // array $_GET e se é diferente de ''
    if (isset($_GET[$get]) && $_GET[$get] != '') {
        // seta a variável com o valor correto
        $$nome = $_GET[$get];

        // pesquise sobre variáveis variáveis
        // http://php.net/manual/en/language.variables.variable.php
    } else {
        $$nome = '';
    }
}

But I think you should review your logic. If you are putting the values into variables in this way, it means you will use them all manually again. Your code should be extremely large and confusing.

Search for php tutorials and see other people’s code. A lot of knowledge can be acquired this way.

0

On the client side just send with a Submit button that all form data will be sent

Exp:

<form id="frmCad" name="frmCad" method="get" action="suapag.php">

<input type="text" id="1" name="1">
<input type="text" id="2" name="2">
<input type="text" id="3" name="3">
<input type="text" id="4" name="4">
<input type="text" id="5" name="5">
<input type="text" id="6" name="6">

<input id="cmdSalvar" name="cmdSalvar" type="submit" value="Salvar">

</form>

As there are many fields, I recommend the use of POST, instead of GET, otherwise the url of the page that receives the data will get VERY large...

If you want to do something more sophisticated by sending via Ajax, or JSON, just serializar all fields....

Using the library Jquery

Exp:

$("frmCad").submit(function( event ) {
  console.log( $( this ).serializeArray() );
  event.preventDefault();
});

Depending on your real need, I often choose to make a simple form and submit the form according to the first option, only if there is a need not to happen a refresh on the page I would choose the second, remains simple, however...

On the server side is just you do the treatment of variables according to your need.... and do not know any loophole, on the server side you should do variable treatment by variable....

Note: What @Oeslei commented is important, it is generally good to give a real name to variable, and leave documented, it facilitates in very future code maintenance, the times of work and tiredness, but is the most recommended and is part of good programming practices...

Exp: variable CursoEscolhido, try to abbreviate as much as possible the bridge to be understandable by anyone who will then open their code and do a maintenance, curEsco for example

-1

According to what Nelson said, do something like this:

$json = file_get_contents('url_aqui');
$obj = json_decode($json);
echo $obj->access_token;

For this to work, file_get_contents requires allow_url_fopen to be enabled. If this doesn’t work, you can just use Curl to get the url:

http://php.net/manual/en/function.curl-init.php

BUT I DON’T THINK THAT’S YOUR PROBLEM. Do you want to automate your GET request in a way that reduces your work in the placement of more than 50 variables? Dude, your logic is linear, it requires you to write something sequential. Therefore, you cannot create another type of logic, which removes this work of writing 100 variables. I believe there is no solution to what you want.

-1

When you see that you are starting to use many variables to just store values, it is best to move everything to one array.

But as for your code, the conditions you are using are redundant so you can soon move on to building the script as follows:

<script>
window.location = 'pagina-cadastro.php?
<?php
    foreach($_GET as $key => $val){
        echo $key."=".$val."&";
    }
?>';
</script>

-3

First in the client using javascript pass the whole form in json format to PHP and then on the server, first decode the json and then parse the result.

Browser other questions tagged

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