I made my entire site in HTML and CSS, but now I need to use PHP to send some data to a database. But I have a question, if I modify all my documents to .php or if I use PHP within HTML (if it is possible).
I made my entire site in HTML and CSS, but now I need to use PHP to send some data to a database. But I have a question, if I modify all my documents to .php or if I use PHP within HTML (if it is possible).
An HTML file with .php extension will behave just like HTML . The PHP interpreter only enters the scene for whatever is between <? ... ?>
.
In this context the ideal is to use everything .php.
I need to change all my files just to send some data to the php and then process them and save to the database?
No! Just keep your file containing the form for example, and set the action
of it to the .php
file, for example:
seu_formulario.html
<form action="processa_dados.php" method="post">
<!-- seus campos e o botão submit aqui -->
</form>
Ready. With this you can already do in a simple way what you want.
processa_dados.php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// valida e processa os dados
// redireciona para uma página de sucesso ou erro
}
It is normal, and it is not bad practice to create a file for example index.php
, with all its default html ( DOCTYPE, html, head, body
, etc) next to PHP to show variables, use conditions ( if, else, foreach, while
). As in the example below (CakePHP):
<!doctype html>
<html lang="en">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>
<?php
if(isset($titulo)) {
echo $titulo . ' | ';
}
?>
StackOverflow
</title>
</head>
<body>
<?php include('cabecalho.php') ?>
<?php echo $conteudo ?>
<?php include('rodape.php') ?>
</body>
</html>
What is bad practice, on the contrary, is to create a class for example, which returns the HTML of your page altogether, concatenating variables already, etc.
<?php
class Exemplo
{
public function resposta()
{
echo '<div id="answer-7706" class="answer" data-answerid="7706">
<table>
<tbody>
<tr>
<td class="votecell">
<div class="vote">
<input type="hidden" name="_id_" value="7706">
<a class="vote-up-off" title="Esta resposta é útil">votar a favor</a>
<span class="vote-count-post ">3</span>
<a class="vote-down-off" title="Esta resposta não é útil">votar contra</a>
</div>
</td>
<td class="answercell">
<div class="post-text">
<p>Um arquivo HTML com extensão .php vai se comportar <strong>exatamente como HTML</strong>. O interpretador PHP só entra em cena para o que estiver entre <code><? ... ?></code>. </p>
<p>Neste contexto o ideal é usar tudo .php.</p>
</div>
<table class="fw">
</table>
</td>
</tr>
</tbody>
</table>
</div>';
}
}
Yes, believe me ... there are programmers who do this. Then NEVER do this.
When you ask:
If I change all my documents to .php or if I use php inside the html
In fact they are not mutually exclusive options: to "use PHP within HTML" you need to modify the document to ".php"! : -)
You can, alternatively, keep the HTML files as they are, and make PHP separate.
Also, at some point, you would use PHP to read this HTML file and modify it.
In general, PHP framework requests changes to the HTML file, either using a specific template language (Blade, Twig, or another), or using PHP itself as a template language, to integrate the data obtained in the database with markup .
Resuming the issue from another angle ...
Do I use Php within an Html or an Html within a Php?
You can increase your site in HTML and CSS by first adding JavaScript .
The ideal would be to communicate between the client side, where HTML, CSS and JS reside, totally with JS - including enabling that same site to use any language on the server side, without being modified.
You would use a library like jQuery or AngularJS or Breeze or another to request the server data via Ajax, making requests to read, create, update or remove data from the server (CRUD = Create, Retrieve, Update, Destroy = INSERT , SELECT, UPDATE, DELETE ...)
That way, you do not mix PHP with HTML. That is, neither PHP within HTML, nor HTML within PHP. This would be the ideal .
The most you can do to avoid merging PHP with HTML, do.
On the other hand, you can see PHP as a templates language.
With very well-disciplined usage, you can take advantage of the control frameworks that the language offers to assemble your HTML from the server including variables:
<p>Seja bem vindo, <?php echo htmlentities($usuario); ?>.</p>
This use of PHP within HTML as a template language is acceptable, and there are frameworks that help you a lot to keep things in their place I'm a fan of Laravel 4 ).
Another example:
<?php foreach ($itens as $item): ?>
<div>Item <?php echo htmlentities($item->nome); ?></div>
<?php endforeach; ?>
Is this PHP within HTML or HTML within PHP? It's hard to say. In the case, we're still using PHP as the "template language".
The HTML within PHP itself would look like this:
<?php
foreach ($itens as $item) {
echo '<div>Item ' . htmlentities($item->nome) . '</div>';
}
?>
This kind of thing should be avoided.
When your code connects to the database, it queries updates , verifies that the user has sent data, validates forms, and in the middle of it all is releasing some HTMLs ... this is common , but it's ghastly - it's the "spaghetti" code, which of course will form for beginners, but should be avoided at all costs.
At the end of the day, there is no difference between "PHP within HTML" and "HTML within PHP".
In practice, when the file contains the ".php" extension, by default, it activates the PHP interpreter when it is served by an HTTP server (such as Apache or NginX), both , HTML markup and PHP code (wrapped within <?php
and ?>
) will be processed. The HTML is unchanged, whereas what is inside the tags mentioned is executed (possibly repeating the HTML, as we have seen).
Whatever is static, keep it static. Example, an HTML page where there is no need to use database or PHP features, there is no reason to use PHP.
Simple like this ... unless you have some very specific reason to parse a certain static content inside the PHP compiler.
To enable php within html, you would have to configure your server (Apache, IIS etc.) to process html files using PHP. This is not the most common configuration.
The normal thing is to only process files with .php extension with PHP
I imagine this is not exactly your question, but the ideal is to keep the mix between HTML and PHP to a minimum. This makes it easy to change things on both sides, so always try to write a PHP file with all the necessary functions - preferably starting with <?php
or closing the tag - that will be invoked by some parts of your HTML. p>
Returning to your question, the most common configuration is that your HTML includes some PHP tags and has a .php extension. You can also make HTML just a template, which is loaded by PHP code and modified in dynamic parts.