Hi,
This code works fine but, when i choose "Female" from selectbox, it sets "Male" in the selectbox again. What is the problem?
Thanks

<?php
if (!isset($_POST["sex"])) {
	print "Please start!";
} else if ("Male" == $_POST["sex"]) {
	print "Male !";
} else if ("Female" == $_POST["sex"]) {
	print "Female !";
} else {
	print "Himmm!";
}
?>

<html>
<head></head>
<body>
<form action="result.php" method="post"> 
SEX? <select name="sex"  >
	  <option value="Male">Male</option>
	  <option value="Female">Female</option>
     </select><br>
<input type="submit" value="Send">
</form>
</body>
</html>

There is much cleaner ways to do this but for a quick fix, below should now work as you expect:

<?php
if (!isset($_POST["sex"])) {
	print "Please start!";
} else if ("Male" == $_POST["sex"]) {
	print "Male !";
     $male_checked = " SELECTED";
} else if ("Female" == $_POST["sex"]) {
	print "Female !";
    $female_checked = " SELECTED";
} else {
	print "Himmm!";
}
?>

<html>
<head></head>
<body>
<form action="result.php" method="post"> 
SEX? <select name="sex"  >
	  <option value="Male" <?php echo $male_checked ?>>Male</option>
	  <option value="Female" <?php echo $female_checked ?>>Female</option>
     </select><br>
<input type="submit" value="Send">
</form>
</body>
</html>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.