0

I'm having issues with formulating the right combination for an identical match of input and output from Javascript to PHP and then back to Javascript

Javascript Encode: (textarea=input1) => outputs to (textarea=input2) btoa(unescape(encodeURIComponent(document.querySelector('.input1').value)));

PHP Decode: (textarea=input2) => outputs to (textarea=input3) htmlspecialchars(SQLite3::escapeString(base64_decode($_POST['input2'])));

PHP Encode: (textarea=input3) => outputs to (textarea=input4) base64_encode(htmlspecialchars(urldecode(($d['data']))));

Javascript Decode: (textarea=input4) => outputs to (textarea=input5) decodeURIComponent(escape(atob(document.querySelector('.input4').value)));

But they do not match, I use https://text-compare.com/ to compare, it outputs in input and shows " and it also Deletes all + signs

How do I get a both input and output to match identically?

6
  • 1
    Why such complication? Why do you need URI encoding, Base64 and HTML escaping all at the same time? Commented Apr 26, 2020 at 11:53
  • It is unclear from this how exactly you pass that data, but as for strictly the format of data to use, i think you'd have more luck with json. Which would be json_encode and json_decode in PHP, and JSON.stringify and JSON.parse in Javascript. Optionally you can still base64 that if your goal is to hide it. But this will be more reliable than all the escaping you're currently doing. Sidenote, maybe it's the ENT_QUOTES parameter you were missing on the PHP side. Commented Apr 26, 2020 at 12:09
  • I'm passing it through Ajax and I get Latin1 out of range and it is being passed in json, its the encoding that is the issue Commented Apr 26, 2020 at 12:24
  • Oh, i see what you're saying.. Well in that case your solution is mb_convert_encoding in PHP. Commented Apr 26, 2020 at 12:39
  • in which part do I place that? Commented Apr 26, 2020 at 13:56

1 Answer 1

1

What I used, do correct me if I'm wrong/quicker way, it encodes, passes through ajax to php, decodes, encodes, passes back through ajax to js response, then final decode, the match from input to output was identical even with emojis

I'm posting the method below to help anyone in the same situation as I was

Method was 
  JS Encode: url > btoa
  PHP Clean: ' ' > '+'
  PHP Decode: json > base64 > url
  PHP Encode: url > base64 > json
  JS Decode: json > atob > url

JS Page:
  Encode to pass through AJAX to PHP :
    //create the json objects
    var obj = {"data":{}};
    //add data
    obj['data'][0] = window.btoa(encodeURIComponent((data)));
  Decode from PHP through AJax
    //parse json
    var json = JSON.parse(xhr.responseText);
    //decode
    var x = (decodeURIComponent(window.atob(json.data[0])));
  Ajax Header: Post
  xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded;charset=utf-8");

PHP Page:
  Decode:
    //clean post
    $clean = strtr($_POST["x"], ' ', '+');
    //decode json
    $obj = json_decode($clean, true);
    //decode javascript post
    $decode['data']['0']  = rawurldecode(base64_decode($obj['data'][0]));
  Encode:
    //encode javascript post
    $encode['data']['0']  = base64_encode(rawurlencode($decode['data'][0]));
    //encode json
    echo json_encode($encode);

The clean of the post I got from this page JavaScript atob operation using PHP

The rawurl I got from this page PHP equivalent for javascript escape/unescape

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

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.