I am trying to make two selects
nested, in which when the value of the first is changed, some values collected from the Database are loaded in the next one. For this I am trying to do it through php and ajax .
The code of the selects is the following:
<div class="row">
<div class="span3">
<p class="letraAzulOsucroN"><?php echo $gen_campus; ?>:</p>
</div>
<div class="span9">
<select id="campus" name="campus" onchange="cargarEdificios(this);" >
<option value="-1"><?php echo $pccrear_elegir_campus; ?></option>
<?php
foreach ($campus as $camp){
?>
<option value="<?php echo $camp->id; ?>">
<?php echo $camp->nombre; ?></option>
<?php
}
?>
</select>
</div>
</div><br />
<div class="row">
<div class="span3">
<p class="letraAzulOsucroN"><?php echo $gen_edificio; ?>:</p>
</div>
<div class="span9">
<select id="edificios" name="edificios" >
<option value="-1"><?php echo $pccrear_elegir_edificio; ?></option>
</select>
</div>
</div><br />
and the ajax request is as follows:
<script type="text/javascript">
function cargarEdificios (obj){
var campus = obj.options[obj.selectedIndex].value;
var idioma = $('#selLang').val();
var parametros = {"idCampus": campus, "idioma" : idioma};
<?php
$rutaObtencion = DIRECTORY_SEPARATOR ."UVControlAulasPC" . DIRECTORY_SEPARATOR ."controlAulas". DIRECTORY_SEPARATOR ."Utiles". DIRECTORY_SEPARATOR ."obtenerEdificios.php";
?>
$.ajax ({
data: parametros,
url: "<?php echo $rutaObtencion; ?>",
type: 'post',
beforeSend: function (){
// Lanzar el div de cargando...
},
success: function (response){
$("#edificios").html(response);
}
});
}
</script>
When I'm testing it I get the following error from the Chrome browser console:
in which you can see that it does not find the file because the address that I send to the ajax the bars disappear.
Could someone tell me what this is due to? How could I solve it?
Thanks to the help of @AlvaroMontoro and @aldanux I realized that I was misusing the
DIRECTORY_SEPARATOR
since what I am doing is requesting a URL, so the '/' slash should be used, so the code would be the following way:Another way to fix it would be to define a variable that contains the URL separator like so:
and use in the ajax request code:
Thanks for the help!