Square brackets problem in GET form method

223 views Asked by At

I'm working on a form in my aaa.php file, with checkboxes using square brackets in the "name" parameter :

<form method="get" action="bbb.php">
     <input type="checkbox" id="eleve_element_18" name="eleve_element_18[]" value="1">
</form>

The form is in the aaa.php file and uses the GET method.

On submit, the aaa.php file heads to the bbb.php file which displays the submitted data to allow users to check it and then whether go back to the aaa.php file to modify the data or to save it in the database. In this last case, the ccc.php file saves the data.

To make this work, I used the GET method to write all de data submitted by aaa.php in the URL to the bbb.php file. So, a PHP command line in bbb.php retrieves the data displayed in the URL and writes a link to the ccc.php file with the data submitted by aaa.php, so that the ccc.php file can insert the data in the database.

The issue is that GET method makes the web browser rewrite the square brackets [] as explained here : Form GET key square brackets encoded upon form submission. Thus :

• the bbb.php file can't display the checkboxes values before saving data;

• if I use the POST method, the bbb.php file displays the checkboxes values but I lose access to the submitted data to rewrite the URL to the ccc.php file.

Here is the code :

• aaa.php

       //This part of the code writes the checkboxes lines
       $listeelementssignifiants = $dbco->query('SELECT * from referentiels');
        
       while ($referentiel=$listeelementssignifiants->fetch()) {
            echo '<input type="checkbox" id="eleve_element_' . $eleves['numero_eleve'] . '" name="eleve_element_' . $eleves['numero_eleve'] . '[]" value="' . $referentiel['numero'] . '"><label class="elementsignifiant" for="eleve_element_' . $eleves['numero_eleve'] . '">' . $referentiel['element'] . '</label><br>';
    }

• bbb.php

   //These lines retrieve the URL written by the GET method and build a new URL to ccc.php
   if(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on') 
        $url = "https://"; 
   else
        $url = "http://";
            
   $url .= $_SERVER['HTTP_HOST'] . str_replace("bbb.php", "ccc.php", $_SERVER['REQUEST_URI']);
        
    
   //Bouton Valider
   echo '<button type="button" onclick="location.href=\'' . $url . '\'" class="valider">Valider</button>';
   

With the GET method, the result URL is :

https://domainname/bbb.php?...&matiere_18=ulis&numero_eleve=18&objectifs_18=dfsdfs&activites_18=sdfsdgfdgdfgd&aesh_18=fgdfgdfgd&commentaires_18=fghdfhqdhdghd&eleve_element_18%5B%5D=1&eleve_element_18%5B%5D=47&eleve_element_18%5B%5D=73

Is it possible to keep the square brackets and use the GET method in that case ?

Thanks for your answers and comments!

1

There are 1 answers

0
Fabien Nguyen On

Following your advices, here's the way I found to my problem :

• aaa.php contains the form and now submits the data with the POST method;

• bbb.php reads the data in $_POST and displays the result. There are two code modifications in bbb.php :

► change $_GET ingo $_POST;

► add a session variable to store the $_POST data :

$_SESSION['data'] = $_POST;

• ccc.php reads the session variable as if it was the $_POST. Instead of having :

$dump = $_POST['fieldfromform'];

I wrote :

$dump = $_SESSION['data']['fieldfromform'];

Now ccc.php stores data in the database as it used to!

Thanks for your help!