javascript - Changing the values in a using another using AJAX (PHP) -
javascript - Changing the values in a <select> using another <select> using AJAX (PHP) -
i'm trying alter values of select using select
as can see both selects filled using php.
now wanted when choosing specialty, select containing medics change.
here's code
<select id="specialties" class = "formulario_text"> <?php foreach ($specialties $especialidade_id => $especialidade) { ?> <option value="<?php echo $especialidade_id; ?>"><?php echo $especialidade; ?></option> <?php } ?> </select> <br> <br> <label> <b class="formulario_labels2">médico:*</b> </label> <br> <select id="medics" class="formulario_text"> <?php foreach ($medics $id => $nome) { ?> <option value="<?php echo $id; ?>"><?php echo $nome; ?></option> <?php } ?> </select>
i have little knowledge on ajax, php , javascript, i'm pretty much hitting wall here. hoping can help me.
thanks in advance
so far have this
$(document).ready(function() { $("#specialties").change(function() { var val = $(this).val(); if (val == "16") { $("#medics").html("<option value='1'> <?php $medics[1]; ?> </option>"); } }); });
i testing see if capture first position of $medics array have in file.
$specialties = model\clinicalspecialty::getspecialties();
$medics = model\medic::getmedics();
and methods work this
static function getspecialties(){ $res = array(); $db = new dbaccess(); if($db->conn){ $query = 'select id,name clinical_specialty'; $result_set = $db->conn->query($query); while ($row = $result_set->fetch_assoc()) { $res[$row['id']] = $row['name']; } $result_set->free(); $db->conn->close(); } homecoming $res; }
sorry not adding code @ first
you best off dividing in steps, (i think) easiest way understand it.
i not know sure how include php, don't know rest of code. give static illustration , have combine php yourself. hope it'll help anyways!
step 1: include jquery.
you have include jquery library between html <head>
tags so: (if have jquery included, skip step)
<!--including jquery here --> <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
step 2:working in javascript. first have fire onload callback functions know when go off, so:
<script> //when page done loading $(document).ready(function(){ }); </script>
step 3: big script
//when page done loading $(document).ready(function() { // let's select first! var firstselector = $('#yourselectid'); var medicselector = $('#yourmedicselectid'); //we fill variable on select changes var currentselection = firstselector.val(); //set initial medic values medicselector.html( "your options here strings (between quotes). see below example" ); //get value on alter firstselector.change(function() { currentselection = $(this).val(); //check if current value want if (currentselection == "1") { //this replaces options medicselector.html( //you can utilize php variable here fill it. //just utilize <?php echo $optionsforthischoice ?> // $optionsforthischoice filled so: // $optionsforthischoice = "<option>option1</option><option>option2</option> etc.; "<option>option1</option><option>option2</option><option>option3</option>" ); } if (currentselection == "2") { // same here 1, when 2 selected! } }); });
important note: style quite static, can see. if want more dynamic solution going give improve info , more code.
this javascript replaces options had previously, have rewrite them every if statement. not add together or substract. replace.
!edit!
i see have edited question. can help. leave above "tutorial" reference. (by way, can utilize php foreach loop te total array javascript well)
see below
if want properly, might want connect dots in database, , utilize sql joins instead. save lot of code , create whole thing easier. however, since haven't, there 1 proper way solve this. see, cannot utilize ajax on specific function. have create php script.
you create illustration "getmedics.php".
then phone call "getmedics.php" file ajax phone call when selection changes, so:
//the first alternative link file //the ?specialty parameter going in script. //this selection made before //within function "result" variable values phpscript returns $.get('phpscripts/getmedics.php?specialty=' + $("#specialties").val(), function(result) { //if result returned if (result.trim()) { //you want result options $('#medics').html(result); } });
now php script trickier. how it's going look:
//get specialty id $specialty = strip_tags($_get['specialty']); //get medics //i'm guessing results arrays within arrays $medics = medic::getmedics(); //set array options $options = array(); //prepare our results $optionresults = ""; switch ($specialty) { case 0: $options[1] = "1"; $options[2] = "2"; $options[3] = "5"; $options[4] = "7"; break; case 1: $options[1] = "1"; $options[2] = "2"; $options[3] = "5"; $options[4] = "7"; break; case 2: echo "specialty equals 2"; break; } //this foreach loop pass options variable foreach($option $options){ //.= adds variable $optionresults .= "<option value='".$medics[$option][0]."'>".$medics[$option][1]."</option>"; } //this homecoming ajaxed results: echo $optionresults;
javascript php jquery html ajax
Comments
Post a Comment