/    Sign up×
Community /Pin to ProfileBookmark

Trying to dynamically create a video tag using PHP.

Via AJAX/ or a Javascript framework like Jquery of course. However my question isn’t about the .js but rather the <?php> .

I wanted to make a server call via AJAX to php and have it make a video take with the correct attributes and stuff and shove it into a div that was already pre made. However when I ran it nothing happened. I checked to see if they were any syntax errors but as far as I can tell there aren’t any. So I’m going to post the code here and see of anyone can figure out if I did something wrong before I find it the error myself. Any help would be most appreciated.

[CODE]<?php

$n;
$dom = new DOMDocument();
$id = $dom->getElementById(‘videos’);

/* Loop through the directory and get vidoes*/
function videosLoad(){
$n = sizeof(readdir(‘/videos’));
if ($handle == opendir(‘/videos’)) {

/* This is the correct way to loop over the directory. */
while (false !== ($entry = readdir($handle))) {
/*Video tag*/
$video = $dom->createElement(‘video’);
$video->createAttribute(‘id’)->value = ‘example_video_’.$n;
$video->createAttribute(‘class’)->value = ‘video-js vjs-default-skin’;
$video->createAttribute(‘controls preload’)->value = ‘auto’;
$video->createAttribute(‘width’)->value=’640′;
$video->createAttribute(‘height’)->value=’264′;
$video->createAttribute(‘data-setup’)->value = “{ ‘controls’: true, ‘autoplay’:false, ‘prelaod’: ‘auto’}”;
/*source tags*/
$source = $dom->createElement(‘source’);
$source->createAttribute(‘src’)->value= $entry;
$source->createAttribute(‘type’)->value= ‘video/mp4’;
/*script tags:: May not need it
$script = $dom->createElement(‘script’);
$script->createAtrribute(‘type’);
$script->value = “text/javascript”;
$script->appendChild(“var vObj =
document.getElementById(“example_video_”‘.$n’);
document.getElementById(“vidoes”).appenedChild(vObj);”);
*/
$video->appendChild($source);
$id->appendChild($video);

/*
‘<video id=”example_video_” class=”video-js vjs-default-skin”
controls preload=”auto” width=”640″ height=”264″
data-setup='{“example_option”:true}’>
<source src=”‘$entry'” type=’video/mp4’/>
</video>’;
“<script type=”text/javascript”>
var vObj = document.getElementById(“example_video_”‘.$n’);
document.getElementById(“vidoes”).appenedChild(vObj);
</script>”
*/
$n–;
}

/* This is the WRONG way to loop over the directory.
while ($entry = readdir($handle)) {
echo “$entryn”;
}
*/
closedir($handle);
}
}
/*Use Video.js as a the containter for the video*/

?>[/CODE]

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@jamesm6162Mar 04.2012 — Well the first problem I notice is these first two lines.

[CODE]
$dom = new DOMDocument();
$id = $dom->getElementById('videos');
[/CODE]


You are creating a new DOMDocument, so it won't have any elements yet.

Retrieving an element with id 'videos' won't return anything. Furthermore, the getElementById method won't work on a DOMDocument unless you set which attribute is actually the "id" attribute. See http://www.php.net/manual/en/domdocument.getelementbyid.php

Secondly, all your video logic is in a function that isn't ever called...

So yes nothing will happen.

Thirdly the videosLoad function will not have access to the $id and $dom objects because they were declared outside the function and need to be globals to be visible inside the function.

Then lastly I don't see that you do anything with your $dom object that will save it / output it. You need to do something with the object, such as echo the content back to the browser. The javascript side should then place the content in the appropriate div.
Copy linkTweet thisAlerts:
@hastxMar 04.2012 — If all you are trying to do is create a video tag...with attributes that you are statically entering...I think you are over compicating it. The only dynamic attribute you have is the src.

The ajax part of the question isn't really relevant to the video tag part of the question since ajax is just going to populate the div with whatever response it gets....So I think the div you want to popluate belongs on the source page, not in the php script

I would just try using a template and replace the dynamic values...make sure the parsed video source is correct before moving on to the ajax part, and a template might be easier to work with than the domdocument class if you are unfamiliar with it. The template code could reside in a file by itself (and there are many free scripts available too) but just for demo:

[code=php]
$vid_src = //...however you aquire the source path...
$vid_templ = "
<video width="640" height="264" controls="controls">
<source src="[moviesrc]" type="video/mp4" />
Your browser does not support the video tag.
</video>
";
$vid = str_replace('[moviesrc]',$vid_src,$vid_templ);

echo $vid;
[/code]
×

Success!

Help @Lawrence_Fields spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.7,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...