PHP表单提交,强制CSS重新加载?

PHP表单提交,强制CSS重新加载?

问题描述:

I have a PHP form that I need to force a CSS reload once submitted. How do I do this?

Thanks!

我有一个PHP表单,我需要在提交后强制重新加载CSS。 我该怎么做? p>

谢谢! p> div>

The only way I can think of to guarantee that the CSS reloads, is to change the name of the css file.

You could create a rewrite rule that redirects all addresses of type "...mycssfile.css1234" to "...mycssfile.css" and in your php, add a random 4 digits to the end of your css file location.

Edit: To reflect the comments to this answer, you can do the following:

<link rel="STYLESHEET" href="mycssfile.css?r=<?php echo rand(1, 999999); ?>" type="text/css">

Which would force the browser to update the css page, since the address to the css file changed, although it will actually link to the same css file.

Is there some reason the whole page can't reset? Because if that's okay, then the form could just submit to the page itself, with an action of get, and the (dropdown|input|radio button) submitted as an argument (something like "css").

When the page loads, PHP can check for the variable:

$css = empty($_GET['css']) ? $_GET['css'] : 'default';

Then, it can echo the stylesheet's href based on that variable:

<link href="<?php echo htmlspecialchars($css, ENT_QUOTES, 'UTF-8') ?>.css" ... />

You can do it via Javascript.

<form id="form" onsubmit="cssChange()" action=".....

For example cssChange function :

     function cssChange() {

         var a = document.getElementsByTagName('link');
         var random = Math.floor(Math.random()*11);
         if(a.getAttribute('rel') == 'css')
             {
                    a.attr('src', 'new-css.css?' + random);
             }

     }

And you can define .newClass class on your css file.