Condiion for two field in the same form in php?

Dear All

I have form with many field, there I have two field like “Select your gender” & another is “Your Name title”. I want to put the condition as if Gender select Male then he must type the title Mr., if not so then it will show an arror msg. How to do itin html/php?

See the example below

<?php

if (isset($_POST['submit'])) {
    $gender = isset($_POST['gender']) ? $_POST['gender'] : false;
    $title = isset($_POST['title']) ? $_POST['title'] : false;
    
    if ($gender && $title) {
        if ($gender == 'male' && strtolower($title) != 'mr') {
            die('An error has occurred');
        }
    }
}

?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Form Testing</form>
</head>
<body>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    <label for="gender">Please select your gender:</label>
    <select name="gender" id="gender">
        <option value="male">Male</option>
        <option value="female">Female</option>
    </select><br />
    <label for="title">Please enter your title:</label>
    <input type="text" name="title" id="title" size="10"><br />
    <input type="submit" value="Submit" name="submit">
</form>

</body>
</html>

Thank you very much for your help.

Need one more things, if select Female then title must have the input Ms. Please help.

The current IF statement would would simply become

if (($gender == 'male' && strtolower($title) != 'mr') || ($gender == 'female' && strtolower($title) != 'ms')) {

Thank you dear

Its nice help.

If they always have to digit ‘Mr’ and ‘Ms’, why not get rid of the ‘Your name title’ box? Just have them choose the gender, and have the script add the ‘Mr’ and ‘Ms’.