Quick Note
I'm on a fair beginner's level of web development HTML, CSS, javascript ran locally. For this, I'm using Visual Studio Code.
To The Problem
Recently I've gotten to the part where I need web server functions and database control. For this, I decided to go with XAMPP & MySQL Community Server. There have been no issues with setting this up locally.
So when I started delving into PHP coding, it's fun and I've already created a couple of files and also managed to make the PHP files connect to my database.
HOWEVER I can't seem to find one obvious way to actually connect my ".php" files to my actual main document which is the HTML document.
I might have misunderstood how PHP actually works, though to make an example to my question;
Just like you have your HTML document separated from a CSS document, at least in VS Code, you establish a connection link rel="stylesheet" href="index.css" for example, for my reasons at least to make the workspace much cleaner than bombarding the HTML document with CSS.
How do I do this with PHP files? Cause whenever I run my PHP files as they are, they work as intended, but I can't find a way to actually bind/connect them to my HTML document.
I'd love some input or a proper answer to this, and I will gladly respond with the necessary information.
Cheers :)!
You will use html inside the php file. So instead of having an index.html, you will have an index.php file where you place all your html in there doctype, head, etc. And then inside the html you inject php code with <?php echo 'foo'; ?>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<title></title>
<base href="">
<script src="script.js"></script>
<link rel="stylesheet" href="style.css">
</head>
<body>
<h1>Some html text</h1>
<?php echo 'This is php code'; ?>
</body>
</html>
HTML and Javascript is Client side and PHP is Server side.
That mean you have 2 possibilities:
Send data from your HTML Site to your .php
Receive Data from your .php in your HTML
For Begginers:
To send data to your .php use <form> Tag
This allows you to send Data to your .php file
Example:
HTML Site:
<form action="yourphpfile.php" action='post'>
Firstname <input type=text id=firstname name=name>
Lastname <input type=text id=lastname name=lastname>
<button type=submit> Go! </button>
</form>
Your .php file
<?php
$firstname = $_POST['firstname'];
$lastname= $_POST['lastname'];
echo("<p>Welcome " . $firstname . " " . $lastname . " </p");