Jump to content

How to get undetermined number of variables, and insert them into MySQL db...


Guest kilbad

Recommended Posts

I have created a javascript that can be used to collect an undetermined number of inputs.  The code for this script is::

 

<html>
<head>
<script type="text/javascript">
var alt=1;
function addAllergy(f){
try{
var inp=document.createElement('<input type="text" name="allergy'+alt+'">')
}catch(E){
var inp=document.createElement('input');
inp.type = "text";
inp.name = 'allergy'+alt;
}
nam = f.elements[f.elements.length-2];
var b = document.createElement('br');
f.insertBefore(b,nam);
f.insertBefore(inp,b);
alt++;
return true;
}

</script>
</head>
<body>
<form action="ALL.module.php">
Allergies:<br>
<input type="text" name="allergy0"><br>
<input type="button" value="Add Another Allergy" onclick="addAllergy(this.form)"><br>
<input type="submit" value="Add Patient Allergies">
</form>
</body>
</html>

 

My question is, in the script that will process this form, how go I use $_POST to get all those variables when I do not know how many there will be?

 

My ultimate goal is to insert each allergy in a separate MySQL table column with the same name as its variable name (i.e. allergy0, allergy1, allergy2, etc.); so, I will also need to check if there is a column already made for that particular variable, if not, create it, and then insert it.

 

Thank you all in advance!

 

Brendan

@fert - Thank you so much for your quick reply.  I really appreciate your help.

 

I looked at the manual references you cited, but wanted to know if you could help me apply those functions because I am not sure exactly how.

 

Regardless, thank you for your time!

 

Brendan

Instead of creating columns in a table (poor design strategy) and ending up with

recid | somedata | allergy0 | allergy1 | .... | allergyN

 

split it into two tables, mytable and allergy. Add each allergy as a row in the second table along with id of main record. You can then have as many allergies per main record as you need without altering any tables. (read up on "data normalization")

 

[pre]

STRUCTURE

 

mytable            allergy

=========          ===========

recid    ----+    allergyid

somedata    +---- mainid                 

                  allergy                 

 

DATA

 

recid  |  somedata                allergyid |mainid | allergy

-------+--------------            ----------+-------+----------------

  1    |    abc                      1    |  1  |  Nuts         

  2    |    def                      2    |  1  |  Cats         

  3    |    xyz                      3    |  1  |  Strawberries         

                                      4    |  3  |  Gerbils         

                                      5    |  3  |  Bee stimgs 

[/pre]

 

 

 

 

@Barand - That seems like a better design strategy, which I will definitely use.  I am also reading up on data normalization.  With that being said, thank you for your help!

 

However, I still need help actually getting the undetermined number of variables from the script shown above, so that I can insert them to a second table as we have discussed.

 

Thanks again,

Brendan

Each time you create an allergy field, give it the name "allergy[]"

 

They will then be posted as an array of values.

 

To process

 

<?php
$id = $_POST['id'];

foreach ($_POST['allergy'] as $val) {

     $sql = "INSERT INTO allergy (mainid, allergy)
               VALUES ('$id', '$val')";                   
     mysql_query ($sql);
}
?>

 

 

Here's an example without the auto-generation of the input fields

 

<?php
if (isset($_POST['allergy'])) {
    $id = $_POST['id'];

    foreach ($_POST['allergy'] as $val) {
          if ($val) {
                 $sql = "INSERT INTO allergy (mainid, allergy)
                           VALUES ('$id', '$val')";                   
                 mysql_query ($sql);
          }  
    }
}
?>
<form method='post' >
User ID : <input type="text" name="id" size="12"><br/><br/>

Allergies<br/><br/>
<?php 
    for ($i=0; $i<5; $i++) {
        for ($j=0; $j<4; $j++) {
            echo "<div style='float:left; margin:5px'>
                  <input type='text' name='allergy[]' size='20'>
                  </div> ";
        }
        echo "<br style='clear:both' />";
    }

?>

<input type='submit' name='action' value='Submit'>
</form>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.