How can I validate a time type field with this code?

0

What I need is to modify this script so that it takes the value of a time input from which I must take the time and validate it with mysql and show the user a div that says it is available or that the time is not available selected

  $(document).ready(function(){
       $.datepicker.setDefaults({
            dateFormat: 'dd-mm-yy'
       });
       $(function(){
            $("#fechax").datepicker("");

       });
       $('#filter').click(function(){
            var fecha = $('#fechax').val();

            if(fecha != '')
            {
                 $.ajax({
                      url:"ajax_ac.php",
                      method:"POST",
                      data:{fecha:fecha},
                      success:function(data)
                      {
                           $('#order_table').html(data);

                      }
                 });
            }
            else
            {
                 alert("Selecciona una fecha");
            }
       });
  });
    
asked by Pedro Agostini 05.12.2018 в 19:07
source

1 answer

0

That's how I solved my problem

<script> function validar_hora() { var horario = document.getElementById("horario2").value; var xhr; if (window.XMLHttpRequest) { xhr = new XMLHttpRequest(); } else if (window.ActiveXObject) { xhr = new ActiveXObject("Microsoft.XMLHTTP"); } var data = "horario=" + horario; xhr.open("POST", "ajax_ac.php", true); xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); xhr.send(data); xhr.onreadystatechange = display_data; function display_data() { if (xhr.readyState == 4) { if (xhr.status == 200) { //alert(xhr.responseText); document.getElementById("horav").innerHTML = xhr.responseText; } else { alert('Existe un problema con la consulta.'); } } } } </script> and query <?php include_once("connect_db.php"); $horario = $_POST['horario']; $checkhorario = mysql_query("SELECT horario FROM aireacondicionado WHERE horario='$horario'"); $horario_exist = mysql_num_rows($checkhorario); if ($horario_exist>1) { echo ' <div class="col-md-4"><div class="alert alert-danger" role="alert"> <strong>Empresa no disponible para la hora seleccionada, por favor ingrese una nueva hora.</strong></div></div>'; echo '<br/>'; }else{ echo '<span class="alert-success">Hora '.$_POST['horario'].' disponible</span><br> <input type="hidden" class="form-control" name="horario" value='.$_POST['horario'].' ><br/> <button class="btn btn-primary" style="margin-left: 100px;" type="submit" class="btn btn-default">Enviar</button>'; } ?>

    
answered by 06.12.2018 в 01:42