Bisa Posted October 15, 2008 Share Posted October 15, 2008 First of all, I suck at arrays, I never really understood how they work, that said here is my problem. I have been working all day on this form where the user can add more fields if they need to by submitting the form and thus getting back to the same form but with one extra field, I've got some static fields and I've managed to transmit the values of those fields so the only thing the user sees is the same form with his/her values and a new field to add something new into. Now this is the problem, while I am able to transmit the static fields values I cant get my head around how I would do with the dynamic fields as the amount of fields (and thus the $_POST['dynamicfield'] variables) are not the same from one user to the other. This is how my code looks like. //My attempt to retrieve and put the current dynamic values to use. for ($t=0; $t<$i; $t++) { $y = "artnr" . $t; $x = "antal" . $t; $artnr[$t] = secureFormInput($_POST['$y']); $antal[$t] = secureFormInput($_POST['$x']); } /*This works to the extent that I am able to add the "old" fields and a new one but I am not able to get the old values to transmit from the old form. (I suspect it has to do with the array but as I said I suck at arrays).*/ for($t=0; $t<$i; $t++) { echo "<tr><td><label>"; echo "<input name=\"artnr" . $t . "\" type=\"text\" value=\""; $artnr[$t]; echo "\" size=\"15\" maxlength=\"3\" />"; echo "</label></td>"; echo "<td><label>"; echo "<input name=\"antal" . $t . "\" type=\"text\" value=\""; $antal[$t]; echo "\" size=\"6\" />"; echo "</label></td>"; echo "<td><label>"; echo "<input name=\"artnr"; $t++; echo $t . "\" type=\"text\" value=\""; $artnr[$t]; echo "\" size=\"15\" maxlength=\"3\" />"; echo "</label></td>"; echo "<td><label>"; echo "<input name=\"antal" . $t; echo "\" type=\"text\"value=\""; $artnr[$t]; $t--; echo "\" size=\"6\" />"; echo "</label></td><tr>"; } Quote Link to comment https://forums.phpfreaks.com/topic/128612-solved-dynamic-form-fieldsarrays-can-i-have-some1-look-at-this-please/ Share on other sites More sharing options...
Bisa Posted October 16, 2008 Author Share Posted October 16, 2008 ok, if I rephrase the question: Am I using the arrays in an appropriate way? Quote Link to comment https://forums.phpfreaks.com/topic/128612-solved-dynamic-form-fieldsarrays-can-i-have-some1-look-at-this-please/#findComment-666944 Share on other sites More sharing options...
kenrbnsn Posted October 16, 2008 Share Posted October 16, 2008 Can we see how the form is set up? Ken Quote Link to comment https://forums.phpfreaks.com/topic/128612-solved-dynamic-form-fieldsarrays-can-i-have-some1-look-at-this-please/#findComment-666948 Share on other sites More sharing options...
Bisa Posted October 16, 2008 Author Share Posted October 16, 2008 sure thing, I'll include the lot: <?php function secureFormInput($data) //Säkerhetsfunktion för att rensa bort skadlig input { $data = ereg_replace("[\'\")(;|`,<>]", "", $data); stripslashes($data); return $data; } $i = $_POST['i']; //Antalet artiklar som lagt till + 1 $i++; $addfields = $_POST['addfields']; if(strlen($addfields) != "0") //Om "Lägg till fler rader"-knappen har använts: $submit = 0; else { if ($i == 1) $submit = 0; else { $submit = 1; echo "Vänligen granska din beställning och klicka sedan pä \"Skicka\" en gäng till. <br /><br />"; } } //Tilldelar variabler värden från $_POST och säkrar input fån skadlig kod $namn = secureFormInput($_POST['namn']); $tele = secureFormInput($_POST['tele']); $teldag = $_POST['teldag']; $telkvall = $_POST['telkvall']; $mobil = secureFormInput($_POST['mobil']); $mobdag = $_POST['mobdag']; $mobkvall = $_POST['mobkvall']; $adress = secureFormInput($_POST['adress']); $postnr = secureFormInput($_POST['postnr']); $stad = secureFormInput($_POST['stad']); $meddelande = secureFormInput($_POST['meddelande']); for ($t=0; $t<$i; $t++) { $y = "artnr" . $t; $x = "antal" . $t; $artnr[$t] = secureFormInput($_POST['$y']); $antal[$t] = secureFormInput($_POST['$x']); //A temporary echo while dev. to see the variable values. echo $artnr[$t] . $t . "<br />"; echo $antal[$t] . $t . "<br /><br />"; } ?> <form class="bform" method="post" action="index.php?do=2"> <?php //För antalet fält vidare via FORM echo "<input name=\"i\" type=\"hidden\" value=\"" . $i . "\" />"; ?> <table width="400" border="0" cellspacing="0"> <tr> <td>Artikelnummer</td> <td>Antal</td> <td>Artikelnummer</td> <td>Antal</td> </tr> <?php //Skapar gammla fält och lägger till ett nytt for($t=0; $t<$i; $t++) { echo "<tr><td><label>"; echo "<input name=\"artnr" . $t . "\" type=\"text\" value=\""; $artnr[$t]; echo "\" size=\"15\" maxlength=\"3\" />"; echo "</label></td>"; echo "<td><label>"; echo "<input name=\"antal" . $t . "\" type=\"text\" value=\""; $antal[$t]; echo "\" size=\"6\" />"; echo "</label></td>"; echo "<td><label>"; echo "<input name=\"artnr"; $t++; echo $t . "\" type=\"text\" value=\""; $artnr[$t]; echo "\" size=\"15\" maxlength=\"3\" />"; echo "</label></td>"; echo "<td><label>"; echo "<input name=\"antal" . $t; echo "\" type=\"text\"value=\""; $artnr[$t]; $t--; echo "\" size=\"6\" />"; echo "</label></td><tr>"; } /* <tr> <td><label> <input name="artnr1" type="text" value="artnr1" size="15" maxlength="3" /> </label></td> <td><label> <input name="antal1" type="text" value="antal1" size="6" /> </label></td> <td><label> <input name="artnr2" type="text" value="artnr2" size="15" maxlength="3" /> </label></td> <td><label> <input name="antal2" type="text" value="antal2" size="6" /> </label></td> </tr> */ ?> <tr> <td colspan="4"><input type="submit" name="addfields" value="Lägg till fler artiklar" id="addfields" /></td> </tr> </table> <br /> <table width="400" border="0" cellpadding="0" cellspacing="0"> <tr> <td colspan="2">Vid beställning kommer vi att kontakta er för en bekräftelse per telefon!<br> <br> Vänligen uppge namn och minst ett telefonnummer där nä er dag- eller kvällstid.<br></td> </tr> <tr> <td colspan="2"> </td> </tr> <tr> <td width="60">Namn:</td> <td width="340"><label> <input name="namn" type="text" value="<?php echo $namn; ?>" size="30"> </label></td> </tr> <tr> <td>Tele:</td> <td width="340"><label> <input name="tele" type="text" value="<?php echo $tele; ?>" size="15" maxlength="9"> Dag: <input name="teldag" type="checkbox" <?php if ($teldag == "on") echo "checked=\"checked\""; ?>> Kväll: <input type="checkbox" name="telkvall" <?php if ($telkvall == "on") echo "checked=\"checked\""; ?>> </label></td> </tr> <tr> <td>Mobil:</td> <td width="340"><label> <input name="mobil" type="text" value="<?php echo $mobil; ?>" size="15" maxlength="10"> Dag: <input type="checkbox" name="mobdag" <?php if ($mobdag == "on") echo "checked=\"checked\""; ?>> Kväll: <input type="checkbox" name="mobkvall" <?php if ($mobkvall == "on") echo "checked=\"checked\""; ?>> </label></td> </tr> </table> <br> <table width="400" border="0" cellpadding="0" cellspacing="0"> <tr> <td colspan="2">Vid leverans per post ser vi gärna att ni fyller i en leveransadress.</td> </tr> <tr> <td colspan="2"> </td> </tr> <tr> <td width="60">Adress:</td> <td width="340"><label> <input name="adress" type="text" value="<?php echo $adress; ?>" size="30"> </label></td> </tr> <tr> <td>Postnr:</td> <td width="340"><label> <input name="postnr" type="text" value="<?php echo $postnr; ?>" size="10" maxlength="5"> </label></td> </tr> <tr> <td>Stad:</td> <td width="340"><label> <input name="stad" type="text" value="<?php echo $stad; ?>" size="20"> </label></td> </tr> </table> <br> <table width="400" border="0" cellspacing="0"> <tr> <td>Vill ni bifoga ett meddelande eller några speciella önskemål med er beställning går det bra att göra det i textrutan här nedanför.</td> </tr> <tr> <td><label> <textarea name="meddelande" cols="60" rows="8"><?php echo $meddelande; ?></textarea> </label></td> </tr> </table> <br> <p> <label></label> <input name="submit" type="submit" value="Skicka" /> </p> </form> (most comments are made in Swedish as well as the text output - I figured ppl would hopefully understand just by checking the code but if need be I will be able to translate it for you) Quote Link to comment https://forums.phpfreaks.com/topic/128612-solved-dynamic-form-fieldsarrays-can-i-have-some1-look-at-this-please/#findComment-666951 Share on other sites More sharing options...
Bisa Posted October 17, 2008 Author Share Posted October 17, 2008 Any chance the form shed some light on the problem? Quote Link to comment https://forums.phpfreaks.com/topic/128612-solved-dynamic-form-fieldsarrays-can-i-have-some1-look-at-this-please/#findComment-667984 Share on other sites More sharing options...
Daniel0 Posted October 17, 2008 Share Posted October 17, 2008 If you give a form field (or multiple fields) the name foo[] then you can access $_POST['foo'] on the processing page as an array. You can also add an index inside the square brackets exactly like you would when working with arrays normally. Quote Link to comment https://forums.phpfreaks.com/topic/128612-solved-dynamic-form-fieldsarrays-can-i-have-some1-look-at-this-please/#findComment-668086 Share on other sites More sharing options...
Bisa Posted October 17, 2008 Author Share Posted October 17, 2008 If you give a form field (or multiple fields) the name foo[] then you can access $_POST['foo'] on the processing page as an array. You can also add an index inside the square brackets exactly like you would when working with arrays normally. I see, thank you, however - would I have to name the field specifically using the []? In my code it would then be for example namn[]? Quote Link to comment https://forums.phpfreaks.com/topic/128612-solved-dynamic-form-fieldsarrays-can-i-have-some1-look-at-this-please/#findComment-668134 Share on other sites More sharing options...
Daniel0 Posted October 17, 2008 Share Posted October 17, 2008 Exactly. Also, if you're going to display the form again then you'll have to remember to use the indexes as well. Quote Link to comment https://forums.phpfreaks.com/topic/128612-solved-dynamic-form-fieldsarrays-can-i-have-some1-look-at-this-please/#findComment-668141 Share on other sites More sharing options...
Bisa Posted October 17, 2008 Author Share Posted October 17, 2008 cheers, as I said in my original post I never really understood arrays (I never had to use them before ) any way, thank you, I'm currently in the works of giving my code a more advanced set of comments in English for my own sake, I'll get back if I run into any more problems later. Quote Link to comment https://forums.phpfreaks.com/topic/128612-solved-dynamic-form-fieldsarrays-can-i-have-some1-look-at-this-please/#findComment-668149 Share on other sites More sharing options...
Bisa Posted October 17, 2008 Author Share Posted October 17, 2008 okey, it's not working and I think I did what Daniel just said (obviously I did not as it isn't working but yea...) so could anyone check my changes and see if I missed something? First of all, I'm not sure if handling the arrays like this is valid or if I by doing this won't get all the data out of them etcetera? $artnr[] = secureFormInput($_POST['artnr[]']); $antal[] = secureFormInput($_POST['antal[]']); Next up where I create the dynamic fields and try to enter the user submitted data looks like this (where $i is the number of times this form has been resubmitted to create new fields) for($t=0; $t<$i; $t++) { echo "<tr><td><label>"; echo "<input name=\"artnr[]\" type=\"text\" value=\""; $artnr[$t]; echo "\" size=\"15\" maxlength=\"3\" />"; echo "</label></td>"; echo "<td><label>"; echo "<input name=\"antal[]\" type=\"text\" value=\""; $antal[$t]; echo "\" size=\"6\" />"; echo "</label></td>"; echo "<td><label>"; echo "<input name=\"artnr[]\" type=\"text\" value=\""; $t++; echo $artnr[$t]; echo "\" size=\"15\" maxlength=\"3\" />"; echo "</label></td>"; echo "<td><label>"; echo "<input name=\"antal[]\" type=\"text\"value=\""; echo $artnr[$t]; $t--; echo "\" size=\"6\" />"; echo "</label></td><tr>"; } Figured I had missed something important and would appreciate if anyone could check up on me Here comes the entire code with the applied changes if you want to try it out locally. (and English comments ) <?php //Function to prevents SQL Injection function secureFormInput($formdata) { $formdata = ereg_replace("[\'\")(;|`,<>]", "", $formdata); $formdata = mysql_real_escape_string(trim($formdata)); return stripslashes($formdata); } //i represents the number of times this form has been submitted back to itself $i = $_POST['i']; $i++; //If $_POST['addfields'] has been set it means the user wants to add another field if(isset($_POST['addfields'])) $submit = 0; else { if ($i == 1) //If i==1 the form is viewed for the first time and thus should not be submitted $submit = 0; else { $submit = 1; echo "Vänligen granska din beställning och klicka sedan pä \"Skicka\" en gäng till. <br /><br />"; } } //Secures the input and fetches user input data everytime but the first time the user $artnr[] = secureFormInput($_POST['artnr[]']); $antal[] = secureFormInput($_POST['antal[]']); $namn = secureFormInput($_POST['namn']); $tele = secureFormInput($_POST['tele']); $teldag = $_POST['teldag']; $telkvall = $_POST['telkvall']; $mobil = secureFormInput($_POST['mobil']); $mobdag = $_POST['mobdag']; $mobkvall = $_POST['mobkvall']; $adress = secureFormInput($_POST['adress']); $postnr = secureFormInput($_POST['postnr']); $stad = secureFormInput($_POST['stad']); $meddelande = secureFormInput($_POST['meddelande']); ?> <form class="bform" method="post" action="index.php?do=2"> <?php //Adds a hidden field with the value $i to keep track of how many times the form has been submitted to itself (how many extra fields that have been created) echo "<input name=\"i\" type=\"hidden\" value=\"" . $i . "\" />"; ?> <table width="400" border="0" cellspacing="0"> <tr> <td>Artikelnummer</td> <td>Antal</td> <td>Artikelnummer</td> <td>Antal</td> </tr> <?php //Re-creates the old fields along with the users submitted values and adds one new set of fields for($t=0; $t<$i; $t++) { echo "<tr><td><label>"; echo "<input name=\"artnr[]\" type=\"text\" value=\""; $artnr[$t]; echo "\" size=\"15\" maxlength=\"3\" />"; echo "</label></td>"; echo "<td><label>"; echo "<input name=\"antal[]\" type=\"text\" value=\""; $antal[$t]; echo "\" size=\"6\" />"; echo "</label></td>"; echo "<td><label>"; echo "<input name=\"artnr[]\" type=\"text\" value=\""; $t++; echo $artnr[$t]; echo "\" size=\"15\" maxlength=\"3\" />"; echo "</label></td>"; echo "<td><label>"; echo "<input name=\"antal[]\" type=\"text\"value=\""; echo $artnr[$t]; $t--; echo "\" size=\"6\" />"; echo "</label></td><tr>"; } ?> <tr> <td colspan="4"><input type="submit" name="addfields" value="Lägg till fler artiklar" id="addfields" /></td> </tr> </table> <br /> <table width="400" border="0" cellpadding="0" cellspacing="0"> <tr> <td colspan="2">Vid beställning kommer vi att kontakta er för en bekräftelse per telefon!<br> <br> Vänligen uppge namn och minst ett telefonnummer där nä er dag- eller kvällstid.<br></td> </tr> <tr> <td colspan="2"> </td> </tr> <tr> <td width="60">Namn:</td> <td width="340"><label> <input name="namn" type="text" value="<?php echo $namn; ?>" size="30"> </label></td> </tr> <tr> <td>Tele:</td> <td width="340"><label> <input name="tele" type="text" value="<?php echo $tele; ?>" size="15" maxlength="9"> Dag: <input name="teldag" type="checkbox" <?php if ($teldag == "on") echo "checked=\"checked\""; ?>> Kväll: <input type="checkbox" name="telkvall" <?php if ($telkvall == "on") echo "checked=\"checked\""; ?>> </label></td> </tr> <tr> <td>Mobil:</td> <td width="340"><label> <input name="mobil" type="text" value="<?php echo $mobil; ?>" size="15" maxlength="10"> Dag: <input type="checkbox" name="mobdag" <?php if ($mobdag == "on") echo "checked=\"checked\""; ?>> Kväll: <input type="checkbox" name="mobkvall" <?php if ($mobkvall == "on") echo "checked=\"checked\""; ?>> </label></td> </tr> </table> <br> <table width="400" border="0" cellpadding="0" cellspacing="0"> <tr> <td colspan="2">Vid leverans per post ser vi gärna att ni fyller i en leveransadress.</td> </tr> <tr> <td colspan="2"> </td> </tr> <tr> <td width="60">Adress:</td> <td width="340"><label> <input name="adress" type="text" value="<?php echo $adress; ?>" size="30"> </label></td> </tr> <tr> <td>Postnr:</td> <td width="340"><label> <input name="postnr" type="text" value="<?php echo $postnr; ?>" size="10" maxlength="5"> </label></td> </tr> <tr> <td>Stad:</td> <td width="340"><label> <input name="stad" type="text" value="<?php echo $stad; ?>" size="20"> </label></td> </tr> </table> <br> <table width="400" border="0" cellspacing="0"> <tr> <td>Vill ni bifoga ett meddelande eller några speciella önskemål med er beställning går det bra att göra det i textrutan här nedanför.</td> </tr> <tr> <td><label> <textarea name="meddelande" cols="60" rows="8"><?php echo $meddelande; ?></textarea> </label></td> </tr> </table> <br> <p> <label></label> <input name="submit" type="submit" value="Skicka" /> </p> </form> Quote Link to comment https://forums.phpfreaks.com/topic/128612-solved-dynamic-form-fieldsarrays-can-i-have-some1-look-at-this-please/#findComment-668236 Share on other sites More sharing options...
Daniel0 Posted October 17, 2008 Share Posted October 17, 2008 Just quickly glancing over it, this is where you got it wrong: $artnr[] = secureFormInput($_POST['artnr[]']); $antal[] = secureFormInput($_POST['antal[]']); $_POST['artnr'] and $_POST['antal'] are arrays, so what you'd do is: $artnr = array_map('secureFormInput', $_POST['artnr']); $antal = array_map('secureFormInput', $_POST['antal']); That'll sanitize the arrays using your function. You can try to print_r($artnr); and print_r($antal); after you've done that to get an idea of how it works. Quote Link to comment https://forums.phpfreaks.com/topic/128612-solved-dynamic-form-fieldsarrays-can-i-have-some1-look-at-this-please/#findComment-668376 Share on other sites More sharing options...
Bisa Posted October 18, 2008 Author Share Posted October 18, 2008 If I resubmit the form to itself a few times after changing the sanitation lines you suggested I get the following while printing: Array ( [0] => [1] => [2] => [3] => [4] => [5] => [6] => ) Array ( [0] => [1] => [2] => [3] => [4] => [5] => [6] => ) by looking at this I realise the values are not sent/assigned as I would want them to, too tired to really do any real work at this time but if anyone got any idea as of why this occurs I'de be glad for any hints :-\ Quote Link to comment https://forums.phpfreaks.com/topic/128612-solved-dynamic-form-fieldsarrays-can-i-have-some1-look-at-this-please/#findComment-668472 Share on other sites More sharing options...
philipolson Posted October 18, 2008 Share Posted October 18, 2008 I've not read this entire thread, but quick glance shows broken code: <?php // Your code echo "<input name=\"antal[]\" type=\"text\" value=\""; $antal[$t]; echo "\" size=\"6\" />"; // Working code echo "<input name=\"antal[]\" type=\"text\" value=\""; echo $antal[$t]; echo "\" size=\"6\" />"; // Some preferred code (that works too) echo '<input name="antal[]" type="text" value="' . $antal[$t] . '" size="6" />'; ?> Also, one form of debugging is to look at your HTML ... so go look at your generated HTML (via your browser) and see if it looks like what you want. For example, it is likely showing an empty value here which will explain why PHP sees no values... Quote Link to comment https://forums.phpfreaks.com/topic/128612-solved-dynamic-form-fieldsarrays-can-i-have-some1-look-at-this-please/#findComment-668496 Share on other sites More sharing options...
Bisa Posted October 18, 2008 Author Share Posted October 18, 2008 ah yes, thank you. with applied changes it now looks as follows and it IS WORKING //Re-creates the old fields along with the users submitted values and adds one new set of fields for($t=0; $t<$i; $t++) { echo '<tr><td><label>'; echo '<input name="artnr[]" type="text" value="' . $artnr[$t] . '" size="15" maxlength="4" />'; echo '</label></td>'; echo '<td><label>'; echo '<input name="antal[]" type="text" value="' . $antal[$t] . '" size="6" />'; echo '</label></td>'; echo '<td><label>'; echo '</label></td><tr>'; } At first this was not working either, the html code in my browser showed empty values (thnx for the tip philipolson) thus I understood where to look, I deactivated the secureFormInput-function and I immediately got the desired results. Thnx for the help everyone, this topic can now be considered solved Quote Link to comment https://forums.phpfreaks.com/topic/128612-solved-dynamic-form-fieldsarrays-can-i-have-some1-look-at-this-please/#findComment-668612 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.