Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

This question already has an answer here:

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

share|improve this question
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: 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 stackoverflow.com/questions/13840429/… –  Felix Kling 17 hours ago
    
var answers = [1, 2, 3, 4, ...]; –  Tomáš Zato 17 hours ago
add comment

marked as duplicate by Tomáš Zato, Joe, Mark Amery, Samuel Edwin Ward, Donal Fellows 17 hours ago

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

5 Answers

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'];
share|improve this answer
add comment

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>
share|improve this answer
add comment

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.

share|improve this answer
add comment

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.

share|improve this answer
add comment

try this :-

<?php
   echo "<script>alert(speed)</script>";
?>
share|improve this answer
    
it works for me. –  Vishal Varshney 17 hours ago
1  
This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post - you can always comment on your own posts, and once you have sufficient reputation you will be able to comment on any post. –  secretformula 17 hours ago
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.