Guest kilbad Posted February 25, 2007 Share Posted February 25, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/40002-how-to-get-undetermined-number-of-variables-and-insert-them-into-mysql-db/ Share on other sites More sharing options...
fert Posted February 25, 2007 Share Posted February 25, 2007 http://us2.php.net/manual/en/function.count.php http://us2.php.net/manual/en/control-structures.foreach.php Quote Link to comment https://forums.phpfreaks.com/topic/40002-how-to-get-undetermined-number-of-variables-and-insert-them-into-mysql-db/#findComment-193453 Share on other sites More sharing options...
Guest kilbad Posted February 25, 2007 Share Posted February 25, 2007 @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 Quote Link to comment https://forums.phpfreaks.com/topic/40002-how-to-get-undetermined-number-of-variables-and-insert-them-into-mysql-db/#findComment-193458 Share on other sites More sharing options...
Barand Posted February 25, 2007 Share Posted February 25, 2007 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] Quote Link to comment https://forums.phpfreaks.com/topic/40002-how-to-get-undetermined-number-of-variables-and-insert-them-into-mysql-db/#findComment-193567 Share on other sites More sharing options...
Guest kilbad Posted February 25, 2007 Share Posted February 25, 2007 @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 Quote Link to comment https://forums.phpfreaks.com/topic/40002-how-to-get-undetermined-number-of-variables-and-insert-them-into-mysql-db/#findComment-193676 Share on other sites More sharing options...
Barand Posted February 25, 2007 Share Posted February 25, 2007 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); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/40002-how-to-get-undetermined-number-of-variables-and-insert-them-into-mysql-db/#findComment-193763 Share on other sites More sharing options...
Guest kilbad Posted February 25, 2007 Share Posted February 25, 2007 I tried the above script, and used "allergy[]" for the name of each input field, but am getting an "Invalid argument supplied for foreach()" error.. any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/40002-how-to-get-undetermined-number-of-variables-and-insert-them-into-mysql-db/#findComment-193834 Share on other sites More sharing options...
Barand Posted February 25, 2007 Share Posted February 25, 2007 Is your form method GET or POST? Has any data been submitted to the page? Quote Link to comment https://forums.phpfreaks.com/topic/40002-how-to-get-undetermined-number-of-variables-and-insert-them-into-mysql-db/#findComment-193868 Share on other sites More sharing options...
Guest kilbad Posted February 28, 2007 Share Posted February 28, 2007 My form method is POST, and data has been submitted to the page. Maybe I am misunderstanding what you mean by "allergy[]"?.. any ideas? Thanks again, Brendan Quote Link to comment https://forums.phpfreaks.com/topic/40002-how-to-get-undetermined-number-of-variables-and-insert-them-into-mysql-db/#findComment-196335 Share on other sites More sharing options...
Barand Posted February 28, 2007 Share Posted February 28, 2007 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> Quote Link to comment https://forums.phpfreaks.com/topic/40002-how-to-get-undetermined-number-of-variables-and-insert-them-into-mysql-db/#findComment-196367 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.