How to get a dynamic select option with php without any submit button? This code shows Undefined index: taskOption error

<form name="form" action="" method="POST">

<select name="taskOption">
  <option value="1">First</option>
  <option value="2">Second</option>
  <option value="3">Third</option>
</select>

</form>

<?php $selectOption = $_POST['taskOption'];?>

Answer

You cannot get form values without posting the form. In order to get the value without button, use jquery to post form with onselect.

<form name="form" id="form" action="someAction.php" method="POST">

<select name="taskOption" id="taskOption" onselect="getValue()">
  <option value="1">First</option>
  <option value="2">Second</option>
  <option value="3">Third</option>
</select>

</form>

<script>
  function getValue(){
  $("#taskOption").val();
  $("#form").submit();
}