I am trying to figure out how to retrieve video_link from my database after users select the video using a html form. 2 videos are uploaded into my database, the original video and a compressed video, depending on user connection. The appropriate video will be embed in html 5 video tag in viewvideo.php however because ajax is expecting a response in $( "#speed" ).val( html ); i dun think i can embed the video in viewvideo.php
So my question is what should i do now? where should i retrieve the video_link and embed the video?
This is my coding now.
I have a html form that retrieve user connection speed and video_id that user wish to watch and using ajax to post the form data
Html form
<form action="viewvideo.php" method="post" >
<br/>
Please select the video
<br/>
<select name="video_id">
<?php
while($row = mysqli_fetch_array($result))
{
?>
<option value="<?php echo $row['video_id']?>">
<?php echo $row['videoname']?>
</option>
<?php
}
?>
</select>
<br />
<input type="text" id="speed" name="speed" value="">
<input type="Submit" id="Submit" value="Submit" />
</form>
Ajax
$.ajax({
method: "POST",
url: "viewvideo.php",
data: {speedMbps: speedMbps,
video_id: $('[name="video_id"').val()},
cache: false
}).done(function( html ) {
$( "#speed" ).val( html );
});
viewvideo.php
if(isset($_POST['video_id']) && isset($_POST['speedMbps'] )){
$id = trim($_POST['video_id']);
$speed = $_POST['speedMbps'];
echo $id;
$result = mysqli_query($dbc , "SELECT `video_id`, `video_link` FROM `video480p` WHERE `video_id`='".$id."'");
$count = mysqli_num_rows($result);
if (($speed < 100) && ($count>0)) { //if user speed is less than 100 retrieve 480p quailtiy video
//does it exist?
//if($count>0){
//exists, so fetch it in an associative array
$video_480p = mysqli_fetch_assoc($result);
//this way you can use the column names to call out its values.
//If you want the link to the video to embed it;
echo $video_480p['video_link'];
}
else{
//does not exist
}
?>
<video id="video" width="640" height="480" controls autoplay>
<source src="<?php echo $video_480p['video_link']; ?>" type="video/mp4">
Your browser does not support the video tag.
</video>
<br />
<?php
$result2 = mysqli_query($dbc , "SELECT `video_id`, `video_link` FROM `viewvideo` WHERE `video_id`='".$video_id."'");
$count2 = mysqli_num_rows($result2);
// retrieve original video
if (($speed >= 100) && ($count2 >0)) {
//does it exist?
//if($count2>0){
//exists, so fetch it in an associative array
$video_arr = mysqli_fetch_assoc($result2);
//this way you can use the column names to call out its values.
//If you want the link to the video to embed it;
echo $video_arr['video_link'];
}
else{
//does not exist
}
?>
<video id="video" width="640" height="480" controls autoplay>
<source src="<?php echo $video_arr['video_link']; ?>" type="video/mp4">
Your browser does not support the video tag.
</video>
<br />
<?php
mysqli_close($dbc);
?>