125369
125369

Reputation: 3657

Keep a check whether a value if selected from dropdown list using PHP

I need to keep a check on whether a value is selected by the user from the dropdown box other than the default value which is at null position. If selected (any value other than default ) then I want to set a variable say $varSet = 1 and if not $varSet = 0, so depending on the $varSet value, I try to trigger the appropiate functions from my PHP script.

Is it possible to achieve this using only PHP and HTML without any Javascript(client side)? Or is there any other possibilty to get this logic going?

Upvotes: 2

Views: 19721

Answers (2)

Hassan Qasim
Hassan Qasim

Reputation: 483

Here is the simple easy solution to check either dropdown value is selected already if yes then show the selected with selected='selected' else nothing....

<select class="form-control" name="currency_selling" required >
         <option value="">Select Currency</option>
         <option value="pkr" <?=$selected_currency == 'pkr' ? ' selected="selected"' : '';?> >PKR</option>
      <option value="dollar"  <?=$selected_currency == 'dollar' ? ' selected="selected"' : '';?> >USD</option>
      <option value="pounds"  <?=$selected_currency == 'pounds' ? ' selected="selected"' : '';?> >POUNDS</option>
      <option value="dirham"  <?=$selected_currency == 'dirham' ? ' selected="selected"' : '';?> >DRHM</option>
      </select>

Upvotes: 0

dario111cro
dario111cro

Reputation: 805

Let's assume that you have form with method="POST" and action="script.php". Form would have select tag (I assume that is what you call dropdown list):

<select name="dropdown">
  <option value="">Default value (key thing is leaving value="")</option>
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="mercedes">Mercedes</option>
  <option value="audi">Audi</option>
</select>

And script.php would read value of dropdown list and check if default value is checked:

if($_POST['dropdown'] != '') {
  //do something
} else {
  //user leaved default value for dropdown, tell him that:
  echo "Please select valid value from dropdown list";
}

Upvotes: 4

Related Questions