Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6719031
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:02:40+00:00 2026-05-26T09:02:40+00:00

I have a search form and am displaying the results on the same page

  • 0

I have a search form and am displaying the results on the same page as the search form. So, before a search takes place, all the possible results are visible. Once a keyword is entered into the form, the results are only those that contain the keyword. I would like the form to display the keyword that was entered into the form when the results are displayed. I have tried multiple things and my latest attempt is to enter into various parts of the form, but with no luck. I have searched multiple tutorial sites and am not finding this particular request to be addressed. Can any of you help me to get the search criteria to remain in the search box when the results are displayed? Here is the code I am using:

    <html>
    <body>
    <?php error_reporting (E_ALL ^ E_NOTICE); ?>  
    <?php 
        function getRecords($query) {
        $con = mysql_connect("localhost", "movie", "moviepw");
        if (!$con)
        {
        die('Could not connect: ' . mysql_error());
        } 
        mysql_select_db("movies", $con);

        $result = mysql_query($query);
        return $result;   
    }

    function buildQuery() {

    $keyword = $_GET['keyword'];
    $sql = "SELECT * from table1 WHERE (movie_title LIKE '%$keyword%'
                     OR movie_description LIKE '%$keyword%')";
    return $sql;
    } ?>

   <form action="movie_form.php" method=get>
    <fieldset>
     <legend>Movies</legend>
     <label for="keyword">Search</label>
     <input id="keyword" name="keyword" />
     <input type=submit name=submit value=Search />
     <? echo $keyword ?>
    </fieldset>
   </form>

    <?
      $query = buildQuery();
      $records = getRecords($query);

     while($row = mysql_fetch_array($records)){ ?>
    <table>
      <tbody>
       <table border='1'>
        <tr>
         <td><?= $row['movie_title']; ?></td>
         <td><?= $row['movie_rating']; ?></td>
         <td><img src="<?= $row['movie_image'];?>"></td>
         <td><?= $row['movie_description']; ?></td>
         <td><a href="movie_form.php">Return to Search</a></td>
        </tr>
      <? } ?>
      </tbody>
     </table>
 </body>
 </html>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T09:02:40+00:00Added an answer on May 26, 2026 at 9:02 am

    Add the “value” attribute to your “keyword” form input. Something like this:

    <input id="keyword" name="keyword" value="<?php echo "$keyword"; ?>"/>
    

    Also, move the setting of $keyword out of the buildQuery() method so that it will be available when your form is output. You could put it right after error_reporting is set:

    <?php error_reporting (E_ALL ^ E_NOTICE); 
          $keyword = rtrim($_GET['keyword']);
    ?>
    

    Finally, this code is vulnerable to sql injection. Check this out:
    How can I prevent SQL injection in PHP?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

what I have: a google search form (displayed on all pages) a wordpress page
I have a search form & knockout-based grid for results. When search is performed,
I have a page with a search bar, facets, and a list of results.
I have two pages. Search page is the first page that takes user inputs.
i have search form in my page. On page load it is on focus
I have a page with a search form, and when submitted, the form action
I have a Search form in my database that takes in a string, explodes
I have a search form. I would like when someone type the term, first
I have a search form where user can search for products within their specified
I have a search form on my site that submits the url in the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.