What is the best practice for calling a PHP file from an HTML form submit and returning to the original HTML file with variables intact?
When calling a PHP file from an HTML form submit and returning to the original HTML file with variables intact, you can use the POST method to send form data to the PHP file. In the PHP file, process the form data and store any necessary variables. Then, redirect back to the original HTML file with the variables passed as URL parameters.
// HTML form
<form action="process.php" method="post">
<input type="text" name="username">
<input type="submit" value="Submit">
</form>
// process.php
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$username = $_POST['username'];
// Process the form data
// Redirect back to the original HTML file with variables intact
header("Location: original.html?username=" . urlencode($username));
exit();
}
?>