I want to comment off .cities
whenever I select the value Singaporean
however it should get visible If I choose any other nationality apart from singaporean. Is it possible in PHP ?
<div class="cities">New York</div>
<select id="emp_nationality" name='emp_nationality' class='form-control'>
<option value="Singaporean">Singaporean</option>
<option value="Albanian">Albanian</option>
<option value="Algerian">Algerian</option>
<option value="American">American</option>
</select>
I tried this but for some reason it is not working
<?php
if ($_POST['emp_nationality'] != 'Singaporean') {
echo '<div class="cities">New York</div>';
}
?>
ERROR: Undefined index: emp_nationality
You can use an onchange handler in js and hide / show the ".cities" div depending on the value chosen. Note I added a display:none to the .cities since your first option in the select list is Singaporean and the function requires a change to trigger it. Also I would normally separate the onclick as a separate handler - rather than mixing it into the HTML but for this example I did it this way. Does not require PHP which would need the option to be selected on page load and would not easily change the visibility of the div on a different choice. This functions to hide the div if the correct option is selected and show it on other option selections.
function hideDiv(option)
{
if(option=="Singaporean"){$('.cities').hide()}else{$('.cities').show()}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="cities" style="display:none">New York</div>
<select id="emp_nationality" name='emp_nationality' class='form-control' onchange="hideDiv(this.value)">
<option value="Singaporean">Singaporean</option>
<option value="Albanian">Albanian</option>
<option value="Algerian">Algerian</option>
<option value="American">American</option>
</select>
<div class="cities" style="display:none">New York</div>
<select id="emp_nationality" name='emp_nationality' class='form-control' onchange="hideDiv(this.value)">
<option value="Singaporean">Singaporean</option>
<option value="Albanian">Albanian</option>
<option value="Algerian">Algerian</option>
<option value="American">American</option>
</select>
//js
function hideDiv(option)
{
if(option=="Singaporean"){$('.cities').hide()}else{$('.cities').show()}
}
</div>
If you like would to avoid the "Undefined index: emp_nationality" you need to make sure this variable exists before checking it's value. Use isset( ). http://php.net/manual/en/function.isset.php
if (isset($_POST['emp_nationality']) && ($_POST['emp_nationality'] != 'Singaporean')) {
echo '<div class="cities">New York</div>';
}