Jump to content

Recommended Posts

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 !

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.  :-\

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.

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>';
}
?>

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.

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.

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.

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>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.