Index  •   DF Generator  •   MQ Generator  •    

It is currently Mon Feb 19, 2018 6:59 am
LOG IN TO SEE THE REST OF THE FORUMS






Post new topic Reply to topic  [ 2 posts ] 
 An easy method of integrating PHP into HTML and CSS 
Author Message
 Post subject: An easy method of integrating PHP into HTML and CSS
PostPosted: Thu Dec 21, 2017 21:48 pm 
User avatar

Joined: Thu Aug 25, 2016 11:02 am
Posts: 20
Reply with quote
Introduction : Requirements:
  • A webserver running PHP 7.0 or newer (I'll be using my webserver package for this tutorial which you can find on this site for Windows users).
  • Some experience with HTML and CSS (although this tutorial will cover some of that if you needed a refresher).
HTML : Brief:
Here we'll be making a simple HTML page that will grab user input and print it back out.

Code:
<?php

echo <<<HTML
<form method="post">
    <input type="text" name="userdata" /><br />
    <input type="submit" name="formdata" value="Submit" />
</form>
HTML;


if(filter_has_var(INPUT_POST, 'formdata')) {
    $input = filter_input(INPUT_POST, 'userdata');
    echo htmlspecialchars($input);
}


What happens here is that we will print out the HTML above the PHP code and then echo out the user input when the form gets submitted.

The main thing to catch is the opening and ending tags for this method:
Opening:
Code:
<<<HTML

Closing:
Code:
HTML;


Don't forget to echo this or put it in a variable otherwise the HTML data won't show up on the site.
CSS : Brief:
Here we'll be making a simple CSS page and integrating it with another page using the same method as you would any other CSS file.

Code:
<?php

$colorscheme = [
    'Default' => [
        'Background' => '#000000',
        'Foreground' => '#CCCCCC'
    ],
    'Header' => [
        'Background' => '#000000',
        'Foreground' => '#FF3300'
    ]
];

$css = <<<CSS
html,body {
    background-color: {$colorscheme['Default']['Background']};
    color: {$colorscheme['Default']['Foreground']};
   
    max-width: 100vw;
    max-height: 100vh;
}

#header {
    display: block;
    margin: 0 auto;
   
    font-family: "Trebuchet MS", sans-serif;
    font-weight: 900;
   
    color: {$colorscheme['Header']['Foreground']};
}
CSS;

echo $css;
header('Content-type: text/css');


The main bits to take away from this are:
Opening:
Code:
<<<CSS

Closing:
Code:
CSS;

Header:
Code:
header('Content-type: text/css');


Now we will integrate it with a page:
Code:
<link rel="stylesheet" href="style.php" />

Why use this? : Because it's easier to integrate with PHP and is more secure than using PHP's eval function to handle this.

Plus it makes the task of implementing HTML/CSS templates more easier with PHP and can be carried over into other languages (not just HTML and CSS).



Offline
 Profile  
 
 Post subject: Re: An easy method of integrating PHP into HTML and CSS
PostPosted: Sat Dec 30, 2017 18:17 pm 

Joined: Sun Mar 01, 2015 0:30 am
Posts: 84
Reply with quote
this is the dumbest idea i have ever witnessed... PHP templating has existed for the longest time ever yet you show this to people... you should be publicly executed for sharing this...


Offline
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 




 Who is online 

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for: