Jump to content

Form Select Issue


Zepo.

Recommended Posts

Im having a problem with a form select auto selecting after it's gone through a loop. The first auto select is right, but then everyone after that is the same.

 

$members=mysql_query("SELECT id,name,position,about,aim,website FROM members WHERE teamid='$team[id]' ORDER BY name");
while(list($id,$name,$position,$about,$aim,$website)=mysql_fetch_row($members)){

if($config[altcolorx]==$config[altcolora]){
$config[altcolorx]="$config[altcolorb]";
}else{
$config[altcolorx]="$config[altcolora]";
}

$selectposition="$position";
$selected_rank[$selectposition]="SELECTED";

$out[body].="
<tr bgcolor='$config[altcolorb]'>
<form method='post'>
<td width='19%' valign='center' align='left'><input type='text' class='input' name='team[membername]' value='$name' size='15' maxlength='25'></td>
<td width='19%' valign='center' align='left'>
<select name='team[position]'>
<option value='6' $selected_rank[6]>Member</option>
<option value='5' $selected_rank[5]>2nd Lieutenant</option>
<option value='4' $selected_rank[4]>1st Lieutenant</option>
<option value='3' $selected_rank[3]>Captain</option>
<option value='2' $selected_rank[2]>Co-Leader</option>
<option value='1' $selected_rank[1]>Leader</option>
</select></td>
<td width='19%' valign='center' align='left'><input type='text' class='input' name='team[aim]' value='$aim' size='15' maxlength='15'></td>
<td width='19%' valign='center' align='left'><input type='text' class='input' name='team[website]' value='$website' size='15' maxlength='35'></td></tr>
<tr bgcolor='$config[altcolorb]'>
<td width='70%' colspan='3' valign='center' align='left'><textarea class='button' name='team[about]' value='$about' maxlength='255' style='width:320px; height:40px;'>$about</textarea></td>
<td width='30%' valign='center' align='center'>
<table><tr bgcolor='$config[altcolorb]'><td width='50'>
<input type='hidden' name='action' value='editmember'>
<input type='hidden' name='team[memberid]' value='$id'>
<input type='hidden' name='login[id]' value='$login[id]'>
<input type='hidden' name='login[pass]' value='$login[pass]'>
<input type='submit' name='submit' value='Update >>'></form>
<form method='post'></td><td width='50'>
<script language='javascript'>var confirmdelete2='Are you 100% sure you want to DELETE this member?';</script>
<input type='hidden' name='action' value='deletemember'>
<input type='hidden' name='team[memberid]' value='$id'>
<input type='hidden' name='login[id]' value='$login[id]'>
<input type='hidden' name='login[pass]' value='$login[pass]'>
<input type='submit' name='submit' value='Delete >>' onClick='return confirm(confirmdelete2);'></form></td></tr></table>
</td>
</tr>";
$totalmembers++;
}

 

You can see a demo of what i mean:

http://eliteladders.com/devlopement/manager.php

user:36

pass:aaaaaa

 

It's the member manager.

Link to comment
https://forums.phpfreaks.com/topic/78447-form-select-issue/
Share on other sites

That's one messy code.

 

You're better off listing the ranks in a table.

 

$sql = "SELECT * FROM `ranks`";
$res = mysql_query($sql) or die(mysql_error());

while($row = mysql_fetch_assoc($res)){
$check = ($row['id'] == $position) ? " CHECKED" : "";

echo "<option value=\"{$row['id']}\"$check>{$row['name']}</option>\n";
}

Link to comment
https://forums.phpfreaks.com/topic/78447-form-select-issue/#findComment-403361
Share on other sites

The main problem is here:

$selected_rank[$selectposition]="SELECTED";

This is executed every time you loop through the rows.  Say the first time $selectposition is 1.  So that means that $selected_rank[1] = "SELECTED", while the others are empty.  Next row, $selectposition is 2.  So you set $selected_rank[2] = "SELECTED", but $selected_rank[1] is also "SELECTED".  If the browser sees many "SELECTED" options, it'll select the first one and leave the rest.  Look at your HTML source, you probably have SELECTED's everywhere.

 

Better:

$members=mysql_query("SELECT id,name,position,about,aim,website FROM members WHERE teamid='$team[id]' ORDER BY name");

while(list($id,$name,$position,$about,$aim,$website)=mysql_fetch_row($members)){
// NOTE: We don't need the $selectedposition variable... it's the same as $position...
//$selectposition="$position";
// Don't use this array
//$selected_rank[$selectposition]="SELECTED";

$out[body].="
...
<select name='team[position]'>
	<option value='6' " . ( $position == 6 ? "SELECTED" : "" ) . " >Member</option>
	<option value='5' " . ( $position == 5 ? "SELECTED" : "" ) . " >2nd Lieutenant</option>
	...
</select>
...
";
}

This syntax:

test_value ? return_if_true : return_if_false

Is useful here.  If "test_value" is true, it'll return "return_if_true".  Otherwise, it'll return "return_if_false".  So this:

$position==6 ? "SELECTED" : ""

Means check if $position is 6... if so, return "SELECTED", if not return "".  Works for you?

Link to comment
https://forums.phpfreaks.com/topic/78447-form-select-issue/#findComment-403444
Share on other sites

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.