FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Getting a dropdown to "stick"





Aredon
Essentially here we have a profile editor, and what I'm having an issue with right now is getting the drop down menu for 'gender' to stick to the value it is entered as. This wouldn't be a problem except for now it sets gender to undecided every time someone edits their profile. I could use a different perspective here, because clearly I'm missing something that should be blatantly obvious. :/

Code:
if($act == "editprofilecard"){
if(isset($_POST['gender'])){
$sql="UPDATE `registered_accounts` SET gender='".mysql_real_escape_string($_POST['gender'])."' WHERE user_id=$userid"; @mysql_query($sql);
}

if(isset($_POST['aim']) || isset($_POST['msn']) || isset($_POST['yim']) || isset($_POST['icq'])|| isset($_POST['email'])|| isset($_POST['xfire'])|| isset($_POST['skype'])){
$sql="UPDATE `registered_accounts` SET AIM='".mysql_real_escape_string($_POST['aim'])."',MSN='".mysql_real_escape_string($_POST['msn'])."', YIM='".mysql_real_escape_string($_POST['yim'])."',ICQ='".mysql_real_escape_string($_POST['icq'])."',email='".mysql_real_escape_string($_POST['email'])."',Xfire='".mysql_real_escape_string($_POST['xfire'])."',Skype='".mysql_real_escape_string($_POST['skype'])."' WHERE user_id=$userid";
@mysql_query($sql);
}
if(isset($_POST['month']) && isset($_POST['day']) && isset($_POST['year'])){
$m=(int)$_POST['month'];
$d=(int)$_POST['day'];
$y=(int)$_POST['year'];
if(!checkdate ( $m, $d, $y)){echo "Invalid date.";exit;}
$sql="UPDATE `registered_accounts` SET birthdate=".mktime(0, 0, 0, $m, $d, $y)." WHERE user_id=$userid";
@mysql_query($sql);
}
$sql="SELECT * FROM `registered_accounts` WHERE user_id=".$userid;
$result=mysql_query($sql);
$num_rows = mysql_num_rows($result);
if(!$num_rows){echo "User not found."; exit;}
$row=mysql_fetch_object($result);
?>
<style type="text/css">
table tr th, table tr td{text-align:left; background:#eeeeee;}
table tr a:link, table tr a:visited, table tr a:active, a.user:link, a.user:visited, a.user:active{
 text-decoration: none; color: #0000dd; font-weight:bold;
 }
table tr a:hover,a.user:hover {
 text-decoration: none; color: #3333ff;  font-weight:bold;
}
.c{
text-align:center;
}
</style>
<form action="usercp.php?act=editprofilecard" method="post">
<?
if($row->birthdate){
$D=getdate($row->birthdate);
$m=$D['mon'];$d=$D['mday'];$y=$D['year'];}else{$m=0;$d=0;$y="";}
include("modules/dropdowns.php");
?>
<table border="0" cellpadding="0" cellspacing="2">
<tr><td>Gender:</td><td><?= htmlentities($row->gender); ?></td><td><?= getGenderDropDown(); ?></td><td>*</td></tr>
<tr><td>Birthday:</td><td>Month:</td><td>Day:</td><td>Year:</td></tr>
<tr><td></td><td><?= getMonthDropDown($m) ?> &nbsp;</td>
<td><?= getDayDropDown($d);?> &nbsp;</td>
<td><input name="year" value="<?= $y ?>" size="4" maxlength="4" type="text">
</td></tr>
</table>
<table cellpadding="2" cellspacing="1">
<tr><td>AIM Name:</td><td><?=  htmlentities($row->AIM); ?></td><td><input type="text" name="aim" value="<?=  htmlentities($row->AIM); ?>"></td></tr>
<tr><td>MSN Identity:</td><td><?=  htmlentities($row->MSN); ?></td><td><input type="text" name="msn" value="<?=  htmlentities($row->MSN); ?>"></td></tr>
<tr><td>YIM Identity:</td><td><?=  htmlentities($row->YIM); ?></td><td><input type="text" name="yim" value="<?=  htmlentities($row->YIM); ?>"></td></tr>
<tr><td>ICQ Number:</td><td><?=  htmlentities($row->ICQ); ?></td><td><input type="text" name="icq" value="<?=  htmlentities($row->ICQ); ?>"></td></tr>
<tr><td>Xfire Name:</td><td><?= htmlentities($row->Xfire); ?></td><td><input type="text" name="xfire" value="<?= htmlentities($row->Xfire); ?>" /></td></tr>
<tr><td>Skype Name:</td><td><?= htmlentities($row->Skype); ?></td><td><input type="text" name="skype" value="<?= htmlentities($row->Skype); ?>" /></td></tr>
<tr><td>Prefered E-mail:</td><td><?=  htmlentities($row->email); ?></td><td><input type="text" name="email" value="<?=  htmlentities($row->email); ?>"></td></tr>
</table>
<input type="submit" value="Edit">
</form>
<?
}
?>
kv
in the method getGenderDropDown(), pass the current gender. When generating options html, check the passed gender and make it as selected. If would have been better if you had provided code for getGenderDropDown(). Anyway, here is a sample code.

Code:

function getGenderDropDown($gender)
{
$content='<SELECT>';
if($gender='M')
{
  $content.='<OPTION selected="YES">M</OPTION>';
  $content.='<OPTION>F</OPTION>';
}
else
{
  $content.='<OPTION>M</OPTION>';
  $content.='<OPTION selected="YES">F</OPTION>';
}

$content.='</SELECT>';
return $content;
}


This is not an efficient code, but it gives an idea.
Aredon
Well that would need a bit of change since there's actually three types rather than two, but that is pretty easily doable. It seems you've found my mistake. Looks like I need to rewrite that function. (probably a good thing I forgot to post that function seeing as how I wrote it at about 2 in the morning and looking at it now its pretty terribad.) I must say I feel like an idiot now, but thanks for the help. Smile

Here, while I'm at it have a good laugh at my sleepy code:
Code:

function getGenderDropDown(){
$s=array("","","");
$s=" selected=\"selected\"";
return "<select name=\"gender\">
<option value=\"Undisclosed\">Undisclosed</option>
<option value=\"Male\">Male</option>
<option value=\"Female\">Female</option>
</select>";
}

Pretty sad that I define the same variable twice >.>

Anyway, thanks to you for the basic construct, here's what I arrived at that now works Smile

Code:

function getGenderDropDown($gender){
$content='<SELECT name="gender">';
if($gender=='Male')
{
  $content.='<OPTION value="Male">Male</OPTION>';
  $content.='<OPTION value="Female">Female</OPTION>';
  $content.='<OPTION value="Undisclosed">Undisclosed</OPTION>';
}
elseif($gender=='Female')
{
  $content.='<OPTION value="Female" selected="selected">Female</OPTION>';
  $content.='<OPTION value="Male">Male</OPTION>';
  $content.='<OPTION value="Undisclosed">Undisclosed</OPTION>';
}
elseif($gender=='Undisclosed')
{
  $content.='<OPTION value="Undisclosed">Undisclosed</OPTION>';
  $content.='<OPTION value="Male">Male</OPTION>';
  $content.='<OPTION value="Female">Female</OPTION>';
  }
$content.='</SELECT>';
return $content;
}
kv
Actually, the method can be simplified further.

Code:

function getGenderDropDown($gender='')
{
    $content='<SELECT name="gender">';
    $selstr='';
    foreach($opt as array('Male', 'Female', 'Undisclosed') )
    {
      $selstr=($gender==$opt)?"selected='yes' ":'';
      $content.="<OPTION value='$opt' $selstr >$opt</OPTION>";
    }
   
    $content.='</SELECT>';
    return $content;
}
Related topics
Google Messenger
There's One Born Every Minute
Getting paid online?
Top 14 Humor of Getting married
Outside With Me
a really funny joke
London is getting nailed with terrorists
Manchester United getting Americanized!!!!!!
my flash
Most the return card that praises
vidio games dont work
Getting a guy to talk...
Cars: manual or auto?
getting pver someone help!
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.