LoolKovsky Posted August 23, 2012 Share Posted August 23, 2012 Hello ! I have written a simple code for creating a form using a foor loop. So far I have : <form name="theForm" method="POST" action="for.php"> <? for($i=1;$i<5;$i++) { ?> <select name="color<?=$i?>" > <option value="color">color line <?php echo $i; ?></option> <option value='1' <? if(isset($_POST['color$i']) && $_POST['color$i']=='1') echo 'selected="selected"'?> >Black</option> <option value='2' <? if(isset($_POST['color$i']) && $_POST['color$i']=='2') echo 'selected="selected"'?> >Red</option> </select> <BR> <? } ?> <input type=submit name="submit" value="submit"> </form> But I can't make it keep my selection after submission. The code isn't working. Can you please show me how to get it fixed ? And I also need to keep all the data after submission so I can use it later. Thanx ! Quote Link to comment Share on other sites More sharing options...
BuildMyWeb Posted August 23, 2012 Share Posted August 23, 2012 ive had trouble getting POST to work for me in sticky forms as well. not sure if sometihng has changed in recent versions of php. ive resorted to using SESSION vars instead. would love to get some insight into this as well. :-\ Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted August 23, 2012 Share Posted August 23, 2012 Your code is not using the $i variable when you have it inside of a single-quoted string, because php variables are only replaced with their value inside of a string when the string is using double-quotes. You should also always use full opening <?php tags, since the short opening tag <? are not always enabled and you might not have the ability to enable it on any particular server. Quote Link to comment Share on other sites More sharing options...
Jessica Posted August 23, 2012 Share Posted August 23, 2012 I would also recommend instead of having the variables be $color1 through $color5, you use an array. I rewrote the code a little, this is sort of how I would do it. Not tested. <? $colors = array(1=>'Black', 2=>'Red'); //Where does this data come from? for($i=1;$i<5;$i++){ $id = 'color_'.$i; echo '<p> <label for="'.$id.'">color line '.$i.'</label> <select id="'.$id.'" name="color['.$i.']">'; foreach($colors AS $color_key=>$color){ if(isset($_POST['color'][$i]) && $_POST['color'][$i]==$color_key){ $selected = 'selected="selected"' }else{ $selected = ""; } echo "<option value=\"$color_key\"$selected>$color</option>"; } echo '</select></p>'; } ?> Quote Link to comment Share on other sites More sharing options...
LoolKovsky Posted August 23, 2012 Author Share Posted August 23, 2012 I would also recommend instead of having the variables be $color1 through $color5, you use an array. I rewrote the code a little, this is sort of how I would do it. Not tested. <? $colors = array(1=>'Black', 2=>'Red'); //Where does this data come from? for($i=1;$i<5;$i++){ $id = 'color_'.$i; echo '<p> <label for="'.$id.'">color line '.$i.'</label> <select id="'.$id.'" name="color['.$i.']">'; foreach($colors AS $color_key=>$color){ if(isset($_POST['color'][$i]) && $_POST['color'][$i]==$color_key){ $selected = 'selected="selected"' }else{ $selected = ""; } echo "<option value=\"$color_key\"$selected>$color</option>"; } echo '</select></p>'; } ?> Does this code work for you? You didn't close all the tags, and after you press the submit button, which you didn't include, the selection is lost. And by the way, there aren't only 2 colors. There are some more, and that is why I need to make the form with a loop. And I need to do this for an unknown number of rows not only for 5. And after I submit it I need to use each color for writing a text, so this is why i need to keep the data after submission, because you don't like how the text looks. But thanx for the help. Quote Link to comment Share on other sites More sharing options...
Jessica Posted August 23, 2012 Share Posted August 23, 2012 1. I said "not tested". I didn't run it, I reformatted it. 2. Find me an open tag in that code, please. 3. Add the other colors to the array at the top. As I put in the comments, where do they come from? 4. There ARE still loops. 5. You're the one who wrote 5 rows, not me. Use a variable then. 6. Don't be so stuck up. Quote Link to comment Share on other sites More sharing options...
LoolKovsky Posted August 23, 2012 Author Share Posted August 23, 2012 1. I said "not tested". I didn't run it, I reformatted it. 2. Find me an open tag in that code, please. 3. Add the other colors to the array at the top. As I put in the comments, where do they come from? 4. There ARE still loops. 5. You're the one who wrote 5 rows, not me. Use a variable then. 6. Don't be so stuck up. Sorry for "my" previous post. The code is good, it works, but after submission it doesn't keep the selection. That is the major problem. And there are no problems with quotes or etc. Sorry again. And I really apreciate your help, but this code is pissing me off. Quote Link to comment Share on other sites More sharing options...
Jessica Posted August 23, 2012 Share Posted August 23, 2012 What do you mean by after submission it doesn't keep the selection? If you post it and look at the HTML source, do you see any errors in there? Do you have error reporting turned on and set to E_ALL? Can you post your current code including the form and submit button? Edit: I went ahead and loaded it on my localhost. If you fix the <? to <?php depending on if you have short tags enabled, and add the semicolon on line 11....it works. It prints 4 rows not 5, but again that's your original code. This is what I ran on my localhost and it works perfectly. <form method="post"> <?php $colors = array(1=>'Black', 2=>'Red'); //Where does this data come from? for($i=1;$i<5;$i++){ $id = 'color_'.$i; echo '<p> <label for="'.$id.'">color line '.$i.'</label> <select id="'.$id.'" name="color['.$i.']">'; foreach($colors AS $color_key=>$color){ if(isset($_POST['color'][$i]) && $_POST['color'][$i]==$color_key){ $selected = 'selected="selected"'; }else{ $selected = ""; } echo "<option value=\"$color_key\"$selected>$color</option>"; } echo '</select></p>'; } ?> <input type="submit" name="submit" /> </form> Quote Link to comment Share on other sites More sharing options...
LoolKovsky Posted August 23, 2012 Author Share Posted August 23, 2012 It works, thanx. I forgot to add <form method="post" action="for.php">. And now I can acces the data like this : $_POST['color1'] ? Thanx again, much appreciated! Quote Link to comment Share on other sites More sharing options...
Jessica Posted August 23, 2012 Share Posted August 23, 2012 If you're using the code I posted, it's $_POST['color'][1]. Look at the php manual or google "multi dimensional array" Quote Link to comment 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.