Problems sending many variables by _GET

2

I apologize if I'm redoing this question, I searched a lot and did not find anything like it.

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 cadastrar.php page 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've posted only 10 variables, but I have to do 150. How can I optimize my time not to write one by one?

    
asked by anonymous 09.01.2015 / 14:32

5 answers

1

I'd like to 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/pt_BR/language.variables.variable.php
    } else {
        $$nome = '';
    }
}

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

Look for php tutorials and look at other people's code. Much knowledge can be gained in this way.

    
09.01.2015 / 15:01
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>

Since there are many fields, I recommend using POST , instead of GET , otherwise the url of the page that receives the data will be VERY large ...

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

Using the Jquery

Exp:

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

Depending on your actual need, I often choose to make a simple form and submit the form according to the first option, only if there is a need to not be able to refresh on the page I would opt for the second, continue being simple, yet ...

On the server side you only have to treat the variants according to your needs .... and I do not know of any escape, on the server side you have to do multi-variable treatment ....

Note: What @Oeslei commented on is important, it's usually good to give a real name to the variable, and letting it documented, this makes future code maintenance very easy, sometimes it's tiring and tiring, but it's the most recommended and is part of good programming practice ...

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

    
09.01.2015 / 14:48
-1

When you see that you are beginning to use lots of variables to just store values, it is best to pass everything to array .

But as for your code, the conditions you are using are redundant so you can proceed to the script as follows:

<script>
window.location = 'pagina-cadastro.php?
<?php
    foreach($_GET as $key => $val){
        echo $key."=".$val."&";
    }
?>';
</script>
    
09.01.2015 / 19:03
-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 that allow_url_fopen is enabled. If it does not work, you can just use curl to get the url:

link

BUT I BELIEVE THAT THIS IS NOT YOUR PROBLEM. Do you want to automate your GET request in a way that slows down your work by placing more than 50 variables? Dude, your logic is linear, it requires you to write something sequentially. Therefore, you can not create another type of logic, which removes this your work from writing 100 variables. I believe there is no solution for what you want.

    
09.01.2015 / 14:51
-3

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

    
09.01.2015 / 14:37