Advanced search by button

0

I've been looking for a way to do the typical search in the navigation bar but this time using a button with the appearance of a link, I intend to make a MercadoLibre type filter

This is my code:

<?php session_start();

require 'extras/config.php';
require 'functions.php';

comprobarSession();

$conexion = conexion($bd_config);
if (!$conexion) {
    header('Location: error.php');
}

if ($_SERVER['REQUEST_METHOD'] == 'GET' && !empty($_GET['1000'])) {
    $busqueda = limpiarDatos($_GET['1000']);

    $statement = $conexion->prepare('SELECT * FROM publications WHERE salario LIKE :1000');

    $statement->execute(array(':1000' => "%$busqueda%"));
    $resultados = $statement->fetchAll();
    print_r($statement);

    if(empty($resultados)){
        $titulo = 'No se encontraron articulos con el resultado '. $busqueda;
    } else {
        $titulo = 'Resultados de la busqueda: ' . $busqueda;
    }
} 

require 'views/1000+.php';

?>

and my button is this:

<form action="1000+.php" method="GET">
   <input type="submit" class="btn btn-link" value="$1,000+">$1,000+</input>(16947)
</form>
    
asked by Cesar Gutierrez Davalos 20.05.2017 в 20:10
source

1 answer

0

First try to change the name of the file to another as 1000more.php avoid special characters in the names.

Second, the HTML inputs are not lockable tags, do this:

<input type="submit" class="btn btn-link" value="$1,000+">$1,000+</input>(16947)

It's totally wrong, you should do it like this:

<input type="submit" class="btn btn-link" value="$1,000+" />(16947)

And ready, to start, third:

<form action="1000+.php" method="GET">

There is no need to use the GET method, you can send it by POST in the same way, if you want GET then you could do it in another way by having JS take care of the redirection to generate a special URL query encoded to avoid that, you get "strange characters" and then simply decode them. Since the browser does it, but for example Hola%20mundo you should seek to do it as Hola+mundo as preferred (this is extra data, it is not necessarily done in this way).

Part of the error

Now, here is the error:

You make a form that sends a value without a name, that is: Your url would look something like: http://localhost/1000+.php?1000

But as such there is no value, then it is "empty" the part $ _GET ['1000']

What you occupy is to make an extra input, something to make your form look like this:

<form action="1000+.php" method="GET">
   <input type="hidden" value="1000" name="valor" />
   <input type="submit" class="btn btn-link" name="send">$1,000+</input>(16947)
</form>

And then instead of !empty($_GET['1000']) you would use something like: !empty($_GET['valor']) and you should get the value since the URL would look correct but it would include the name "send" of the URL looking:

http://localhost/1000+.php?valor=1000&send

If I'm not mistaken, anything, tell me what happens with the changes and continue to give the solution.

    
answered by 20.05.2017 / 20:41
source