How to show alert when search does not match BD - PHP

0

Hi, I'm designing a PHP site with MySQL and I would like it every time a search is made, by previously selecting the values of 4 combobox respectively; If the data in these drop-down lists do not match the values in the database, I throw an error saying, for example:

'Your search does not match our records', hopefully with JavaScript, which is quite simple.

I leave an image of the site that I have, and the type of message I hope to show.

NOTE: That message I modified because it corresponded to an error message that I created for when the user left at least one combobox unselected.

I also add the code I use for the search.

Page that sends the form:

<?php 
    include ("../php/cabecera.php");
    include ("../php/util.php");
    include ("../php/sql.php");

    $link = myinit();
    $usuario = $_SESSION['usuariocookie'];

    /*ESTE query ME LLENA LA TABLA QUE SE CARGA AL ENTRAR AL SITIO*/

    $query="SELECT detalle.id_alerta, 
        detalle.id_detalle, 
        R.id_region, 
        detalle.id_ruta, 
        detalle.id_segmento, 
        detalle.id_parada, 
        sc.descripcion_sucursal, 
        items.descripcion_item, 
        detalle.estado_ruta, 
        detalle.imei, 
        detalle.fecha_ing, 
        detalle.hora_ing, 
        detalle.conductor, 
        detalle.tipo_alerta, 
        detalle.descripcion,
        CASE WHEN detalle.leida = 0 THEN 'NO' 
        WHEN detalle.leida = 1 THEN 'SI'
        END,
        u.Nombres, 
        detalle.fecha_leida, 
        detalle.hora_leida, 
        detalle.obs_leida,  
        clientes.descripcion_cliente, 
        justificacion.justificacion
        FROM alertas_detalle AS detalle 

        LEFT JOIN alerta_justificacion AS justificacion ON 
        detalle.id_justificacion = justificacion.id_justificacion 
        INNER JOIN tipo_items items ON detalle.id_tipo_item = items.id_tipo 

        INNER JOIN clientes AS clientes ON detalle.id_cliente = 
        clientes.id_cliente
        INNER JOIN sucursales AS sc ON detalle.id_sucursal = sc.id_sucursal
        INNER JOIN usuarios AS u ON detalle.usuario_leida = u.id_usuario
        INNER JOIN region AS R ON detalle.id_region = R.id_region

        ORDER BY detalle.id_alerta";


    /*ESTAS VARIABLES $ValorRuta, $ValorCliente, $ValorRegion y $UserVal son 
    las que rescatan el valor tomado desde los combobox, respectivamente*/

    if ($_POST['Submit']) 
    {
      $cadena="";
      $cadena_excel="";

      $UserVal= $_POST['userA'];
      $ValorRuta= $_POST['RutaAl'];
      $ValorRegion = $_POST['RegionA'];
      $ValorCliente = $_POST['ClientesA'];

        if ($_POST['Submit']=="BUSCAR")
        {

        $cadena = $cadena."&RutaAl=".$ValorRuta;
        $cadena_excel = $cadena_excel."?&RutaAl=".$ValorRuta;

        $cadena = $cadena."&userA=".$UserVal;
        $cadena_excel = $cadena_excel."?&userA=".$UserVal;

        $cadena = $cadena."&RegionA=".$ValorRegion;
        $cadena_excel = $cadena_excel."?&RegionA=".$ValorRegion;


        $cadena = $cadena."&ClientesA=".$ValorCliente;
        $cadena_excel = $cadena_excel-"?&ClientesA=".$ValorCliente;

        }
    }


    $result=mysql_query($query,$link);
?>

And I add the form.

Form of the same site that processes the data:

<form method="POST" action="resultado_alertas.php" name="formAlertas">
    <table width="1000" cellpadding="0" cellspacing="0">
        <tr>
            <td width="125" class="Verde_tit">
                <td class="Verde_tit">RUTA:&nbsp;
                    <select name="RutaAl">
                        <?php echo(ObtenRutaAlertas($link,$RutaAl));?>
                    </select>
                </td>

                <td class="Verde_tit">&nbsp;USUARIO:&nbsp;
                    <select name="userA">
                        <?php echo(ObtenUsuarioAlertas($link,$userA));?>
                    </select>
                </td>
                <td class="Verde_tit">&nbsp;REGION:&nbsp;
                    <select name="RegionA">
                        <?php echo(ObtenRegionAlertas($link,$RegionA));?>
                    </select>
                </td>

                <td class="Verde_tit">&nbsp;CLIENTE:&nbsp;
                    <select name="ClientesA">
                        <?php echo(ObtenClientesAlertas($link,$ClientesA));?>
                    </select>
                </td>

                <td>
                    <input type="submit" name="Submit" id="Submit" value="BUSCAR" class="bot_buscar1" onclick="return valida();">
                </td>
            </td>
        </tr>
    </table>
</form>

I remain attentive to your answers, suggestions and / or comments. Thanks

    
asked by M4uriXD 25.04.2018 в 16:33
source

1 answer

0

You could use Ajax for this, perfectly.

In your JavaScript code you would have something like

  $('#Submit').on('click',function(){
       $.post("resultado_alertas.php",
        {
          userA : $('#userA').val(),
          RutaAl: $('#RutaAl').val(),
          RegionA: $('#RegionA').val(),
          ClientesA: $('#ClientesA').val(),
          Submit: "BUSCAR"
        },
        function(data){
            if(!data)
            {
               // Alerta
            }
        });
    });

Afterwards in your PHP code, after the result you look at the number of rows and return it.

$result=mysql_query($query,$link);
$numero_filas = mysql_num_rows($result);

echo $numero_filas;

Remember that in order for the Submit of the form not to occur you have to change its action:

<form onsubmit="return false" />
    
answered by 25.04.2018 / 17:19
source