0

I'm working with python 2.7 and CGI.

I have a form in html with multiple inputs (text) named like that :

  • Ex Commande
  • Ex_1 Commande_1
  • Ex_2 Commande_2
  • etc...

The inputs names are in a correct order in the html page. The goal is to associate each "Ex" with the correspondent "Commande". After that, I want to put the values of the inputs into a xml file.

To do so, I use the form.keys() function, but the order of the values returned in the FieldStorage seems to be random !

How could I order the values returned by the form ?

I'm stuck with that for hours, if somebody could help, it will be very nice !

Here is my code which only print keys (without all the xml work) :

form.html :

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <title>Create TP</title>
        <script type="text/javascript">
           var i = 0;

            function addKid()
            {
                if (i < 20)
                {
                    var newRow = document.createElement('tr');

                    newRow.innerHTML = '<td> <input type="text" name="Ex_'+i+'" size="50"><td> <input type="text" name="Commande_'+i+'" size="200"></td><td><input type="button" id="add_kid()" onClick="addKid()" value="+" /><input type="button" value="-" onclick="removeKid(this.parentNode)"></td>';

                    document.getElementById('kids').appendChild(newRow);
                    i++;
                }
            }

            function removeKid(element)
            {
                document.getElementById('kids').removeChild(element.parentNode);
                i--;
            }
        </script>
    </head>
    <body>
        <div>
            <form method="post" action="test.cgi">
                    Nom du TP :&nbsp;&nbsp;&nbsp;
                    <input id="Text1" type="text" name="nomTP" />
                    <br /><br /><br /><br />
                    <table id="kids">
                        <tr>
                            <th>Name exercice</th>
                            <th>Commande</th>
                            <th></th>
                        </tr>
                        <!--<tbody  > -->
                            <tr >
                                <td  >
                                    <input type="text" name="Ex" size="50">
                                </td>
                                <td>
                                    <input type="text" name="Commande" size="200">
                                </td>
                                <td>
                                    <input type="button" id="add_kid" onClick="addKid()" value="+" />
                                </td>
                            </tr>
                        <!--</tbody> -->
                    </table>
                    <div style="height: 23px; width: 118px; margin-left : 69px;  margin-top: 15px">
                        <input id="valider" type="submit" value="Valider" />
                    </div> 
            </form>
        </div>
    </body>
</html>

test.cgi :

#! c:\Program Files (x86)\Python27\python.exe
# -*- coding: utf-8 -*-

import cgitb, cgi, sys
sys.stderr = sys.stdout
cgitb.enable()

print "content-type : text/html\n\n";
print

form = cgi.FieldStorage()
TP = form.getvalue("nomTP")


entete = """<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>Creation TP</title></head><body>"""
pied_page = """</body></html>"""



print entete

for key in form.keys():
    #variable = str(key)
    print key

    #if variable.startswith( 'Ex' ):
        #do something

    #if variable.startswith( 'Commande' ):
        #do something


print pied_page
harold
  • 1
  • It is not random, there is [reason behind the madness](https://stackoverflow.com/questions/15479928/why-is-the-order-in-python-dictionaries-and-sets-arbitrary). – Martijn Pieters Mar 13 '16 at 12:28
  • Thank you ! I didn't know about the hash table storage. Instead of use `OrderedDict()`, I made a `sort()` on the `form.keys()` and I used some conditions after that. My xml looks great now ! – harold Mar 13 '16 at 13:53

0 Answers0