Php Result page to be displayed on same page

26,193

Put your form and PHP in one page, PHP on top with HTML below, then use action=""

Use the variables from the inputs as $var=$_GET['var']; then echo $var;

Sidenote: If you want to stop a process, you can use die(); or exit();

You can put a message inside it; i.e.: die("Enter a search term");

A basic example:

<?php
if(isset($_GET['submit'])){
$query=$_GET['query'];
echo $query;
}
?>

<html>
<head>
<title>Search</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="style.css"/>
</head>
<body>
    <form action="" method="GET">
        <input type="text" name="query" />
        <input type="submit" name="submit" value="Search" />
    </form>
</body>
</html>

An alternative, showing an error message if field is empty:

<?php
if(isset($_GET['submit'])){
  if(empty($_GET['query'])){
echo "Enter a search term";
  }

$query=$_GET['query'];
echo $query;
}
?>

<html>
<head>
<title>Search</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="style.css"/>
</head>
<body>
    <form action="" method="GET">
        <input type="text" name="query" />
        <input type="submit" name="submit" value="Search" />
    </form>
</body>
</html>
Share:
26,193
Nau Nau head
Author by

Nau Nau head

Updated on February 12, 2020

Comments

  • Nau Nau head
    Nau Nau head about 4 years

    This is my coding for the search form:

    <html>
    <head>
    <title>Search</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link rel="stylesheet" type="text/css" href="style.css"/>
    </head>
    <body>
        <form action="https://excelforth.com/search1.php" method="GET">
            <input type="text" name="query" />
            <input type="submit" value="Search" />
        </form>
    </body>
    </html>
    

    The thing is , When I submit this form on my web page , It goes to the php Results page and displays the results. But the thing is that it is just a plain page with just the results displayed.

    My search page :

    www.mysite/test/certification-database-search/
    

    The Php file is located in my webroot folder . I was told that for wordpress , the individual subpages cannot be edited .

    How do I :

    1.Display the results on the same page as the search page. Not a completely new page.

    2.Retain the page layout and theme / headers/ footers of the page

    3.If possible run the .php file in /certification-database-search/ and query it from there. instead of using the one in my webroot folder.

    THANKS!!