2

My problem is not easy to understand, so I start from beginning: Let´s say you have two computers. You click on a link on one computer and on the other computer starts a video, which matches to the link.

My idea was to make two files, one with the link and one which opens the video (e.g. link.php and video.php) The link.php should write a variable (given from the link) to a file, which is read by the video.php and shown on the other device.

But it shouldn´t refresh all the time, so AJAX has to be taken. And there I have two problems, where I can´t find a solution.

First: How do I transfer a variable between PHP and Javascript? This is my actual (not working) solution:

Link (inside PHP-Code):

<a href=\"#\" onclick=\"writeVideo(".$Vorname->item(0)->childNodes->item(0)->nodeValue . " " . $Nachname->item(0)->childNodes->item(0)->nodeValue . ");\">

Function:

<script>
function writeVideo(video) {
    videoUrl = "video-write.php?video=".video;
    $.ajax({url: videourl });
));
</script> 

The PHP, which writes the file looks like this: It´s working but, not reached from the Javascript.

<html>
<head>
<title>Videoseite</title>
  <style type="text/css">
    body{background-color: #000; }
  </style>
</head>

<body>
<?php
$video = $_GET["video"];
$zeile = "<video width=\"100%\" height=\"100%\" autoplay onended=\"videoendedFunction()\"><source src=\"resources/video/" . $video . ".mp4\" type=\"video/mp4\"></video>";
file_put_contents("video-file.txt", $zeile);
?>
</body>

</html>

Second problem:

Then we have the other side, where the video is loaded. The code below works, but only on click on the button. I thought it would be the best if the script is checking the file-in-the-middle all the time, and uses the content of the file to show the video. My problem is, that I don´t know how to get the function running infinitely, and react to changes - from my understanding this would end in a loop.

<html>
<head>
<title>Videoseite</title>
  <style type="text/css">
    body{background-color: #000; }
  </style>
  <script src="foundation-6.4.2/js/vendor/jquery.js"></script>
</head>

<body>
<div id="video-ausgabe"></div>
<p>
  <button onclick="ausgabe()">Hier klicken</button>
</p>
<script>
 function ausgabe() {
  $.get('video-file.txt', function(data) {
   $('#video-ausgabe').html(data); 
  })
 }
</script>
</body>

</html>

I hope you understand my project and my problem(s). I´m not a professional programmer, so if you know a better way to get a variable from one computer to the other, let me know.

Thanks a lot!

brandy
  • 31
  • 1
  • 3
  • 2
    what you seem to want to achieve are websocket connections -> take a look at socket.io – wodka Jan 22 '19 at 11:44
  • Possible duplicate of [Edit PHP variable with JavaScript/jQuery](https://stackoverflow.com/questions/54079448/edit-php-variable-with-javascript-jquery) – AndrewL64 Jan 22 '19 at 11:44
  • @brandy what you mean by two computers? Are these files link.php and video.php hosted somewhere on the server? – CodeThing Jan 22 '19 at 11:57
  • `Send data with Request` or `Use redis/memcache to store and share data` or `Use websockets` – BadPiggie Jan 22 '19 at 11:59
  • Yes, the files are stored on a server in the same directory. – brandy Jan 22 '19 at 20:27
  • Are websockets the solution for both problems? I mean, does the transfer happen inbetween the websockets or is it just possible to transfer without interaction from the client? – brandy Jan 22 '19 at 20:39

0 Answers0