0

i am trying to retrieve this data speedMbps from my JavaScript. using Ajax to post the data to my php code but i am not getting any output. I am new to ajax and only used ajax for auto-completion.

<script src="//code.jquery.com/jquery-1.10.2.js"></script>          

<script>
//JUST AN EXAMPLE, PLEASE USE YOUR OWN PICTURE!
var imageAddr = "testimage.jpg"; 
var downloadSize = 2097152; //bytes

window.onload = function() {
    var oProgress = document.getElementById("progress");
    oProgress.innerHTML = "Loading the image, please wait...";
    window.setTimeout(MeasureConnectionSpeed, 1);
};

function MeasureConnectionSpeed() {
    var oProgress = document.getElementById("progress");
    var startTime, endTime;
    var download = new Image();
    download.onload = function () {
        endTime = (new Date()).getTime();
        showResults();
    }

    download.onerror = function (err, msg) {
        oProgress.innerHTML = "Invalid image, or error downloading";
    }

    startTime = (new Date()).getTime();
    var cacheBuster = "?nnn=" + startTime;
    download.src = imageAddr + cacheBuster;

    function showResults() {
        var duration = (endTime - startTime) / 1000;
        var bitsLoaded = downloadSize * 8;
        var speedBps = (bitsLoaded / duration).toFixed(2);
        var speedKbps = (speedBps / 1024).toFixed(2);
        var speedMbps = (speedKbps / 1024).toFixed(2);
        oProgress.innerHTML = "Your connection speed is: <br />" + 
           speedBps + " bps<br />"   + 
           speedKbps + " kbps<br />" + 
           speedMbps + " Mbps<br />";

        $.ajax({
          method: "POST",
          url: "test_select.php",
          data: {speedMbps: speedMbps },
          cache: false
        }).done(function( html ) {
            $( "#speed" ).val( html );
        });
    }
}

</script>

<input type="text" id="speed" name="speed" value="">

test_select.php

<?php
    echo $_POST['speedMbps'];

    if(isset($_POST['speedMbps'])){
        echo $speedMbps = $_POST['speedMbps'];
    }

?>
5
  • Try setting method: "POST" in your AJAX call Commented Feb 2, 2016 at 1:24
  • Your speedMbps variable is not in scope for your AJAX call. Commented Feb 2, 2016 at 1:24
  • Possible duplicate of Data transfer from JavaScript to PHP Commented Feb 2, 2016 at 1:24
  • where is speedMbps declared in your ajax code? Commented Feb 2, 2016 at 1:27
  • I just tried your updated code and it works on my end. Are you still having the same problem? if so, have you checked your browser's console? any errors show up? Commented Feb 2, 2016 at 12:11

2 Answers 2

0
$.ajax({
      type: "POST",
      url: "test_select.php",
      data: {speedMbps: speedMbps },
      cache: false
    }).done(function( html ) {
        $( "#results" ).val( html );
    });

change method:"POST" to type:"POST".

Sign up to request clarification or add additional context in comments.

2 Comments

try add option dataType:"text" or just echo 1; and check output
method or type does the same thing.
-1

You'll probably want to merge the top an bottom script and place it after your jquery hook because the variables.

Also you are missing method: "POST" in your Ajax script from what I can tell. This is needed to specify the type of HTTP request, since your using $_POST in your .php file, the request types need to match.

$.ajax({
  method: "POST",
  url: "test_select.php",
  data: {speedMbps: speedMbps },
  cache: false
}).done(function( html ) {
    $( "#results" ).val( html );
});

Here is jQuery's docs on ajax: ajax docs

1 Comment

i merge my script but still not getting any output.. please help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.