Solve the problem of square brackets in GET form method
P粉300541798
P粉300541798 2024-01-10 16:57:31
0
1
399

I'm working on a form in the 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 located in the aaa.php file and uses the GET method.

When submitting, the aaa.php file will jump to the bbb.php file, which displays the submitted data for user inspection, and then returns to the aaa.php file to modify the data or save it in the database. In the last case, the ccc.php file holds the data.

To accomplish this, I use the GET method to write all the data submitted by aaa.php in the URL into the bbb.php file. Therefore, the PHP command line in bbb.php retrieves the data shown in the URL and writes the data submitted by aaa.php to a link to the ccc.php file so that the ccc.php file can insert the data into the database.

The problem is that the GET method causes the web browser to rewrite the square brackets [] as described below: Form GET key square brackets are encoded on form submission. therefore:

• The bbb.php file cannot display the checkbox value before saving the data;

• If I use the POST method, the bbb.php file displays the checkbox value, but I cannot access the submitted data and rewrite the URL to the ccc.php file.

This 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>';

Using the GET method, the resulting 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 this case?

Thank you for your answers and comments!

P粉300541798
P粉300541798

reply all(1)
P粉147045274

Based on your suggestion, this is how I found the problem:

• aaa.php contains the form, now uses the POST method to submit data;

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

► Change $_GET to $_POST;

► Add session variables to store $_POST data:

$_SESSION['data'] = $_POST;

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

$dump = $_POST['fieldfromform'];

I wrote:

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

Now ccc.php stores data in the database as before!

thanks for your help!

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!