0

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.

diego
  • 77
  • 1
  • 3
  • 12
  • Just a side note: as [`h1` is a block element](http://stackoverflow.com/q/4041820/2088851), you probably do not need to insert it in a `div` (with the same class moreover). – Voitcus Jun 20 '13 at 09:20
  • yeah I know that, thats jus for the example, thanks @Voitcus – diego Jun 20 '13 at 09:22
  • A first thing to do would be to var_dump($_POST) to make sure it doesn't contain the quotes. This would tell you whether the error is from js or php. – Virus721 Jun 20 '13 at 09:23

2 Answers2

1

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);
DevZer0
  • 13,433
  • 7
  • 27
  • 51
0

Your problem is caused by magic_quotes_gpc.

2 possible solutions:

  • Disable magic_quotes_gpc
  • call stripslashes on `$_POST['htmlString']
Fracsi
  • 2,274
  • 15
  • 24