0

Was so awesome of the customer to type "%10" instead of "10%"

0_o

$PACKAGE_json_decode = json_decode(urldecode(($_POST['textarea']), true);

print_r($PACKAGE_json_decode); // LENGTH 0


foreach($PACKAGE_json_decode as $row){
}

ERROR: "Message: Invalid argument supplied for foreach()"

How do I urldecode without causing the %10 to take on a different meaning when sent back via AJAX?

And decode seems to produce that square character I cannot paste here... you know ... looks sorta like "[]"

*The string needs to be the same for the client when they get it back - they save it with a % they want it back with a %. - Any suggestions about replacing it?

Jonathan Gagne
  • 4,241
  • 5
  • 18
  • 30

3 Answers3

0

You could encode your request send by Ajax directly, such as below:

$.ajax({
    type:'POST',
    dataType: 'json',
    ...

Or like this:

JSON.stringify('%10');

Inside your PHP, just json_decode() now, as below.

$PACKAGE_json_decode = json_decode($_POST['textarea']);

Like this, your %10 will become "%10", et voila!!!

Jonathan Gagne
  • 4,241
  • 5
  • 18
  • 30
  • 1
    Why and how will this fix the issue? What’s the mechanism? – Sami Kuhmonen Oct 06 '18 at 04:34
  • I refract my code as I thought about something better. – Jonathan Gagne Oct 06 '18 at 04:42
  • 1
    Since it’s already sent via AJAX according to the question, AJAX doesn’t change it into that, and urldecode will still mangle the %10, I don’t think that’s any better. I’d wait for the answer why they even need to decode something that’s not encoded in the first place. – Sami Kuhmonen Oct 06 '18 at 04:45
  • @SamiKuhmonen short example. Try `JSON.stringify('%10')` you will understand. You will get `"\"%10\""` and not `'%10'` – Jonathan Gagne Oct 06 '18 at 04:52
  • 1
    It’s not a single thing being sent from there, it’s inside other data. Again, let’s wait until they explain why they’re trying to do this before answering with hypotheticals. – Sami Kuhmonen Oct 06 '18 at 04:54
0

The % character is used in URL encoding. Either you remove % from the front end before passing to the server or you deal the same in the server side.

Sunil Singh
  • 11,001
  • 2
  • 27
  • 48
  • The string needs to be the same for the client when they get it back - they save it with a % they want it back with a %. - Any suggestions about replacing it? –  Oct 09 '18 at 20:59
  • Added one more answer below to share the code. Just provide more info where is it breaking ? – Sunil Singh Oct 10 '18 at 08:49
  • when returning from the database nd passing back with AJAX –  Oct 11 '18 at 02:17
0

Please use the following code and let me know where is it breacking ?

<?php

    $url_encode = urldecode("sampleTextWith%");
    echo '<b>Ecoded URL </b>'. $url_encode."<br>";

    $obj = new StdClass();
    $obj->text = $url_encode;

    $encoded_json = json_encode($obj);
    echo '<b>Encoded JSON </b>'. $encoded_json."<br>";

    $decoded_json = json_decode($encoded_json);

    echo "<b>Decoded JSON </b>";

    print_r($decoded_json);

    echo "<br>";

    foreach($decoded_json as $row){
        echo "<b>Row Value : </b>". $row."<br>";
    }

?>
Sunil Singh
  • 11,001
  • 2
  • 27
  • 48