所以我要在页面加载之前提交我的AJAX,并在ajax之后像这样在同一页面上调用它。

<input type="text" class="hidden" id="storeID" value="<?php echo $_GET['store']; ?>">
$(document).ready(function()
{
var store = $("#storeID").val();
$.ajax(
{
  url: '../user-style.php',
  method: 'POST',
  data: {"store":store}
});
});
<link rel="stylesheet" href="../user-style.php" media="screen" />


用户样式.php

if(isset($_POST['store']))
{
$stmtgetstyle = $mysqli->prepare("SELECT * FROM store_style_configuration WHERE store_id=?");
$stmtgetstyle->bind_param("i", $_GET['store']);
$stmtgetstyle->execute();
$getstyle = $stmtgetstyle->get_result();
$style = $getstyle->fetch_assoc();
$stmtgetstyle->close();
}


但是user-style.php不会获取任何数据,数据库中也不会有任何东西。

最佳答案

您需要提取CSS的内容并返回它。现在,您可以将其动态放入jQuery Object中:

$.ajax({
    url: '../user-style.php',
    method: 'POST',
    data: {"store":store},
    success: function(data) {
        var $cssStyles = $('<style/>');
        $cssStyles.attr('type', 'text/css');
        $cssStyles.html(data);
        $cssStyles.appendTo($('head'));
    }
});


另一种方法是使用$_GET通过URL参数将其传递到当前脚本:

<link rel="stylesheet" href="../user-style.php?store=store" media="screen" />


或使用jQuery$_GET

$(document).ready(function() {
    var store = $("#storeID").val();
    var $cssStyles = $('<style/>');
    $cssStyles.attr({ 'type': 'text/css', 'href': '../user-style.php?store=' + store });
    $cssStyles.appendTo($('head'));
});

10-05 20:44
查看更多