johansson johansson - 4 months ago 8
Ajax Question

PHP display div with select option

I have two tables
material :

- id :int,
- name : varchar(255),
- providers_id : int (foreign key)


And table providers :

- id_provider: int,
- code_providers : varchar(200)


I have some data in the table providers :


1,provider1
2,provider2
3,provider3



And i have page addMaterial.php :

<div class="form-group" id="TextBoxDiv1">
<label>Providers</label>
<select class="form-control">
<option value='-1'>Choose provider</option>
<?php
$stmt2 = $db->prepare("SELECT * FROM providers");
$stmt2->execute();
while($row2 = $stmt2->fetch()){
echo "<option value='".$row2["id_provider"]."'>".$row2["code_provider"]."</option>";
}
?>

</select>

</div>

<div>add</div>
<div>update</div>
<div>delete</div>



  • I would like to display div add when i choose provider 1

  • I would like to display div update when i choose provider 2

  • I would like to display div update when i choose provider 3


Answer

HTML:

<select class="form-control" id="chooseProvider">
 ...

<div id="add">add</div>
<div id="update">update</div>
<div>delete</div>

JS:

$("#chooseProvider").change(function(){
    if($(this).val()=="1"){
       $("#add").show();
       $("#update").hide();
    } 
    else if($(this).val()=="2" || $(this).val()=="3"){
       $("#update").show();
       $("#add").hide();
    }
});