0

Now I googled a bit and could not find anything that may helps me so I decided to ask you.

I have a script that collects Data from a form. Source is not needed.

There is also a <textarea id="sample"></textarea> where some content gets written in. In my jQuery is use following Line:

$(document).ready(function(){
    var cData = {};
    var url = 'http://www.sample.xy/samplefolder/index.php?cData=';

    $('#submit_btn').on('click', function(){

        //here are some more fields
        //...

        cData.text = $('#sample').val();
    }

    var bCode = btoa('{"field_one":"' + cData.field_one + '", "text":"' + cData.text + '"}');
    //sending the data...
    window.open(url + bCode, '_blank');
});

Actually this all works fine. But on the PHP side I run into json problems.

btoa transforms my content into a base64 string. My PHP source looks like

$data   =  base64_decode( $_GET['cData'] ) ;

What brings me the expacted string

{"field_one":"i am field one","text":"I am a text over
multiple rows

and this makes me
not valid for json"}

As you may noticed in the content of text, multirow causes the json validate of json_decode( ... ) to fail.

So my question is, how can I get the multi row valid for json without losing the linebreaks? Because I need them as they are. Because I want to use the content to create a PDF file with FPDF. So filling in <br /> doesn't really helps.

I tried json_encode before json_decode what just retuned my a null :/

Dwza
  • 6,494
  • 6
  • 41
  • 73
  • JSON does not allow line-breaks, you need to replace all the line breaks into `\n` – Daan Jul 23 '15 at 10:28
  • Take a look here: http://stackoverflow.com/questions/4253367/how-to-escape-a-json-string-containing-newline-characters-using-javascript – t.h3ads Jul 23 '15 at 10:28
  • but textarea uses `\n` too... or how can I catch the textarea linebreaks ? – Dwza Jul 23 '15 at 10:30
  • @Typoheads, hmm i try this... i will tell you if i was successful – Dwza Jul 23 '15 at 10:30

0 Answers0