2

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?

2 Answers 2

4

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";
}
Sign up to request clarification or add additional context in comments.

Comments

0

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.