Home > Software design >  Isset Variable Is Not Being Echoed Onto Page - PHP
Isset Variable Is Not Being Echoed Onto Page - PHP

Time:05-06

I have a form that is going to submit a search query with PHP via a MySQL database. The form action attribute is set to another page, namely search.php. I want to store the search term in a variable, $searchQuery, but when I try and echo that variable out in the HTML, it doesn't show any value and I'm not getting any errors?

This is the first time I've built a search form in PHP, but I don't understand why this variable isn't being echoed?

The echo "test string"; line is being outputted as expected.

OUTPUT - search.php page

<div>
<?php  
if(isset($_POST['search-submit'])) {

    $searchQuery = $_POST['search-submit'];

    if (isset($searchQuery)) {  
        echo $searchQuery;
        echo "test string";
    }
}
?>
</div>

FORM in the header of every page

<form action="search.php" method="POST">
    <label for="search">Enter Search</label>
    <input type="text" name="search" id="search">
    <button type="submit" name="search-submit" id="search-submit">Search</button>
</form>

CodePudding user response:

if you are demanding value from your button it should be

<button type="submit" name="search-submit" value="add value here" id="search-submit">Search</button>

but if you are demanding value from your input text it should be

$_POST['search'] "search" which is your input text name not $_POST['search-submit'] "search-submit"which is your button name
  • Related