Show or hide records with AJAX .. FOR A SEARCHER?

0

a screen that allows searches of vehicle availability, where the customer can choose the car, and tell the date and time of collection and delivery. The search result must be updated by Ajax, as soon as conditions change.

No AJAX result.

index.php

    $obj = new Arriendo_autos();

    print "<h1>Arriendo de autos</h1>";

    print "<form action='#' method='post'>";

    print "Fecha de arriendo";

    print "<input type='text' id='fechaa' name='fechaa'>";

    print "Fecha de devolución";

    print "<input type='text' id='fechad' name='fechad'>";

    print "<br>";

    print "<input type='submit' id='btn' value='Buscar'>";

    print "</from>";

    print "<div id='responsecontainer' align='center'>";

    <script type="text/javascript">

    $(document).ready(function(){
       function mostrarBD() {
        $.ajax({
         url: 'buscador.php',
         type: 'get',
         datatype: 'html',
         data: {search: $('#btn').val()},
         success: function(response) {
           $("#responsecontainer").html(response);
          }, 
         error : function() {
            alert("Error!");
         }
       });
      }
      $('#btn').click(function(){
        mostrarBD();
      });

      $('form').submit(function(e){
        e.preventDefault();
            mostrarBD();
      });
    });   
</script>

searcher.php

$obj = new Arriendo_autos();

$arriendos = $obj->getBuscar();

        print "<table>";
        if (is_array($arriendos) || is_object($arriendos))
        {

            foreach ($arriendos  as $arriendo) 
            {
                print "<tr>";
                    print "<td>".$arriendo["titulo"]." <a href='#'>Reservar</a></td>";
                print "</tr>";
            }

        }
        print "</table>";

function.php

function getBuscar()
    {

        $fechaa = (isset($_GET['fechaa']) ? $_GET['fechaa'] : null);
        $fechad = (isset($_GET['fechad']) ? $_GET['fechad'] : null);

        echo $sql = "SELECT * FROM arriendos AS a INNER JOIN incidentes AS i ON a.idarriendo = i.idarriendos WHERE a.fecha_arriendo >= '".$fechaa."' AND a.fecha_devolucion <= '".$fechad."'"; 

        $res = $this->mysqli->query($sql);

        while($row = $res->fetch_assoc())
        {
            $data[] = $row;
        }

        if(isset($data))
        {
          return $data; 
        }
    }
    
asked by Diego Sagredo 14.10.2016 в 02:37
source

1 answer

0

You have several errors in each of the files, I have corrected some but since I do not have a way to prove it I do not know if this time it will work at all.

index.php

print "<h1>Arriendo de autos</h1>";
print "Fecha de arriendo";
print "<input type='text' id='fechaa' name='fechaa'>";
print "Fecha de devolución";
print "<input type='text' id='fechad' name='fechad'>";
print "<br>";
print "<input type='button' id='btn' value='Buscar'>";
print "<div id='responsecontainer' align='center'></div>";
<script type="text/javascript">

$(function(){
    $("#btn").click(function(){
        $.ajax({
            url: 'buscador.php',
            type: 'get',
            datatype: 'html',
            data: {fechaa: $('#fechaa').val(), fechad:$('#fechad').val()},
            success: function(response) {
                $("#responsecontainer").html(response);
            }, 
            error : function() {
                alert("Error!");
            }
        });
    });
});  
</script>

searcher.php

$obj = new Arriendo_autos();

$arriendos = $obj->getBuscar($_GET['fechaa'], $_GET['fechad']);

print "<table>";
if (is_array($arriendos) || is_object($arriendos))
{

    foreach ($arriendos  as $arriendo) 
   {
        print "<tr>";
        print "<td>".$arriendo["titulo"]." <a href='#'>Reservar</a></td>";
        print "</tr>";
   }

}
print "</table>";

function.php

function getBuscar($fechaa, $fechab)
{
    echo $sql = "SELECT * FROM arriendos AS a INNER JOIN incidentes AS i ON a.idarriendo = i.idarriendos WHERE a.fecha_arriendo >= '".$fechaa."' AND a.fecha_devolucion <= '".$fechad."'"; 

    $res = $this->mysqli->query($sql);

    while($row = $res->fetch_assoc())
    {
        $data[] = $row;
    }

    if(isset($data))
    {
      return $data; 
    }
}

I recommend that you check if buscador.php returns the correct values by accessing the file in this way searcher.php? fechaa = 2016/20/01 & fechab = 2016/21/01 (writing the dates correctly)

Once you know that the file works properly, go back to the index.php and perform the search with the form.

    
answered by 14.10.2016 / 06:05
source