/    Sign up×
Community /Pin to ProfileBookmark

Javascript + PHP $_GET URL with % gives me 406 Not Acceptable

Hello to everyone out there!

I am Akke, and I’m a newbie developer, I know some VB, OK PHP, and OK JavaScript and some AJAX.

I’m working on a project that requires me to send stuff through an AJAX $_GET request to a PHP page which then echoes a string which then is fed back to the form on the first page.

So the GET url looks something like this:

[CODE]dataminer.php?question=kassasumma&q=500&kale=10%&nro=6[/CODE]

I have tried to escape that URL with various escaping methods including

[CODE]escape()[/CODE]

and

[CODE]encodeURIComponent()[/CODE]

but, with no luck. The server keeps giving me a 406 Not Acceptable and it’s because of the percentage sign (which is essential) in the url.

Does anyone of you kind sirs have any suggestions?

Sincerly,

Akke

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@007JulienJul 06.2011 — You have not to encode all the URL but only values (if necessary) with encodeURIComponent ...

SEe too PHP urldecode notes : The superglobals $_GET and $_REQUEST are already decoded. Using urldecode() on an element in $_GET or $_REQUEST could have unexpected and dangerous results.
Copy linkTweet thisAlerts:
@AkkeDaBestauthorJul 07.2011 — Thank you a LOT for the answer. I tried applying that in several ways, but failed to get a valid respose from the server (still 406 not acceptable)

Here are 2 different ways I tried applying it:

[CODE] //Set kale variable
kale = document.getElementById("TOS_K_ale").value;
//attempt 1
xmlhttp.open("GET","dataminer.php?question="+encodeURIComponent("kassasumma")+"&q="+encodeURIComponent(str)+"&kale="+encodeURIComponent(kale)+"&nro="+encodeURIComponent(document.getElementById("S_NRO").value),true);
//attempt 2
xmlhttp.open("GET",encodeURIComponent("dataminer.php?question=kassasumma&q="+str+"&kale="+kale+"&nro="+document.getElementById("S_NRO").value),true);[/CODE]


The first ways' URL looks like this: [CODE]/dataminer.php?question=kassasumma&q=500&kale=10%25&nro=6[/CODE]
The second ways' URL looks like this:[CODE]/dataminer.php%3Fquestion%3Dkassasumma%26q%3D500%26kale%3D10%25%26nro%3D6[/CODE]

What did I mess up? ?
Copy linkTweet thisAlerts:
@007JulienJul 07.2011 — The first way is the rigth way.This simple test
[code=php]<?php
header("Content-Type:text/html;charset=utf-8");
if (!empty($_GET)) {
foreach ($_GET as $k=>$v) echo "<br>$k => $v";
}
// Give effectively with this first adress
question => kassasumma
q => 500
kale => 10%
nro => 6
?>[/code]

Your error 406 as probably an other origin. See this page
Copy linkTweet thisAlerts:
@AkkeDaBestauthorJul 07.2011 — Thanks for the reply.

I tried that test, but it gave me the good old 406.

I have however solved this problem an other way, which is replacing the % with p and then sending it to the PHP script:
[CODE]xmlhttp.open("GET","dataminer.php?question=kassasumma&q="+str+"&kale="+kale.replace("%", "p")+"&nro="+document.getElementById("S_NRO").value,true);[/CODE]
notice the
[CODE]+kale.replace("%", "p")+[/CODE]
On the PHP side, I regexp replace the p back to % if it is there:
[code=php]preg_match('[p]', $kale, $matches);

if ($question == "kassasumma") {
if (!empty($matches)) {
$kale = preg_replace('[p]', '%', $kale);
$tapa = "1";
$kassasumma = $laskun_summa - ($laskun_summa * ($kale / 100)) . "€";
}

if (empty($matches)) {
$kale = preg_replace('[€]', '', $kale);
$tapa = "2";
$kassasumma = $laskun_summa - $kale ."€";
}
echo $kassasumma;
}[/code]


Thanks a lot for the help anyway!

--Akke
×

Success!

Help @AkkeDaBest spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...