What are the best practices for handling form actions and button placement in PHP scripts to ensure security and efficiency?

When handling form actions and button placement in PHP scripts, it is important to validate and sanitize user input to prevent SQL injection and cross-site scripting attacks. Additionally, use prepared statements when interacting with a database to prevent SQL injection. Place buttons and form actions in a secure location to prevent unauthorized access and ensure efficient processing.

// Example of handling form actions and button placement in PHP script

// Validate and sanitize user input
$username = filter_var($_POST['username'], FILTER_SANITIZE_STRING);
$password = filter_var($_POST['password'], FILTER_SANITIZE_STRING);

// Use prepared statements to interact with the database
$stmt = $pdo->prepare("SELECT * FROM users WHERE username = :username AND password = :password");
$stmt->bindParam(':username', $username);
$stmt->bindParam(':password', $password);
$stmt->execute();

// Place buttons and form actions in a secure location
<form action="process_form.php" method="post">
  <input type="text" name="username">
  <input type="password" name="password">
  <button type="submit">Submit</button>
</form>