This example will use the HTML's form element for receiving user's inputs and sending that data to PHP.
Create a new php file. I will name the file addUser.php. Try to name the file in a way that describes its function (This PHP file will have one purpose, which is to add a new User in the DB). We'll leave the file empty for now and come back to it later.
Create a index.html in the same folder with the PHP file. Add a form and any inputs you'd want to send to DB. Example below includes inputs for name and email, matching the previously created DB Table.
Bullet points below the code will highlight the most important parts of it.
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Page title</title>
</head>
<body>
<form action="./addUser.php" method="post">
<label for="inputFullName"></label>
<input type="text" id="inputFullName" name="fullname">
<label for="inputEmail"></label>
<input type="text" id="inputEmail" name="email">
<button type="submit">Submit</button>
</form>
</body>
</html>
- Form's action-value is a path pointing to the PHP file, that will receive the inputs of this form.
- Form's method-value is set to 'post' (HTTP POST method), fit for sending data.
- The input elements of the Form each has a name. The inputs need names, so they can be identified in the php.
- Lastly, the button with submit-type, once clicked will send the form inputs to the path given in action
- For more consideration: the field validation should be handled here in the front end, before the form can be submitted to the backend. For simple validation, you can check out pattern and required.