php - 在 Ajax 中调用 PHP 变量

标签 php jquery css ajax

我试着做进度条,css代码是这样的:

#result{
    background:#8c8f91;
margin-left: 15px;
margin-right: auto;
table-layout: fixed;
border-collapse: collapse;
z-index: -1; position:relative;
width: 0%;
}

我想用 Ajax 更新我的进度条:

       $(function worker(){
    // don't cache ajax or content won't be fresh
    $.ajaxSetup ({
        cache: false,
        complete: function() {
          // Schedule the next request when the current one's complete
          setTimeout(worker, 4500);
        }});
   $("#result").css("width"," <?php echo json_encode($percent); ?>");
   $("#result").load("http://localhost/test6/select-oki.php #result").fadeIn();
}
// end  
});

我想用“$percent”更新“width”值,但它没有从中获取值。我认为问题在于:

$("#result").css("width"," <?php echo json_encode($percent); ?>");

最佳答案

关于原始代码的一些事情。 $percent 未在 js 中定义,至少您显示的代码是这样。您甚至在尝试加载之前就请求 json_encode。我没有测试过这个,只是我注意到的一些东西。

考虑这种方法

$(function worker(){
    $.ajax({
        cache: false,
        url:'http://localhost/test6/select-oki.php',
        success: function(data) {
            var response = JSON.parse(data);
            $("#result").css("width",response['percent']).fadeIn();
        },
        error: function(xhr, ajaxOptions, thrownError) {
            console.log('error!');
        }
    });
});

从 PHP 回显你的数据 echo json_encode(['percent'=>$percent]);

关于php - 在 Ajax 中调用 PHP 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47482404/

相关文章:

php - Mysql查询从csv文件插入记录到mysql Db

php - 如何从 Bootstrap 输入标签中获取值,然后插入到 mysql 数据库中

javascript - 下拉菜单在 ie7 中失败

html - 如何垂直固定导航箭头?

php - 在 MySQL 分页结果中查找特定记录

php - 乔姆拉!在模板 index.php 中取消设置 CSS

php - MySQL 从表 WHERE 值> 0 中选择行

javascript - 使用 symfony 的 .post 函数中的 PHP 文件

javascript - 在没有 eventData 的情况下通过 jQuery 事件调用传递匿名函数

html - 设置输入文本框大小的替代方法?