title: dynamic select with ajax php and mariadb
body: <p>I am triying to do a dynamic select using ajax, php and querys to a BBDD.
  For this reason I have three parts, the html where I populated my options of selects with the consults that I am doing to the BBDD, this consult is realised with AJAX and the scripts to consult BBDD is do it in php.
  My html is the following:</p>
  
  <pre><code>&lt;div class="student-data-block"&gt;
                      &lt;div class="row"&gt;
                          &lt;div class="col-md-6"&gt;
                              &lt;label&gt;Nombre&lt;/label&gt;
                              &lt;input type="text" name="name" value="" placeholder=""/&gt;
                              &lt;br/&gt;
                              &lt;label&gt;Apellido&lt;/label&gt;
                              &lt;input type="text" name="last_name" value="" placeholder=""/&gt;
                              &lt;br/&gt;
                              &lt;label&gt;Universidad&lt;/label&gt;
                              &lt;select name="universitySelect" id="universitySelect" value="" placeholder=""&gt;
                                  &lt;option value=""&gt;Escoger universidad&lt;/option&gt;
                                  &lt;?php foreach ($universities as $name) { ?&gt;
                                  &lt;option value="&lt;?php echo $name['UniversityID']?&gt;"&gt;&lt;?php echo $name['UniversityName']?&gt;&lt;/option&gt;
                                  &lt;?php } ?&gt;
                              &lt;/select&gt;
                          &lt;/div&gt;
                          &lt;div class="col-md-6"&gt;
                              &lt;label&gt;Campus&lt;/label&gt;
                              &lt;select name="campusSelect" id="campusSelect" value="" placeholder=""&gt;
                                  &lt;option value=""&gt;Escoger campus&lt;/option&gt;
                              &lt;/select&gt;
                              &lt;br/&gt;
                              &lt;label&gt;Carrera&lt;/label&gt;
                              &lt;select name="careerSelect" id="careerSelect" value="" placeholder=""&gt;
                                  &lt;option value=""&gt;Escoger carrera&lt;/option&gt;
                              &lt;/select&gt;
                              &lt;br/&gt;
                              &lt;label&gt;Nivel académico&lt;/label&gt;
                              &lt;select name="study_type" value=""
                                      placeholder=""&gt;
                                  &lt;option value="grado"&gt;Escoger nivel académico&lt;/option&gt;
                                  &lt;option value="grado"&gt;Grado&lt;/option&gt;
                                  &lt;option value="master"&gt;Máster&lt;/option&gt;
                                  &lt;option value="postgrado"&gt;Postgrado&lt;/option&gt;
                                  &lt;option value="doctorado"&gt;Doctorado&lt;/option&gt;
                              &lt;/select&gt;
                              &lt;br/&gt;
                          &lt;/div&gt;
                      &lt;/div&gt;
  </code></pre>
  
  <p>First we consult the BBDD and populate the first select, the #universitySelect, after to populate the followings selects I need the option choosen for the user, to populate the others select in function to the option choosen. To build the followings we use Ajax, exactly this code:</p>
  
  <pre><code>$(document).ready(function() {
          $('#universitySelect').change(function() {
              var universityID = $("#universitySelect").val();
              if (universityID) {
                  $.ajax({
                      type: 'POST',
                      url: 'action/AjaxSelect.php',
                      data: 'universityID=' + universityID,
                      success: function (html) {
                          $('#campusSelect').html(html);
                          $('#careerSelect').html('&lt;option value=""&gt;Selecciona tu carrera&lt;/option&gt;');
                      }
                  });
              } else {
                  $('#campusSelect').html('&lt;option value=""&gt;Selecciona tu campus primero&lt;/option&gt;');
                  $('#careerSelect').html('&lt;option value=""&gt;Selecciona tu carrera primero&lt;/option&gt;');
              }
          });
          $('#campusSelect').change(function(){
                  var campusID = $("#campusSelect").val();
                  if (campusID) {
                      $.ajax({
                          type: 'POST',
                          url: 'action/AjaxSelect.php',
                          data: 'campusID=' + campusID,
                          success: function (html) {
                              $('#careerSelect').html(html);
                          }
                      });
                  }
                  else {
                      $('#careerSelect').html('&lt;option value=""&gt;Selecciona tu carrera primero&lt;/option&gt;');
                  }
              });
      }   );
  </code></pre>
  
  <p>Finally when I receive the data of the AJAX I consult the BBDD to populate the others select. My AjaxSelect.php is the following:</p>
  
  <pre><code>    if(!empty($_REQUEST["universityID"])){ //&amp;&amp; isset($_REQUEST["universityID"])
      $q_campus = 'select c.CampusID, c.Name as CareerName from unithinks.campus c
                where UniversityID="$_REQUEST["universityID"]" order by c.CampusID asc ';
  
      //Count total number of rows
      $rowCampus = query($db_connection, $q_campus);
  
      //State option list
      if($rowCampus &gt; 0){
          echo '&lt;option value=""&gt;Selecciona campus&lt;/option&gt;';
          foreach ($rowCampus as $campus){
              echo '&lt;option value="'.$campus['CampusID'].'"&gt;'.$campus['CareerName'].'&lt;/option&gt;';
          }
      }else{
          echo '&lt;option value=""&gt;Selecciona primero universidad&lt;/option&gt;';
      }
  }
  else if(!empty($_REQUEST["campusID"])){
      $q_careers = 'select ca.CareerID, ca.Name as CareerName from unithinks.careers ca
                where CampusID = "$_REQUEST["campusID"]" order by ca.CareerID asc ';
      $rowCareers = query($db_connection, $q_campus);
  
      //State option list
      if($rowCareers &gt; 0){
          echo '&lt;option value=""&gt;Selecciona Carrera&lt;/option&gt;';
          foreach ($rowCareers as $careers){
              echo '&lt;option value="'.$careers['CampusID'].'"&gt;'.$careers['CareerName'].'&lt;/option&gt;';
          }
      }else{
          echo '&lt;option value=""&gt;Selecciona primero universidad o campus&lt;/option&gt;';
      }
  </code></pre>
  
  <p>I don't see where is the error. I know that the code function well until arrive at ajax petition, in the Ajax I don't know to see if it is sending the POST.
  (Also, explain that the consult return the data well.).</p>
  
  <p>Sorry, I am new using php. 
  Thanks tou in advance.</p>