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.

3
  • Just a side note: as h1 is a block element, you probably do not need to insert it in a div (with the same class moreover). Commented Jun 20, 2013 at 9:20
  • yeah I know that, thats jus for the example, thanks @Voitcus Commented Jun 20, 2013 at 9: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. Commented Jun 20, 2013 at 9:23

2 Answers 2

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);
Sign up to request clarification or add additional context in comments.

2 Comments

where I put the .htaccess file? in the root? or in the path where I save my .html file? @DevZer0
you can have it in the root or the same level as the script file
0

Your problem is caused by magic_quotes_gpc.

2 possible solutions:

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

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.