0

I want to use jQuery to add some inline CSS which is generated by a PHP file. I want to be able to print the contents of the PHP file between the <style> tags.

Here's the relevant piece of the JS file:

$('#settings button.theme').on('click', function(){
    var whichone = $(this).data('file');
    $('<style type="text/css" media="screen" id="changer"></style>')
        .appendTo('head');
    $('#changer').load('http://example.com/css/style.php?details=' + whichone);
});

I don't want to change the style.php file as it generates the original CSS quite happily, but when I do the above, I'm getting a 500 Internal Server Error in relation to the PHP file.

Can anyone help please?

5
  • 2
    HTTP 500 errors usually denote an error when executing your script. Check your PHP log or webserver log to determine the source of the error in your PHP script. Commented May 28, 2013 at 14:19
  • Are you trying to load content from same domain? Commented May 28, 2013 at 14:19
  • Thanks. Yes, same domain. Commented May 28, 2013 at 14:22
  • Turn on php errors, what happens if you just load the PHP file in the browser? Do you get any errors? Commented May 28, 2013 at 14:25
  • It just prints out the CSS that I want. Commented May 28, 2013 at 14:31

1 Answer 1

3

Instead of trying to load the CSS via AJAX, just dynamically set the href attribute:

$('#settings button.theme').on('click', function(){
    var whichone = $(this).data('file');
    $('<link rel="stylesheet" type="text/css" media="screen" id="changer" />')
        .appendTo('head').attr('href', 'http://example.com/css/style.php?details=' + whichone);
});
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. Makes sense but still getting the 500 Internal Server Error

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.