diego
diego

Reputation: 77

php - save a .html file formatted

This is my problem: I have my html document where I send via ajax two values: "id" and "a html string"

<div class="myClass"><h1 class="myClass">this is my html string </h1></div>

I receive this data in my .php file where I save the data in a .html file:

ajax code:

$(".button").on("click", function(e){
            e.preventDefault();
            var string = $(".htmlString").html();
            $.ajax({
                url: "data.php",
                type: "post",
                data: { 
                    ID: "correctID",
                    htmlString: string
                },
                success: function(){
                    alert('ok');
                },
                error:function(){
                    alert('error');
                }   
            }); 
        });

php code:

if ($_POST['id'] == "correctID") {

    $fileLocation = $_SERVER['DOCUMENT_ROOT']  . "/www/file.html";
    $file = fopen($fileLocation,"w");
    $content = $_POST['htmlString'];
    fwrite($file,$content);
    fclose($file);
}

the output .html file content is like:

<div class=\"myClass\"><h1 class=\"myClass\">

The PROBLEM as you see is the "\" before the quotes:

How can I save my file in a correct html format?

<div class="myClass"><h1 class="myClass">

Thanks a lot, was looking and I found DOMDocument::saveHTML but I could not use it, Im very new at PHP.., I really need the classes in my html file.

Upvotes: 0

Views: 3272

Answers (2)

Fracsi
Fracsi

Reputation: 2314

Your problem is caused by magic_quotes_gpc.

2 possible solutions:

  • Disable magic_quotes_gpc
  • call stripslashes on `$_POST['htmlString']

Upvotes: 0

DevZer0
DevZer0

Reputation: 13535

your issue is magic quotes, turn it off by using a .htaccess file and placing the following directive init.

php_flag magic_quotes_gpc Off

Also to save your file you can do with one line using

$file_content;
file_put_contents("filename.html", $file_content);

Upvotes: 1

Related Questions