Can I make a form in PHP?
It depends what you mean by that.
You need to use HTML to create the actual form, if you want the form to display in a browser. PHP is a server-side language and can’t be processed by the browser.
On the other hand, it’s quite usual to use PHP to process the form once it’s submitted.
A form will be html, but there is no reason why you could not use php to create that html if it fits your needs to do so. For example a form’s fields could be stored in an array or a database, then called and rendered to html via php.
I have not yet done any forms this way, but have an upcoming project which will involve a lot of forms, so I am thinking of doing something along those lines to automate the process, by using stored data to both render the form fields and process them after submission.
I can understand how that could be confusing.
That is HTML code with PHP in it so it is a “.php” file so that the PHP code will be processed before the resulting HTML will be output.
In other words the form is not a PHP form, it is an HTML form that PHP code is putting together and sending to the browser.
That’s fairly cool!
You could output all the HTML using echo statements but I can see no sense in that. It is more usual - and easier to follow - using a mix of HTML and PHP as per the tutorial.
Thanks a lot! Besides my reason was to prevent the code from been viewed from the view source
As for how to mix HTML and PHP, it can be done different ways. eg.
</php
$title = "something";
$heading = "goes here";
$heading2 = "and here too";
$private = "my password";
?>
<title><?php echo $title; ?></title>
<?php
echo "<h1>$heading</h1>";
echo '<h2>' . $heading2 . '</h2>';
?>
and there are other ways too.
As for
PHP code will not be visible in view-source, but HTML will be, no getting around that, the browser needs it. eg. “my password” will not be visible in view-source (well, unless the PHP engine is down and the file gets output as text)
Typically PHP
engine shouldn’t go down unless you are messing with the PHP
folder on your environment. But it’s actually best to separate PHP
and HTML
and avoid spaghetti code
. But you can’t avoid it for long as spaghetti code
will eventually turn up. Just that you should use minimum of it. And I am talking about nesting PHP
in HTML
and vice verse.
Yes, things can get real messy, real fast.
For something like a form I would probably use HEREDOC syntax.
<?php
$title = "something";
$heading = "goes here";
$heading2 = "and here too";
$private = "my password";
$output = <<<MYHTML
<title>$title</title>
<h1>$heading</h1>
<h2>$heading2</h2>
MYHTML;
echo $output;
?>
Templates would be a good option too. eg.
<title>[[title]]</title>
<h1>[[heading]]</h1>
<h2>[[heading2]]</h2>
PHP itself is like a template engine, you can easily include your templates
index.php
<?php
$mynameis = 'Earl';
require('html/index.php');
html/index.php
<html>
<title>This website is from: <?=$mynameis?></title>
</html>
not so comfortable, but works without overhead, and separates your tasks.
This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.