How can PHP files be effectively linked to execute functions based on user input, such as form submissions?
To link PHP files to execute functions based on user input, such as form submissions, you can use the action attribute in the form tag to specify the PHP file that will process the form data. In the PHP file, you can check for the form submission using conditional statements like isset() or $_SERVER['REQUEST_METHOD'] == 'POST', and then call the appropriate functions based on the user input.
// HTML form in a file named form.html
<form action="process_form.php" method="post">
<input type="text" name="username">
<input type="submit" value="Submit">
</form>
```
```php
// PHP file named process_form.php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// Process form data
$username = $_POST['username'];
// Call functions based on user input
if ($username == 'admin') {
// Execute admin function
adminFunction();
} else {
// Execute regular user function
regularUserFunction();
}
}
function adminFunction() {
// Function for admin user
}
function regularUserFunction() {
// Function for regular user
}
Related questions
-
PHP arrays session variables
-
PHP Curl cookies
-
PHP MySQL MySQLi