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>

Dani AI

Generated

The symptom you describe is not a browser bug — it happens because the form HTML is never told which option should be selected after the POST. printed the submitted value server-side but did not mark the corresponding <option> with the selected attribute, so the browser shows the first option by default. pointed this out and provided a workable approach; a few small improvements make it more robust.

Always initialize the posted value, sanitize it before echoing, and output a proper selected attribute when re-rendering the form. If the form posts to a different script (action="result.php"), make sure that script either re-renders the form with the selected value or passes the choice back (via session, redirect query, or by rendering the form in the same file). Use a debug dump (for example, var_dump($_POST)) to confirm the exact value arriving from the browser.

A concise pattern (PHP 5+ compatible) is:

<?php
$sex = isset($_POST['sex']) ? trim($_POST['sex']) : '';
$sex_safe = htmlspecialchars($sex, ENT_QUOTES, 'UTF-8');
?>

<select name="sex">
<option value="">Please choose</option>
<option value="Male" <?php echo ($sex === 'Male') ? 'selected="selected"' : ''; ?>>Male</option>
<option value="Female" <?php echo ($sex === 'Female') ? 'selected="selected"' : ''; ?>>Female</option>
</select>

Sanitize output (see htmlspecialchars manual) and consider filter_input for input retrieval. For HTML semantics and the selected attribute, see MDN’s option documentation: option (MDN).

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.