Jump to content

Recommended Posts

  • Moderators

A web application ends up being encoded in html, js and css.
php is a language which can be used to create these components, but more importantly can also be used within all 3 of these types of files.
That gives some interesting possibilities.

To make that happen, php needs to be activated serverside. Many server hosting environments come with php pre-installed.
Activating php so that it can be used at the time of serving regular .html, .js or .css files by the browser depends on the type of server.
Regular unix based servers make use of a .htaccess file which needs a line like

AddHandler application/x-httpd-php5 .html .htm

Windows servers have another mechanism

1 - Using php within Smart

Php can be invoked like this, where the php statements are inclosed in a <?php block.

    var i = 0;
    i = <?php echo 35; ?>;

A practical use would be to include server side scripts normally accessed through a POST request (which eliminates a server round trip as well).
The below code reads a MySqL database at form init

procedure TForm1.InitializeForm;
  // this is a good place to initialize components

var response : variant := new JObject;

@response =
$link = mysql_pconnect("...domain...", "...user...", "...password...") or die("Could not connect");
mysql_select_db("...database...") or die("Could not select database");

$sql_statement = 'Select * from FishFacts';

$arr = array();
$rs = mysql_query($sql_statement);
while($obj = mysql_fetch_object($rs)) {
$arr[] = $obj;
echo '{"rows":'.json_encode($arr).'}';


writeln(response.rows[0].Common_Name);   //Clown Triggerfish

2 - Using php within Html files
A (possibly) usefull example scenario would be to separate web-pages into a header.html, a regular Smart index.html and a footer file.
php has an include mechanism, which can be used like so

<?php include "header.html" ?>

  <script type="text/javascript">    /* This prevents the window being moved by touches,
      to give the impression of a native app */
    document.ontouchmove = function(e) { e.preventDefault(); }

    <script type="text/javascript" src="main.js.php"></script>


3 - Using php within css files
Similarly stylesheets can be scripted, something like this snippet where, within the stylesheet, the current date is checked which sets an image url

 $month = date('m');
 $day = date('d');
 if($month=='12' && $day=='25') {
    $logoSrc = 'images/holidayLogo.png';
 } else {
    $logoSrc = 'images/logo.png';
h1 {
 background: url(<?=$logoSrc?>) no-repeat;

Demo (of 1 and 2)


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...