开发者

saveHTML() saves 2 copies

开发者 https://www.devze.com 2023-02-17 17:26 出处:网络
I have a dynamic PHP page which on button click change the css file linked to it, I\'ve done that saving the chosen css file to session, so the user will have the chosen css file all the time during h

I have a dynamic PHP page which on button click change the css file linked to it, I've done that saving the chosen css file to session, so the user will have the chosen css file all the time during his session.

My problem is, after editing the href element in the link html element -in order to link the new chosen file- I used saveHTML() function to save the edited version, the output was the disaster, I had two copies of all the elements on the page, I mean two banners, two submit buttons and everything is duplicated !!

my code looks like this:

<!--CSS Theme-->

<link id = "theme" rel="stylesheet" type="text/css" href="White.css" /> 

<!--Change Theme-->
<?php
    if(isSet($_GET["Theme"]))
    {
        $th = $_GET["Theme"];

        $_SESSION["Theme"] = $th;

    }
    else if(isSet($_SESSION["Theme"]))
    {
        $th = $_SESSION["Theme"];
    }

    else
    {
        $th = 'White.css';
    }

    switch ($th) {

      case 'Light Blue':
      $theme_file = 'LightBlue.css';
      break;


      default:
        $theme_file = 'White.css';

} 
    $dom = new DOMDocument;
    @$dom->loadHTMLFile('HomeWork.php');
    $node= $do开发者_运维知识库m->getElementById('theme') ; 
    $node->setAttribute('href', $theme_file);
    echo $dom->saveHTML();


?>

EDIT HomeWork.php is the same file which contains the previous code.

do you have any idea what's the problem??


If homework.php is the very same file you are showing, then it's no wonder the content is duplicated. When you execute homework.php, any HTML that appears before your DOM code is printed. Then your DOM gets executed, loads the very same content again, changes the attribute and prints the entire code once more.

You dont need DOM for this. Just calculate which CSS file you want to use, like you already do and instead of your DOM code, echo the filename and the appropriate position, e.g.

<?php
    // code to calculate from $_SESSION which filename to use
    // store in $fileName variable
?>
// HTML Stuff here
// insert the variable into the HTML
<link id="theme" rel="stylesheet" type="text/css" href="<?php echo $fileName ?>" /> 
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号