POST ajax and combo box problem

-1

I'm trying to update a combo box dynamically, so I have two combo box "predata" and "prehora" , when the user selects a value in the predata field the pre-field has to perform a query in the database with the predata parameter , but the post method is not working, follow the codes;

principal.php

<form action="process.php?action=add-cons" method="post" name="frmShipment" > 

          <tr>
            <td colspan="3" align="right" class="TrackNormalBlue" style="text-align: center"><strong>AGENDAMENTO DE HORÁRIO</strong></td>
            </tr>
          <tr>
            <td align="right" bgcolor="#C7BFBF" class="TrackNormalBlue"><strong>Data:</strong></td>
            <td bgcolor="#C7BFBF">&nbsp;</td>
            <td bgcolor="#C7BFBF"><select id="predata" name="predata">
                <option>Selecione...</option>
<?php
$hoje = date('d/m/Y');
$query = mysql_query("SELECT DISTINCT Data FROM tbl_agenda where Agendado like '0' and data >= '$hoje' order by data ");
 while($prod = mysql_fetch_array($query)) { ?>
 <option value="<?php echo $prod['Data'] ?>"><?php echo $prod['Data'] ?></option>
 <?php } ?>


          </tr>
           <td align="right" bgcolor="#C7BFBF" class="TrackNormalBlue"><strong>Hora:</strong></td>
           <td bgcolor="#C7BFBF">&nbsp;</td>
           <td bgcolor="#C7BFBF"><select id="prehora" name="prehora">
                <option>Selecione Uma Data</option>



          </tr>
          <tr>

            <td><input name="Submit" type="submit" onClick="return document.MM_returnValue" value="Adicionar Embarque"></td>
          </tr>
        </tbody></table></td>
      </tr>
      <tr>
        <td>&nbsp;</td>
      </tr>
    </tbody></table></td>
    <td background="images/boxrightBG.gif"></td>
  </tr>
  <tr>
    <td width="18"><img src="images/boxbtmleftcorner.gif" alt="" height="12" width="18"></td>
    <td background="images/boxbtmBG.gif" width="734"></td>
    <td width="18"><img src="images/boxbtmrightcorner.gif" alt="" height="12" width="18"></td>
  </tr>
</tbody></table>
<br>
</form> 

<script type="text/javascript">

      $(document).ready(function(){
        // Evento change no campo tipo  
         $("select[name=predata]").change(function(){
            // Exibimos no campo marca antes de concluirmos
$("select[name=prehora]").html('<option value="">Carregando...</option>');
            // Exibimos no campo marca antes de selecionamos a marca, serve tamb?m em caso
// do usuario ja ter selecionado o tipo e resolveu trocar, com isso limpamos a
// sele??o antiga caso tenha feito.
// Passando tipo por parametro para a pagina ajax-marca.php
            $.post("ajax-prehora.php",
                  {tipo:$(this).val()},
                  // Carregamos o resultado acima para o campo marca
 function(valor){
                     $("select[name=prehora]").html(valor);
                  }
                  )
         })
      // Evento change no campo marca 




 })

</script>

ajax-prehora.php

<?php
session_start();
require_once('library.php');




$predata = $_POST['predata'];


    $sql = "SELECT * FROM tbl_agenda WHERE Data = '$predata' ORDER BY Hora";
    $qr = mysql_query($sql) or die(mysql_error());

    if(mysql_num_rows($qr) == 0){
       echo  '<option value="0">'.htmlentities('Carregando...').'</option>';

    }else{
          echo '<option value="">Selecione a Data..</option>';
       while($ln = mysql_fetch_assoc($qr)){
          echo '<option value="'.$ln['Data'].'">'.$ln['Data'].'</option>';

       }
    }
    ?>

I made the test to put the fixed date in the query and it works however this field needs to come according to the user's selection.

 $sql = "SELECT * FROM tbl_agenda WHERE Data = '13/06/2016' ORDER BY Hora";

Fixed

I edited the line:

 $.post("ajax-prehora.php",
                  {tipo:$(this).val()},

To

 $.post("ajax-prehora.php",
                  {predata:$(this).val()},
    
asked by anonymous 14.06.2016 / 03:00

3 answers

1

I saw many syntax errors in your code, but I saved you from them.

$(document).ready(function() {
    $("select[name=predata]").change(function() {
        $("select[name=prehora]").html('<option value="">Carregando...</option>');
        var $data = $(this);
        $.post("ajax-prehora.php",
            {
                'data': {
                    'predata': $data.val()
                },
                'success': function(response) {
                    $("select[name=prehora]").html(response);
                }
            }
        );
    }
});

You were not sending the value of predata to $_POST['predata'] to PHP. In the second parameter object in call $.post you declare the object data and the data inside it. $.post will automatically recognize and send to the URL of POST .

    
14.06.2016 / 03:31
1

Klaider is correct, so it can be!

$(document).ready(function() {
$("select[name=predata]").change(function() {
    $("select[name=prehora]").html('<option value="">Carregando...</option>');
    var $data = $(this);
    $.ajax({
            'url': "ajax-prehora.php",
            'method':'POST',
            'data': {'predata': $data.val() },
            'success': function(response) {
                $("select[name=prehora]").html(response);
            }
        }
    );
}
});

Ajax / JQuery settings, date, url, success

    
14.06.2016 / 19:27
0

I edited the line:

 $.post("ajax-prehora.php",
                  {tipo:$(this).val()},

To

$.post("ajax-prehora.php",
                  {predata:$(this).val()},
    
14.06.2016 / 03:30