-1

I'm trying to do a rawurlencode, but it is not working as I though it would do. The data I'm trying to encode is coming from javascript and I stored the information on a variable. The value of the variable seems to be correct (did echo in a couple of places) everywhere except after the encoding. It seems that instead of using the value of the variable for the encoding is using the script that generated the value and encoding that.

<?php

//getting the author name using javascript
$author1=" 
<script type=\"text/javascript\">
jsauthor1 = document.getElementsByTagName(\"span\")[8].innerText;
document.write(jsauthor1);
</script>";

// checking Author name is correct
echo $author1." : Author name after Javascript extraction". "<br>";
$params = array(
    "Service" => "AWSECommerceService",
    "Operation" => "ItemSearch",
    "AWSAccessKeyId" => "AKIAJSGKPYO6HFNCED7A",
    "AssociateTag" => "hobbytryout0a-20",
    "SearchIndex" => "Books",
    "Sort" => "price",
    "Author" => $author1,
);

// checking that array have correct values (taking the value of "$author" correctly)    
echo $params[Author]." : Author name after creating array using variable <br>";

// convert $params[Author] to a String to test the encoding
$test = (string)$params[Author];
echo $test." : Author name after creating string from array"."<br>";
echo rawurlencode($test)." : Author name after rawurlencode"."<br>";
?>

This is what I get:

Jules Verne : Author name after Javascript extraction

Jules Verne : Author name after creating array using variable

Jules Verne : Author name after creating string from array

%20%0A%20%20%20%20%3Cscript%20type%3D%22text%2Fjavascript%22%3E%0A%20%20%20%20jsauthor1%20%3D%20document.getElementsByTagName%28%22span%22%29%5B8%5D.innerText%3B%0A%20%20%20%20document.write%28jsauthor1%29%3B%0A%20%20%20%20%3C%2Fscript%3E : Author name after rawurlencode

Shouldn't it be Jules%20Verne ?

Thanks

Barmar
  • 741,623
  • 53
  • 500
  • 612
Viter
  • 1
  • 1
  • You are outputting in a browser? If so the `js` executes in the browser. The PHP has re-encoded the `js` block so it won't execute as `js`. Your `author` should also be in quotes, here `$params[Author]`. – chris85 Feb 10 '16 at 20:59
  • You're encoding the Javascript code, you're not making the Javascript code encode the value it extracts. – Barmar Feb 10 '16 at 21:23
  • @chris85 . Thanks for your reply. I'm using the js to extract a value on the browser, then data is manipulated for a API request further down (not included in this sample because I think everything else works). I think the js is doing its job correctly, isn't it? The value I was trying to extract was "Jules Verne" on that page. I do echo in 3 places more and get "Jules Verne" again. Only when I try to do the rawurlencode is not taking the value of the variable but the actual js block. Is there a way to force it to do the encoding to the actual value? – Viter Feb 10 '16 at 23:14
  • @barmar. Thanks for your reply. Are you suggesting I do the encoding inside the js block? Is there a reason why is this thing is encoding the javascript instead of the value it extracts? – Viter Feb 10 '16 at 23:19
  • PHP executes first, it outputs your JS. It also is encoding your JS block currently because you tell it to. You want to encode after the page loads and before the ajax request, you will need to do that with JS, or with a second ajax request to a PHP script to encode. – chris85 Feb 10 '16 at 23:20
  • @Viter It's encoding the Javascript because you told it to. `$test` contains HTML and Javascript, you call `rawurlencode($test)`, so that returns encoded HTML+JS. Then you echo that. – Barmar Feb 10 '16 at 23:23
  • 1
    If you want to encode the JS variable, call `encodeURICompnent()` from JS. – Barmar Feb 10 '16 at 23:24
  • Take a look at this thread, http://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming... or see the above comment that sounds like the JS approach. – chris85 Feb 10 '16 at 23:24

1 Answers1

0

To encode the Javascript value, call encodeURIComponent from the Javascript:

$author1=" 
<script type=\"text/javascript\">
jsauthor1 = document.getElementsByTagName(\"span\")[8].innerText;
document.write(encodeURIComponent(jsauthor1));
</script>";
Barmar
  • 741,623
  • 53
  • 500
  • 612