You are using bad practice and run into errors. You cannot have <?php ?>
tags within <?php ?>
tags.
Write clean code and make good practice to a habit.
PHP is an embedded language. Do not try to generate all HTML by PHP echo. Embed peaces of PHP code into a surrounding HTML template. You can even close PHP tags within a block of loop constructs.
Variables standing alone in a PHP tag are not output, except you are using shot-open tags <?=$variable?>
. Short-open tags should not be used since most server configurations do not enable them.
Inline JavaScript is old school and considered to be bad practice. First of all inline on-handlers might be removed from the standards some day. Use event listeners instead.
You have block elements div
inside the inline element a
. This is valid only in HTML5. Ensure that your document type explicitly is HTML5 by prepending <!DOCTYPE html>
as the very first line in the document.
Links doing nothing has been widely discussed if the href
attribute should be #
or javascript:void(0);
. I tend to the latter. void
is a keyword, however, the function style is fine. Regardless to the code style there has to be an argument.
- even better: Do not use non-linking links at all. Use links with a valid fallback URL instead. In the event handler you can call the
preventDefault()
method wich will prevent the href
action, i.e. location change.
PHP site:
<head>
<title>Non-Empty Title</title>
<body>
<ul>
<?php
// fake query result
$rows =
[
[
'videos' => 'phillippines.mpeg4',
'image' => 'philippines.jpg' ,
'title' => 'Philippines' ,
'noscript-frame-page' => 'philippines.html',
],
[
'videos' => 'usa.mpeg4',
'image' => 'usa.jpg' ,
'title' => 'USA' ,
'noscript-frame-page' => 'usa.html',
]
];
// fake fetch row
foreach ($rows as $row)
{
?>
<li>
<a class="video-ref"
href="<?php echo $row['noscript-frame-page'];?>"
data-video-src="images/promvid/pal/<?php echo $row['videos'];?>"
target="video-frame"
>
<span class="vid-thumb">
<img width=72 src="images/promvid/"<?php echo $row['image'];?>
</span>
<div class="desc"><?php echo $row['title'];?>
</div>
</a>
</li>
<?php
}
?>
</ul>
<noscript>
<iframe id="video-frame" name="video-frame" src="start-video-page.html"></iframe>
</noscript>
<div id="debug">DEBUG OUTPUT</div>
<script src="my-script.js"></script>
my-script.js
document.addEventListener('DOMContentLoaded', evt =>
{
"use strict";
const VideoLinkListener = evt =>
{
const videoSrc = evt.currentTarget.getAttribute('data-video-src');
if(videoSrc)
{
document.getElementById('debug').innerHTML = videoSrc; // document.getElementById('vid_frame').src=videoSrc;
// do not change location to link's href
evt.preventDefault();
}
};
document.querySelectorAll('a.video-ref').forEach( link => link.addEventListener('click', VideoLinkListener) );
});