-2

I am thinking to use Speedof.me api to find out the user download speed accurately.I will use the value of the download speed to determine which video quality will be used to stream video to the user.

<html>
<head>
    <script src="http://speedof.me/api/api.js" type="text/javascript"></script>
</head>
<body>
<h2>SpeedOf.Me API Consumer - Sample Page</h2>
<script type="text/javascript">
    SomApi.account = "SOM5380125e96067";   //your API Key here
    SomApi.domainName = "domain.com";      //your domain or sub-domain here 
    SomApi.config.sustainTime = 2; 
    SomApi.onTestCompleted = onTestCompleted;
    SomApi.onError = onError;
    SomApi.startTest();

     function onTestCompleted(testResult) {
       var speed = testResult.download;
       }
</script>

  <?php 
  //how can i use the speed variable here
}
?>
</body>
</html> 

I am a begineer with javascript and i would like to use the javascript variable in the php as shown above without reloading the page.I know that javascript is executed client-side and php is server-side but from what i read online is that ajax is the way to go.Also is there a way in which i can store the result of speedof.me so that i don't need to run the test every time the same user view a video

Thanks for helping me out guys

user2650277
  • 6,289
  • 17
  • 63
  • 132
  • 3
    If you already know that Ajax is a solution, then what problem do you have exactly? If you don't know how to *use* Ajax, read a tutorial first: http://learn.jquery.com/ajax/ . The fact that you have the comment "how can I use variable here" in your code indicates that you have not truly understood the difference between server and client side yet. See http://stackoverflow.com/questions/13840429/reference-what-is-the-difference-between-client-side-and-server-side-programmin – Felix Kling May 24 '14 at 16:15
  • var answers = \[[1](http://stackoverflow.com/q/20315582/607407), [2](http://stackoverflow.com/q/19462649/607407), [3](http://stackoverflow.com/q/6130662/607407), [4](http://stackoverflow.com/q/15461786/607407), [...](https://www.google.cz/search?q=Using+javascript+variable+in+PHP+with+ajax&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a&channel=sb&gfe_rd=cr&ei=DcWAU9KlHIfd_Aav8YCIDg)]; – Tomáš Zato May 24 '14 at 16:15

5 Answers5

4

you can make an ajax call to server to use the javascript variable in php

function onTestCompleted(testResult) {
   var speed = testResult.download;
   $.ajax({
    url:"link to php script" // e.g test/index.php
    type:"POST",             //method to send data
    dataType:"json",         //expected data from server
    data:{speed:speed},      //data to send server 
    success:function(data){
        alert(data);         //alert response data after ajax call success
        }
    });
   }

on php script you can use that javascript variable speed after checking $_POST[]

 echo $_POST['speed'];
A l w a y s S u n n y
  • 36,497
  • 8
  • 60
  • 103
2

passing PHP values to javascript can just be echoed. But javascript to PHP is a bit complicated.

Server scripts like PHP are executed first before Browser scripts (i.e. javascript) do their job. this means, after the page has loaded, your php won't do any good anymore, EXCEPT, you use Ajax requests.

what I use is jquery function .post() (if you're wondering why i use post, you can do your own reading about this functions including .ajax() and .get() )

PHP code somewhere found in /project/execute.php

$speed = $_POST["speed"];
echo $speed * 5;

and in your javascript...

<script type="text/javascript">
SomApi.account = "SOM5380125e96067";   //your API Key here
SomApi.domainName = "domain.com";      //your domain or sub-domain here 
SomApi.config.sustainTime = 2; 
SomApi.onTestCompleted = onTestCompleted;
SomApi.onError = onError;
SomApi.startTest();

 function onTestCompleted(testResult) {
   var speedresult = testResult.download;

   // here's the magic
   $.post("/project/execute.php", {speed:speedresult}, function(result) {
        alert(result);
   } )
 }

PS. DON'T FORGET TO IMPORT JQUERY IN YOUR SECTION OR THE BOTTOM MOST PART OF THE BODY

<script src="http://code.jquery.com/jquery-latest.min.js"></script>
Mong
  • 146
  • 4
1

You do not seem to understand the fundamental differences between Clientside and Serverside Code.

The PHP Code will be executed serverside; only the output is sent to the browser. Then the browser executes the Javascript.

One solution to your problem would be to load the video dynamically with Javascript (either per Ajax or a simple video link naming convention). To store the speed test results, use a Cookie.

René Roth
  • 1,979
  • 1
  • 18
  • 25
1

It doesn't work that way. As you said, JavaScript is client side. Your PHP page is processed by the server first--meaning, all PHP code gets executed first before any of your HTML, CSS, and JS. It doesn't matter if your JS is positioned first before PHP since PHP will get evaluated first. After that, it's sent back to the client for the browser to process HTML, CSS, and JS.

For your case, after running the speed test, send the value back to a PHP script via AJAX. You can use jQuery to make AJAX calls easier. Store a cookie using JS to indicate that the test has been executed once. You'll need to modify your JS so that it will check if this cookie is present and skip the speed test.

hyubs
  • 737
  • 6
  • 18
0

try this :-

<?php
   echo "<script>alert(speed)</script>";
?>