www.webdeveloper.com
Results 1 to 5 of 5

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

Hybrid View

  1. #1
    Join Date
    Jul 2011
    Posts
    3

    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
    I have tried to escape that URL with various escaping methods including
    Code:
    escape()
    and
    Code:
    encodeURIComponent()
    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

  2. #2
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,253
    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.

  3. #3
    Join Date
    Jul 2011
    Posts
    3
    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);
    The first ways' URL looks like this:
    Code:
    /dataminer.php?question=kassasumma&q=500&kale=10%25&nro=6
    The second ways' URL looks like this:
    Code:
    /dataminer.php%3Fquestion%3Dkassasumma%26q%3D500%26kale%3D10%25%26nro%3D6
    What did I mess up?

  4. #4
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,253
    The first way is the rigth way.This simple test
    PHP Code:
    <?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
    => 500
    kale 
    => 10%
    nro => 6
    ?>
    Your error 406 as probably an other origin. See this page

  5. #5
    Join Date
    Jul 2011
    Posts
    3
    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);
    notice the
    Code:
    +kale.replace("%", "p")+
    On the PHP side, I regexp replace the p back to % if it is there:
    PHP Code:
    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;

    Thanks a lot for the help anyway!

    --Akke

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles