1

I'm sending users to a new link which has a value that is encoded with Javascript's btoa, however, on the PHP side, when I decode it and echo the value out, the value that is echoed out is "undefined"

    $("#btn-print-bonus").click(function(){
        var id = btoa(id);
        window.open('/pdf/?type=bonus&id='+id+'&name='+fullname+'');
    });

php file

$id = base64_decode($_GET["id"]);
echo $id;
2
  • Where does fullname and id come from? If you open the browser developer tools and set a breakpoint on the click handler, you might be able to see what the argument to window.open will be. Commented Dec 20, 2016 at 9:02
  • PHP will never generate the undefined keyword by itself because it has no special meaning in the language. I'm pretty sure you have a literal undefined encoded in Base64. Commented Dec 20, 2016 at 9:05

1 Answer 1

1

here's the problem - var id = btoa(id);

var id will "mask" any var named id outside of that click callback, so in effect you are doing

var id = undefined;
id = btoa(id);

Which results in id=dW5kZWZpbmVk passed to php - check in the developer tools network tab and see if I'm right

dW5kZWZpbmVk is the string "undefined" base64 encoded - btoa coerces the given argument to a string

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.