简体   繁体   English

在链接点击时运行PHP脚本而不刷新页面

[英]Run PHP script on link click without page refresh

Ok, so I've done a style switcher in php, and I'm using it as a hidden feature on my site, which is triggered when user types "teema", and a modal box will appear on the top of the page content and displaying the available themes. 好的,所以我在php中做了一个样式切换器,我将它用作我网站上的隐藏功能,当用户键入“teema”时会触发该功能,并且页面顶部会出现一个模态框并显示可用的主题。

<ul>
    <li><a href="teema.php?teema=default" class="ajaxLink">Normaali</a></li>
    <li><a href="teema.php?teema=superhero" class="ajaxLink">Superhero</a></li>
    <li><a href="teema.php?teema=spacelab" class="ajaxLink">Spacelab</a></li>
    <li><a href="teema.php?teema=united" class="ajaxLink">United</a></li>
    <li><a href="teema.php?teema=cyborg" class="ajaxLink">Cyborg</a></li>
</ul>

However, I want to change the theme without the page refreshing so the css should load on background. 但是,我想在没有页面刷新的情况下更改主题,因此css应该加载到背景上。 Here's the teema.php: 这是teema.php:

<?php
    $style = $_GET['teema'];
    setcookie("teema", $style, time()+604800);
    if(isset($_GET['js'])) {
        echo $style; 
    } 
    else{
    header("Location: ".$_SERVER['HTTP_REFERER']);  
    }
?>

And the php on the index that checks the cookie: 和检查cookie的索引上的php:

<?php 
if(!empty($_COOKIE['teema'])) $style = $_COOKIE['teema'];
else $style = 'default';
?>
<link rel="stylesheet" href="css/<? echo $style;?>.css" id="theme">

How could I load the new css on the background, and fade it over to the old css using jQuery? 我怎么能在后台加载新的CSS,并使用jQuery将其淡化为旧的CSS?

You can give a response of JavaScript content type in the PHP file and manipulate the stylesheet. 您可以在PHP文件中提供JavaScript内容类型的响应并操作样式表。

Change the code this way: 以这种方式更改代码:

<?php
    $style = $_GET['teema'];
    setcookie("teema", $style, time()+604800);
    if(isset($_GET['js'])) {
        // echo $style; 
    } 
    else{
     // header("Location: ".$_SERVER['HTTP_REFERER']);  
    }
    header("Content-type: text/javascript");
?>
document.getElementById("theme").setAttribute("href", "teema.css");

Using AJAX and PHP to change the theme. 使用AJAX和PHP来更改主题。

Since you already use the id="theme" in your <link /> tag, we can easily set the stuff in jQuery using AJAX. 由于您已经在<link />标记中使用了id="theme" ,因此我们可以使用AJAX轻松地在jQuery中设置内容。

So, you give a theme request this way: 所以,你这样给出一个主题请求:

$.getScript("theme.php?teema=theme");

And in the PHP: 在PHP中:

header("Content-type: text/javascript");
$('#theme').attr('href', 'css/<?php echo $_GET['teema']; ?>');

更改此链接的内容( $('$#theme').attr('href', 'css/' + selectedtheme +'.css');您也可以在javascript中设置cookie( document.cookie = 'theme=' + selectedtheme

You can simply make an ajax request so the cookie will be returned 您可以简单地发出ajax请求,以便返回cookie

Using jQuery: 使用jQuery:

$('.ajaxLink').click(function(e) {
    e.preventDefault();
    var teemaUrl=this.href;
    var teema=teemaUrl.split('=').pop()
    $.get(teemaUrl, { js:true}, function(data){
       $('#theme').attr('href','css/'+ teema+'.css')

    })
});

This will parse the teema value from link href , make call to that href ( may need to add path info) and update href of link tag with ID=theme 这将解析链接hrefteema值,调用该href (可能需要添加路径信息)并更新ID = theme的链接标记的href

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM