doushi3715 2016-02-03 02:14
浏览 855
已采纳

将javascript值传递给html

This is related to my previous question: retrieving video from database using php

On the previous question i pass the speedMbps in javascript using a ajax form and $( "#speed" ).val( html ); expect a return value so i am unable to embed my html code in viewvideo.php.

I thought of a different way to pass the speedMbps into my php.

Passing the value speedMbps into my html form which can used the method POST to sent the data to my php file.

var imageAddr = "testimage.jpg"; 
var downloadSize = 2097152; //bytes

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

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

    download.onerror = function (err, msg) {
        oProgress.value = "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);
        return speedMbps;
        oProgress.value = "Your connection speed is:        <br />" + 
           speedBps + " bps<br />"   + 
           speedKbps + " kbps<br />" + 
           speedMbps + " Mbps<br />";

document.getElementById("speed").value = speedMbps;   

Html code

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

viewvideo.php

$speed= $_POST['speed'];

Edited: when i echo $speed in my php file it does not get echo.. look like no value is pass from the javascript to my html form.

error on console is unexpected end of input on

document.getElementById("speed").value = speedMbps;
  • 写回答

2条回答 默认 最新

  • dream_high1026 2016-02-03 03:40
    关注

    i solve the problem.

    deleting return speedMbps; i manage to retrieve the value and pass the value into my php file.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘