15
I made all my site in HTML and CSS, but now I need to use PHP to send some data to a database. I’m just wondering if I modify all my documents to . php or if I use PHP inside HTML (if possible).
15
I made all my site in HTML and CSS, but now I need to use PHP to send some data to a database. I’m just wondering if I modify all my documents to . php or if I use PHP inside HTML (if possible).
16
I need to change all my files just to send some data to php and then process them and save to the database?
No! Just keep your file containing the form for example, and set the action
from it to the archive .php
, 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 your standard html (DOCTYPE, html, head, body
, etc) next to PHP to display 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 completely, already concatenating the variables, 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. So NEVER do this.
Why is it bad practice to do it that way?
@Jorgeb. for "n" reasons, I’ll list some: 1) difficult code maintenance. 2) difficult code indentation. 3) Required exhaust character sometimes. 4) makes your code/class much larger. 5) small performance loss depending on size and/or operations within/html code. But the main question is the readability of the code, so it is better to keep it in an external file and just inject/call it at the time of the answer.
14
An HTML file with extension . php will behave exactly like HTML. The PHP interpreter only enters the scene for whatever is between <? ... ?>
.
In this context the ideal is to use all . php.
by PHP standards, always use <?php ? > or <?= ? > for printing
6
When you ask:
if I modify all my documents to . php or use php within html
Actually they are not mutually exclusive options: to "use PHP within HTML" you accurate modify the document to ". php"! :-)
You can alternatively keep HTML files as they are, and do PHP separately.
Anyway, at some point, you would use PHP to read this HTML file and modify it.
In general, the frameworks PHP requests changes to the HTML file, either using a template specific (Blade, Twig, or other), or using PHP itself as the template, to integrate the data obtained in the database with the Markup.
Returning to the question from another angle...
I use Php inside an Html or Html inside a Php?
You can increase your website done in HTML and CSS by adding first Javascript.
The ideal would be to make the communication between the client side, where they inhabit HTML, CSS and JS, totally with JS - including enabling that same website use any language on the server side without being modified.
You would use a library like jQuery or Angularjs or Breeze or other to request server data via Ajax, making requests to read, create, update or remove server data (CRUD = Create, Retrieve, Update, Destroy = INSERT, SELECT, UPDATE, DELETE...)
This way, you don’t mix PHP with HTML. That is, neither PHP within HTML, nor HTML within PHP. This would be the ideal.
As much as you can do to avoid mixing PHP with HTML, do it.
On the other hand, you can see PHP as a language templates.
With a very well disciplined use, you can take advantage of the control structures that the language offers to mount your HTML from the server including variables:
<p>Seja bem vindo, <?php echo htmlentities($usuario); ?>.</p>
This use of PHP within HTML, as a language of template, is acceptable, and there are frameworks that help you to keep things in place (I am 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 inside HTML or HTML inside PHP? It’s hard to say. In this case, we’re still using PHP as "template language".
The HTML inside 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 the database, make queries, updates, checks if the user has sent data, validates forms, and in the middle of it all is dropping some Htmls... this is common, but it’s awful - it’s the code "spaghetti", which naturally graduates for beginners, but should be avoided at all costs.
After all, there’s no difference between "PHP within HTML" and "HTML inside of PHP".
In practice, when the file contains the extension ". php" causing it by default to activate the PHP language interpreter when served by an HTTP server (such as Apache or Nginx), both, HTML markup and PHP code (wrapped inside <?php
and ?>
) will be processed. HTML is unchanged, while what is inside the mentioned tags is executed (eventually repeating HTML, as we saw).
4
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 process with PHP only files with extension . php
Got it, but in case I turn my . html files into . php, they will work normally ?
Yes. PHP interpreter only processes code within tags <?php ... ?>
, the rest is passed without change. Normally HTML files can be renamed to . php without any unexpected effect.
True enough, Havenard, true enough. But you also need to remember to change any link that points to a "blabla.html" address to "blabla.php" // your editor program should be able to convert the links automatically for you.
Of course. Naturally the files the links point to need to exist. Changing the extension implies that the file name has been changed, and the links pointing to it need to be fixed.
Links can also be maintained as is by configuring apache to rewrite urls.
But when I finish my site and publish, some of these changes, both modify to . php, when configuring apache, will have some influence on how it works ?
In the case of apache, you set up the rewriting of urls in an htaccess file at the root of the site. This file needs to be published along with the others.
Valeu gnt, brigado..
4
Anything static, keep static. For example, an HTML page where there is no need to use a PHP database or resources, there is no reason to use PHP.
Simple as that... unless you have some very specific reason for parsing a certain static content within the PHP compiler.
3
I imagine that’s 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 seek to write a PHP file with all the necessary functions - preferably, starting with <?php
and don’t even close the tag - which will be invoked by some parts of your HTML.
Returning to your question, the most common configuration is that your HTML includes some PHP tags and has extension .php. You can also make HTML just a template, which is loaded by PHP code and modified in dynamic parts.
Browser other questions tagged php html
You are not signed in. Login or sign up in order to post.
My opinion? Do both separate and communicate between them via Ajax.
– André Leria
I recommend not using php in your html, make PHP work only in the back-end layer, bringing everything by Restful API..., via javascript. Will suffer less.
– Ivan Ferrer
[+] just need to know how to work with javascript objects and JSON interpretation, which is very similar to XML
– Ivan Ferrer