'How to display the retrieved data in the same page using PHP

I have created a table with 3 fields and stored values into it. And I've created a form where different operations can be performed. One of them is retrieve. When I click retrieve button, the requested data from the table is displayed but on a different page. I want to display that in the same page. I want to do that using PHP alone. Is that possible?

<!DOCTYPE html>
<html>
<head>
<title>Untitled Document</title>
</head>
<body>

<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$dbname = 'DB';
$conn = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
if(mysqli_connect_error()) 
{
die("couldn't connect" . $conn->connect_error());
}
$id = $_POST['Id'];
$name = $_POST['Name'];
$blood = $_POST['BloodGroup'];


if(isset($_POST['insert'])){
        $insert = "Insert into ins(Id, name, BloodGroup) values ('$id','$name', '$blood')" ;
        if($conn->query($insert) === TRUE) {
        echo ("Input data entered successfully");
        } else {
        echo ("Input data failed to be entered" . $conn->error());
        }
        $conn->close();
} elseif(isset($_POST['update'])) {

        $update = "update ins set Name='".$name."', BloodGroup='".$blood."' where Id='".$id."'";
        mysql_query($update);
        if($conn->query($update) === TRUE) {
        echo ("Data updated successfully");
        } else {
        echo ("Data cant be updated" . $conn->error());
        }
        $conn->close();
} elseif(isset($_POST['delete'])) {
        $id = $_POST['Id'];
        $delete = "delete from ins where Id='".$id."'";
        if($conn->query($delete) === TRUE) {
        echo ("Data deleted successfully");
        } else {
        echo ("Data cant be updated" . $conn->error());
        }
        $conn->close();
}
else {
$id = $_POST['Id'];
$retrieve = "SELECT * FROM ins WHERE Id = ".$id;
if ($result=mysqli_query($conn,$retrieve))
 {
  while ($row=mysqli_fetch_row($result))
{
echo '<table>
<tr>
<td>Id</td>
<td>Name</td>
<td>Blood Group</td>
</tr>
<tr>
<td>'.$row[0].'</td>
<td>'.$row[1].'</td>
<td>'.$row[2].'</td>
</tr>
</table>';
//$row[0],$row[1],$row[2]';
}
   mysqli_free_result($result);
}}
$conn->close();
?>

<h2>SELECT THE OPERATION YOU WANT TO PERFORM<h2>
<form method="post">
Id: <input type="text" name="Id" />
Name: <Input type="text" name="Name" />
BloodGroup: <input type="text" name="BloodGroup" /><br /><br />
<input type="submit" name="insert" value="Insert" />
<input type="submit" name="update" value="Update" />
<input type="submit" name="delete" value="Delete" />
<input type="submit" name="retrieve" value="retrieve" />
</form>
</body>
</html>


Solution 1:[1]

elseif(isset($_POST['retrieve']))
{
$id = $_POST['Id'];
$retrieve = "SELECT * FROM `ins` WHERE `Id` ='".$id."'";
if ($result=mysqli_query($conn,$retrieve))
 {
  while ($row=mysqli_fetch_row($result))
{
echo '<table>
<tr>
<td>Id</td>
<td>Name</td>
<td>Blood Group</td>
</tr>
<tr>
<td>'.$row[0].'</td>
<td>'.$row[1].'</td>
<td>'.$row[2].'</td>
</tr>
</table>';
}
   mysqli_free_result($result);
}}

in place of your else condition code place this

Solution 2:[2]

You havent shown us the code in the first file with the retrieve button. If I understand correctly, this is what you want. You need to have a form on the first file, dont specify the action attribute

<form method="post">

and you need to have a button on it like so

<button type='submit'>

When the button is pressed, the same file is executed. At the start of the file you check if there was a post, if there was, process it and output what you want (and dont output the Retrieve button). You dont need to have a second form to show the results.

You also need this before the html header is output, otherwise you will not see the values of your editboxes.

if(!isset($_SESSION))
session_start();

I cant see anything else wrong (without running it myself). You do realise that the page will be refreshed ! If you dont want this then you have to use ajax.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Vivek Singh
Solution 2