What is the recommended method to pass data from an HTML form to a PHP script and display the result back on the same HTML page?
To pass data from an HTML form to a PHP script and display the result back on the same HTML page, you can use the POST method in the form submission and then process the form data in the PHP script. Once the PHP script processes the data, you can echo the result back to the HTML page using PHP.
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$data = $_POST['data']; // Assuming 'data' is the name of the input field in the form
// Process the data as needed
$result = "Processed data: " . $data;
// Echo the result back to the HTML page
echo "<p>$result</p>";
}
?>
<!-- HTML form -->
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="text" name="data">
<button type="submit">Submit</button>
</form>
Related Questions
- How can LAST_INSERT_ID() be used to retrieve the auto-increment value for insertion into another table in PHP?
- How does the choice of delimiter in CSV files impact the way Excel and Open Office interpret the data, and what are the implications for data integrity?
- What are the potential pitfalls to consider when using PHP scripts with buttons for web development projects?