0

I want my search form to display users(username,firstname or lastname) from my database and display them as the user is typing like how facebook and twitter do but when i click search,nothing happens.Here are 2 parts of my header inc that deal with the search form,1st part:

 <?php 
  include( "Connect.php" ); 
  ob_start(); session_start();
  if (isset($_SESSION['user_login'])) {
  $user = $_SESSION["user_login"];
  }
else {
$username = "";
}

$output = "";
//Collect
if(isset($_POST['search'])) {
    $searchq = $_POST['search'];
    $searchq = preg_replace("#[^0-9a-z]#i","",$searchq);

$query = $mysqli->query("SELECT * FROM users WHERE first_name LIKE '%$searchq%' OR last_name LIKE '%$searchq%'") or die("could not search!");
    $count = mysqli_num_rows($query);
    if($count == 0) {
        $output = 'There was no search results!';
        } else {
            while($row = mysqli_fetch_array($query)) {
                $fname = $row['first_name'];
                $lname = $row['last_name'];
                $id = $row['id'];

                $output .= '<div> '.$fname.' '.$lname. '</div>';

                }
                }
                } 
?> 

2nd part:

<form id="searchForm">
                <fieldset>
                    <div class="input">
                        <p>                          
                          <form action="header inc.php" method="post"> 
                          <input type="text" class="Search" id="search" size="35" placeholder="Search username,fullname or topic"/>
                          <label for="Submit"></label>
                          <input type="submit" name="search" value="Go" id="search" />
                        </p>
                    </div>
                    </form>
     <?php print ("$output"); ?>
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
Antony
  • 51
  • 7

2 Answers2

1

invalid action name

 action="header inc.php"

you cannot use space in .php files

Edit the filename as headerInc.php and pass it in the form as:

 action="headerInc.php"
Danyal Sandeelo
  • 12,196
  • 10
  • 47
  • 78
-1

I have changed your html code , just use it and it will start working. In your html there is hierarchy of form tag , I have added method post to first form and I have replaced the name of the text field by submit button name. In each post your data value was "go".

I hope it will resolve your problem :

  <form id="searchForm" method="post">
        <fieldset>
            <div class="input">
                <p>                          
                <form action="header inc.php" method="post"> 
                    <input type="text" class="Search" name="search" id="search" size="35" placeholder="Search username,fullname or topic"/>
                    <label for="Submit"></label>
                    <input type="submit"  value="Go" id="search_button" />
                    </p>
            </div>
    </form>
    <?php echo $output; ?>
Satender K
  • 571
  • 3
  • 13