0

I'm saving JSON string (using javascript's JSON.stringify) via Ajax call to a php script that write this string into a file i.e.

<?php

    $msg = $_GET['hecdata'];

    if (strlen($msg) > 0)
    {
        $file = fopen("hecdata.txt","w");
        fwrite($file,$msg);
        fclose($file);

        echo "True";        
    }
    else
    {
        echo "False";               
    }

?>

but the problem is the format of JSON become:

[{\"customerName\":\"Customer 1\",\"contactNumber\":\"03001234567\",\"hallName\":\"4\",\"bookingDate\":\"09/30/2013\"}]

which is not correct, I am new to php, is this how we should save a json string into a text file?

3
  • 4
    Magic Quotes? Commented Sep 30, 2013 at 11:59
  • What @deceze said. You have magic quotes enabled on your server, which is escaping all the quotes. This has nothing to do with JavaScript, JSON or any of the PHP code you've shown :p Commented Sep 30, 2013 at 12:02
  • It would, indeed, appear to be a magic-quotes issue, which you should disable ASAP Commented Sep 30, 2013 at 12:04

2 Answers 2

1

try below code:

$msg = stripslashes($msg);

and then write it to file. Refer http://php.net/manual/en/function.stripslashes.php

Sign up to request clarification or add additional context in comments.

Comments

0

JSON is correct, you have extra brace "}" on this string

1 Comment

Sorry about that edited the json, I just took some data from the whole string.

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.