So here’s a situation that made me want to pull my hair out.
I was trying to pass a Korean string using the GET method—just adding it directly to a script’s URL. Everything lookedfine in the browser, but when PHP tried to read the value… boom. Garbage characters. Gibberish. Definitely not what I sent.
Let me explain what’s going on.
The Setup
- I have a file called
A.php
that does some server-side calculations and outputs JavaScript code. - In the main HTML, a certain event triggers dynamically loading this
A.php
file as a script.
Here’s a simplified version:
let s = document.createElement('script');
s.src = './A.php?param=뭐임마';
document.body.appendChild(s);
Looks like a kind of pseudo-AJAX, right? The browser treats A.php
like any other script and runs whatever it outputs.
The Problem
In A.php
, I tried to access $_GET['param']
, expecting to see "뭐임마"
.
Instead, I got this nightmare:
%EB%AD%90%EC%9E%84%EB%A7%88
What is this mess?
It’s URL encoding. The browser automatically encodes special characters (like Korean) when sending them in a GET request. That’s why you see the %
symbols.
Even worse—when I tried to just echo the value in a script, it triggered JavaScript syntax errors. That only made me more frustrated.
The Fix – Use urldecode()
After some trial and error, I realized the value just needs to be decoded back to a readable format on the PHP side.
So here’s what worked:
$param = urldecode($_GET['param']);
Boom. "뭐임마"
appears as expected.
Why This Happens
When sending data via the GET method (especially dynamically in script tags), any non-ASCII characters (like Korean, Chinese, etc.) get URL-encoded.
Example:
뭐임마 → %EB%AD%90%EC%9E%84%EB%A7%88
This is totally normal. But if you don’t decode it, PHP will treat it as raw text and give you the encoded mess.
Bonus Tips
- Always wrap your
$_GET
variables inurldecode()
if you’re expecting non-English input. - Also make sure your PHP files are UTF-8 encoded without BOM.
- If you’re outputting to a script, sanitize the string properly or escape quotes to avoid JS syntax errors.
Final Thoughts
Yes, technically this isn’t AJAX—but it feels similar because you’re dynamically loading server-side logic into the page. The key is to remember: GET values are always URL-encoded, so don’t forget to decode them on the server.
What started as a frustrating bug ended up being a reminder that the web is full of encoding gotchas—but luckily, urldecode()
is all it takes to solve this one.