Home >Backend Development >PHP Tutorial >Go Beyond Basic HTML: Add Functionality and Interactivity with PHP

Go Beyond Basic HTML: Add Functionality and Interactivity with PHP

王林
王林Original
2024-10-10 14:50:21906browse

PHP is a server-side scripting language used to extend HTML, adding interactivity and dynamic functionality. Used in conjunction with HTML, PHP can process forms, query databases, and create dynamic content to build interactive and fully functional websites.

Go Beyond Basic HTML: Add Functionality and Interactivity with PHP

Beyond basic HTML: Add functionality and interactivity with PHP

Introduction

Although HTML is the basis for building websites, it lacks interactivity and advanced features. PHP (Hypertext Preprocessing Language) is a server-side scripting language that can extend your HTML to a higher level when you want to add functionality that requires dynamic updates or user interaction.

Practical Example: Creating a Login Form

Let’s create a login form that uses PHP to verify user credentials:

<!DOCTYPE html>
<html>
<head>
  <title>Login Form</title>
</head>
<body>
  <h1>Login</h1>
  <form action="login.php" method="post">
    <label for="username">Username:</label>
    <input type="text" name="username" id="username">
    <br>
    <label for="password">Password:</label>
    <input type="password" name="password" id="password">
    <br>
    <input type="submit" value="Login">
  </form>
</body>
</html>
<?php
// Connect to the database
$con = mysqli_connect("localhost", "root", "password", "db_name");

// Check if form is submitted
if (isset($_POST['username'])) {
  $username = mysqli_real_escape_string($con, $_POST['username']);
  $password = mysqli_real_escape_string($con, $_POST['password']);

  // Query the database to check if user exists
  $query = "SELECT * FROM users WHERE username='$username' AND password='$password'";
  $result = mysqli_query($con, $query);

  // If user exists, redirect to welcome page
  if (mysqli_num_rows($result) == 1) {
    header("Location: welcome.php");
  } else {
    echo "Invalid username or password";
  }
}
?>

How PHP works

PHP code is executed on the server, modifying the HTML page before sending it to the browser. It uses 5c8da1d8b4c5a9634193c26feb258147 tags to embed PHP code into HTML.

In this example:

  1. We connect to the database and get the form data.
  2. We search the database for the user credentials and if a match is found, we use the header() function to redirect to the welcome page.
  3. If no match is found, we display an error message.

Conclusion

By using PHP with HTML, you can create interactive, full-featured applications. From handling forms to creating dynamic content, PHP offers endless possibilities for going beyond static websites.

The above is the detailed content of Go Beyond Basic HTML: Add Functionality and Interactivity with PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn