How can PHP be used to handle form data and search functionality on a single page?

To handle form data and search functionality on a single page using PHP, you can use conditional statements to determine if the form has been submitted or if a search query has been entered. Based on the condition, you can process the form data or perform a search query. You can then display the results on the same page.

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Form data has been submitted
    $name = $_POST["name"];
    $email = $_POST["email"];
    
    // Process the form data
    // Add your processing logic here
} elseif (isset($_GET["search"])) {
    // Search query has been entered
    $searchTerm = $_GET["search"];
    
    // Perform search functionality
    // Add your search logic here
}

// Display the form and search functionality on the page
// Add your HTML form and search input here
?>