Search code examples
phpmysqli

auto fill value "man" in column2 if column1 value selected as "male"


I tried to auto fill value if column2 selected as man then column1 should be auto filled by Female or vise versa,

 <form action="basicdballinput.php" method="post">
   <h1 align="center">Basic User Registration</h1>    

        <p><label for="name" style="font-size: 14pt">UserName:</label>
        <input type="text" id="name" name="name"/></p>
      
        <p><label for="gender" style="font-size: 14pt">Male/Female :</label>
        <select name="gender" style="font-size: 12pt">
             <option value="male">Male</option>
             <option value="female">Female</option>
        </select></p>

$sql = "INSERT INTO basic_user_info(name, male_female)

             VALUES('$name','$gender')";

if you feel any problem in upper code then please avoid, as it should be typing mistake, as I mentioned form and its .php input is all working fine.

I want to auto fill column 3 "gender_type" if column"male_female select as "male" then gerder_type should be "man" if "male_female" select as "female" then "gender_type" should be "women"

I want to fill "gender_type" column at same time when this form filed and submited.

Please help.


Solution

  • Add this line before your SQL

    $gendertype = ($gender == 'male' ? 'man' : 'women');
    

    Then modify your SQL (I had to assume the column name is gender since you didn't tell us).

    $sql = "INSERT INTO basic_user_info (name, male_female, gender_type)
            VALUES('$name','$gender','$gendertype')";