Click here to Skip to main content
15,891,976 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I move code to index.php because I want it to be displayed in current divs.
When I did it this happened - code here:
PHP
<<?php session_start();
 // Check if the user is logged in, if not then redirect him to login page
 if(!isset($_SESSION["loggedin"]) || $_SESSION["loggedin"] !== true){
     header("location: login.php");
     exit;
     echo $username;
 } ?>
   <?php
    $searchtype=$_POST['searchtype'];
    $searchterm=trim($_POST['searchterm']);
    if(!$searchtype || !$searchterm) {
        echo '<p>Nezadali ste niektory z udajov pre vyhladavanie.<br>
        Vratte sa prosim spat a skuste to znovu!</p>';
        exit;
    }
    switch ($searchtype) {
        case 'author';
        case 'title';
        case 'isbn';
            break;
        default: echo '<p>Zadali ste neplatny typ hladania.<br>Vratte sa prosim spat a skuste to znovu.</p>';
        exit;
    }

    @$db = new mysqli('localhost:3306','root','','books');
    if (mysqli_connect_errno()) {
        echo '<p>Chyba: Nepodarilo sa pripojit k databaze.<br>
        Kontaktujte prosim administratora.</p>';
        exit;
    }

    $query = "SELECT isbn, author, title FROM books WHERE $searchtype LIKE ?";
    $stmt = $db->prepare($query);
    $searchterm= '%'.$searchterm.'%';
    $stmt->bind_param('s',$searchterm);
    $stmt->execute();
    $stmt->store_result();
    
    $stmt->bind_result($isbn,$author,$title);
    echo "<p>Pocet najdenych knih: ".$stmt->num_rows."</p>";

    while($stmt->fetch()) {
        echo "<p>Nazov: ".$title."";
        echo "<br>Autor: ".$author;
        echo "<br>ISBN: ".$isbn;
    }

    $stmt->free_result();
    $db->close();
    ?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="styles.css">
    <script src="https://kit.fontawesome.com/8a0ad067a9.js" crossorigin="anonymous"></script>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>

    <title>Pozicovna knih</title>
</head>
<body>
    <div class="container">
      
        <div class="item"><p><h2>Vitajte <?php if(isset($_SESSION['username'])){
    echo " '{$_SESSION['username']}'";
} ?> !</p></p></h2>
</div>
					<div class="form">
                        <form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post"> 
                        <p>Vyberte typ hladania:    
                        <select name="searchtype">
                                <option value="author">Autor</option>
                                <option value="title">Nazov</option>
                                <option value="isbn">ISBN</option>
                            </select></p>
                            <p>Zadajte hladany vyraz
                            <input name="searchterm" type="text" size="40"></p>
                            <p>
                                <input type="submit" name="submit" value="Hladat">
                            </p>
                        </form>
                    </div>
            </div>
				
         </div>
      </div>
      <div class="item">    <form action="logout.php">
    <input type="submit" class="button button1" value="Logout" /></div>
    </div>

</form>
</body>
</html>


Writes:
Warning: Undefined array key "searchtype" in D:\xampp\htdocs\index.php on line 9

Warning: Undefined array key "searchterm" in D:\xampp\htdocs\index.php on line 10


Please write info about what to do with PHP to have it on the same page.

What I have tried:

Move code from results.php to index.php.
Posted
Comments
Richard MacCutchan 19-Nov-21 7:15am    
It looks like the PHP code is trying to run as soon as the page gets loaded, rather than waiting for the form post. Check with the original code to see if you missed something.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900