Home > Software engineering >  HTML POST method receiving empty arrays when submiting anything on PHP file
HTML POST method receiving empty arrays when submiting anything on PHP file

Time:06-07

I'm creating a searchbar, but if I type anything on it, it just returns me to the index file. It doesn't matter if I type something or not, it just redirects me to the index file, which means there's nothing set on $_POST['search']. I don't know why this is happening, I've been using this method on other files in the same proyect and it worked well.

Form Code:

<form name="search" method="post" action="search.php"  style="padding-top: 5px;">
    <div >
        <input type="text"  placeholder="...">
        <div >
            <button  type="submit">Search</button>
        </div>
    </div>
</form>

search.php code:

<?php
    include('db.php'); //mysql database connection

    if(!isset($_POST['search'])){
        header('location: index.php');
    }

    $filtervalues = $_POST['search'];

?>

Thank you for your time.

CodePudding user response:

You should use name attribute on input tag, not the form.

<input type="text" name="search"  placeholder="...">

CodePudding user response:

add the attribute name="search" to your button

<button name="search" type="submit">Search</button>

or you can replace the tag button by the tag input like this :

<input type="submit" name="search"  placeholder="..."   />
  • Related