0

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

Thanks!

3
  • Is there a reason that you don't want to reload the entire page? Commented Jan 29, 2011 at 0:31
  • The entire page has the processing script inside it, but when it reloads on submit the CSS is caching. If I do a ctrl F5 it loads correctly. Commented Jan 29, 2011 at 0:32
  • 4
    Easiest way to get around this is to append a query to the end of your .css link: mystyle.css?v=1 when the page posts back. Commented Jan 29, 2011 at 1:15

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

My suggestion is that you use the modified time of the CSS instead, maybe using filemtime or the last time the form was submitted.
1

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.

5 Comments

What would cssChange() do though? You haven't provided the function.
I wanted to give an idea to him. There are a lot of javascript funtion for changing css file. I will edit my answer now. Thank you.
that doesn't seem to answer the question
@Ben, can you check answer again ?
@Eray. Looks better. Maybe add a random number as query string to the css file to prevent caching too.
0

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" ... />

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.