Jump to content

Gender Field Help


ItsWesYo

Recommended Posts

This is kind of like MySQL and PHP, but I put it here since it's more PHP.

 

Anyway, I created a gender field for a profile feature. In the database, I have the type set as "enum" with the values of 'Not Specified', 'Male' and 'Female'. In "edit_profile.php", I created a dropdown menu that looks like this:

 

<b>Gender:</b><br>
<select name='gender' class='box1'>
<option value='Not Specified'>Not Specified</option>
<option value='Male'>Male</option>
<option value='Female'>Female</option>
</select>

 

The only thing I'm having is that it will save what you choose and it displays on your profile, but the real problem is that when you go back to edit your profile, it says "Not Specified". I'm wondering how can it save what the user put?

Link to comment
https://forums.phpfreaks.com/topic/40227-gender-field-help/
Share on other sites

the values aren't posting. it chooses Not Specified because that is the pre-selected option, since it is the first option. to test this theory, you can add another option at the top like this:

<b>Gender:</b><br>
<option value=''>Choose an option</option>
<select name='gender' class='box1'>
<option value='Not Specified'>Not Specified</option>
<option value='Male'>Male</option>
<option value='Female'>Female</option>
</select>

the values stored should now be empty.

 

we need to see more code to be able to help you further.

Link to comment
https://forums.phpfreaks.com/topic/40227-gender-field-help/#findComment-194626
Share on other sites

It still does the same thing, boo_lolly. What I'm trying to do is SAVE the option of what the user puts. Right now, it automatically goes back to NS because it's at the top anyway. But yeah.

 

edit_profile.php

<?
ob_start();
include ("header.php");
include("config.php");
if ($logged[username])
{
// the user is logged in!  We continue...
if (!$_POST[update])
{
// the form hasn't been submitted.  We continue...
$profile = mysql_query("SELECT * from users where username = '$logged[username]'");
$profile = mysql_fetch_array($profile);
// the above lines get the information so that it can be displayed in the html form.
echo("

<form method='post'>

<b>Name:</b><br>
<input type='text' size='25' maxlength='30' name='name' value='$profile[name]' class='box1'>

<br><br>

<b>Gender:</b><br>
<select name='gender' class='box1'>
<option value='Not Specified'>Not Specified</option>
<option value='Male'>Male</option>
<option value='Female'>Female</option>
</select>

<br><br>

<b>Location:</b><br>
<input type='text' size='25' maxlength='30' name='locate' value='$profile[location]' class='box1'>

<br><br>

<b>MSN:</b><br>
<input type='text' size='25' maxlength='50' name='msn' value='$profile[msn]' class='box1'>

<br><br>

<b>AIM:</b><br>
<input type='text' size='25' maxlength='25' name='aim' value='$profile[aim]' class='box1'>

<br><br>

<b>Email Address:</b><br>
<input type='text' size='25' name='email' value='$profile[email]' class='box1'>

<br><br>

<input type='submit' name='update' value='Update' class='box1'>

</form>

");
}
else
{
$email = htmlspecialchars($_POST[email]);
$aim = htmlspecialchars($_POST[aim]);
$msn = htmlspecialchars($_POST[msn]);
$locate = htmlspecialchars($_POST[locate]);
$name = htmlspecialchars($_POST[name]);
$gender = htmlspecialchars($_POST[gender]);
// the above lines get rid of all html.
echo ("Your profile has been updated!");
$update = mysql_query("Update users set email = '$email',
msn = '$msn', aim = '$aim', location = '$locate', name = '$name', gender = '$gender' where username = '$logged[username]'");
// updates the information in the database.
}
}
else
{
// They aren't logged in!
echo ("<a href=\"login.php\">You must login</a>");
}
?>

Link to comment
https://forums.phpfreaks.com/topic/40227-gender-field-help/#findComment-194630
Share on other sites

Basically, you do it this way:

 

<?php
//I assume $profile['gender'] holds the gender?

echo "<select name='gender' class='box1'>\n";
echo "<option value='Not Specified'";
if($profile['gender'] == "Not Specified")
echo " selected";
echo ">Not Specified</option>";
echo "<option value='Male'";
if($profile['gender'] == "Male")
echo " selected";
echo ">Male</option>\n";
echo "<option value='Female'";
if($profile['gender'] == "Female")
echo " selected";
echo ">Female</option>";
echo "</select>";

?>

 

 

Although, if you had stored it as 0 (not specified), 1 (male/female) and 2 (the second gender) it would have been easier.

 

Orio.

Link to comment
https://forums.phpfreaks.com/topic/40227-gender-field-help/#findComment-194641
Share on other sites

couple of things. why do you have: $gender = htmlspecialchars($_POST[gender]);

 

that's not necessary. also, your if/else statements are where you are getting tripped on. it's not storing the data because it's in the ELSE of your IF statement, and your IF statement is where the user is inputing data. the two need to be in the same IF statement.

Link to comment
https://forums.phpfreaks.com/topic/40227-gender-field-help/#findComment-194644
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.