Tere
Tere

Reputation: 33

What is the right way to implement drop down option menu within an HTML form?

I am trying to put a drop down menu but for some reason it is not able to get the selected option . I send it to a PHP server using POST method.

<center><form style="align:left;"action="signuphand.php" method="post" accept-charset="utf-8">
                  <label>Enter name of teacher: </label><input type="text" name="name" value="" placeholder="Name">
                <br />
                <label>Enter branch:
                    <select name="branch">
                <option value="SELECT BRANCH">SELECT BRANCH</option>    
                <option value="ECE">ECE</option>
                <option value="CSE">CSE</option>
                <option value="MECH">MECH</option>
                <option value="E & I">E & I</option>
                <option value="CIVIL">CIVIL</option>
                <option value="ELECTRICAL">ELECTRICAL</option>
                </select>   

                 </label>
                <br />
            <label>Enter role:
                <select id="role">
                <option value="SELECT ROLE">SELECT ROLE</option>
                <option value="teacher">teacher</option>
                <option value="student">student</option>
                </select>            

and PHP is

   if (isset($_POST['register']))
{
    $name= $_POST['name'];
    if (isset($_POST['branch'])){
        $branch= $_POST['branch'];  
    }
    $user = $_POST['username'];
    $pass = $_POST['password'];
    $pass2= $_POST['password1'];
    if (isset($_POST['branch'])){
        $role= $_POST['role'];  
    }

But the problem is still not resolved. The branch and role columns are still empty in my table. Folowed this technique How to get a value of HTML drop down menu using PHP?

and many others still problem persists please help. Used isset beacause without it I was getting undefined index for 'role' and 'branch'.

Upvotes: 1

Views: 34

Answers (1)

Chrisstar
Chrisstar

Reputation: 646

For the second select tag, you used the "id" attribute instead of "name". So you should change that to name firstly:

<select name="role">
  <option value="SELECT ROLE">SELECT ROLE</option>
  <option value="teacher">teacher</option>
  <option value="student">student</option>
</select> 

Secondly, in your php file, you checked if "branch" was set instead of "role". So fix that too:

if (isset($_POST['register']))
{
$name= $_POST['name'];
if (isset($_POST['branch'])){
    $branch= $_POST['branch'];  
}
$user = $_POST['username'];
$pass = $_POST['password'];
$pass2= $_POST['password1'];
if (isset($_POST['role'])){
    $role= $_POST['role'];  
}

If it still doesn't work, consider posting your whole code, maybe something is wrong there.

Upvotes: 1

Related Questions