php
php

Reputation: 23

how to validate select through javascript

I want to validate select option with javascript like if user select Admin then this page work for admin Login, Now if user select vendor then for vendor while user for userlogin

 <table class="login_table" width="100%" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td style="width:30%;">Username</td>
        <td style="width:70%;"><input name="uname" id="uname" type="text" /></td>
      </tr>
      <tr>
        <td style="width:30%;">Password</td>
        <td style="width:70%;"><input name="upass" id="upass" type="password" /></td>
      </tr>
       <tr>
        <td style="width:30%;">Login Type</td>
        <td style="width:70%;">
        <select>
        <option>Admin</option>
        <option>Vendor</option>
        <option>User</option>
        </select>

        </td>
      </tr>
      <tr>
        <td colspan="2" style="text-align:right; padding-right:5px;"><input type="submit"  class="login_button" name="login" value="Login" /></td>
      </tr>
    </table>

HOw it is Possible

Upvotes: 0

Views: 465

Answers (4)

Annie Lagang
Annie Lagang

Reputation: 3235

USING JS

First, you have to put an id for your dropdownlist and add an eventhandler.

<select id="myId" onchange='Validate()';>
    <option>Admin</option>
    <option>Vendor</option>
    <option>User</option>
</select>

Then, access that dropdownlist using javascript via it's id.

<SCRIPT LANGUAGE="javascript">
<!--
 function Validate()
{
  var dll=document.getElementById("myId")
   var myindex  = dll.selectedIndex
   var SelValue = dll.options[myindex].value

  //do something with selected value
  }
  //-->

 </SCRIPT>

Or you could use an eventhandler that is invoked when a button is clicked to validate the dropdrownlist.

Use this Code Project article or this one as your guide. Cheers!

Upvotes: 0

chris
chris

Reputation: 36937

I'd use a lib like jQuery just to make it easier but essentially with jQuery to get the selection then pass it to php I would use the .change(), .post()

http://api.jquery.com/change/ http://api.jquery.com/jQuery.post/ http://api.jquery.com/selected-selector/

You would use the change event to see what the selected value was on the select, then the post to pass the data to PHP. If i wasn't so tired right now I would further this point with example code. But I'm to fried for that right now. After that you would use something like

window.location = page2go2; //based on value of selected to load whatever page per.

Upvotes: 0

Gregory Nozik
Gregory Nozik

Reputation: 3374

in javascript it will be something like this

 var selectObj = document.getElementById("selObj");
   var selectedValue = selectObj.options[selectObj.selectedIndex].value;
   if (selectedValue  == "Admin")

   else

Assign id to select

Upvotes: 0

JercSi
JercSi

Reputation: 1047

Maybe something like this (without JS)?

<select name="userType">
 <option value="Admin">Admin</option>
 <option value="Vendor">Vendor</option>
 <option value="User">User</option>
</select>

<?php
// php
if ('Admin' == $_POST['userType']) {
  // work as admin
} else if ('Vendor' == $_POST['userType']) {
  // work as vendor
} else {
  // work as user or any other option
}
?>

Upvotes: 1

Related Questions