July 11, 2010

The most strange error in the world (PHP $_GET, $_POST, $_REQUEST and ajax)

Question by Eureka

I hope you’ll able to help me. I’m fed up of trying things without any solution and php it’s just driving me crazy. I’m looking for help because I have a html document where I use ajax thanks to jquery api. Inside this file, in a js function I have:

$.ajax({
type: "GET",
url: "c.php",
data: "dia="+matriz[0]+"&mes="+matriz[1]+"&ano="+matriz[2]+"&diaa="+matriz2[0]+"&mess="+matriz2[1]+"&anoo="+matriz2[2]+"&modo=0&semana=0",
success: Mundo,
error: function(e){
alert('Error: ' + e);
}
});

This code allows me to send the information that I want to the file c.php where I have:


include('funciones.php');
include('config.php');

 $mierda = array();
 $mierda[0] = $_GET['modo']; 
 $mierda[1] = $_GET['dia']; 
 $mierda[2] = $_GET['mes']; 
 $mierda[3] = $_GET['ano']; 
 $mierda[4] = $_GET['diaa']; 
 $mierda[5] = $_GET['mess']; 
 $mierda[6] = $_GET['anoo']; 
 $mierda[7] = $_GET['semana'];   

As you see it’s very simple. My crazy problem is that with firebug I’ve seen that the data is sent well but for some reason I can’t use it. I have tried with $_Get, $_post and $_request and always is the same problem. But this can be stranger… If I put:

echo json_encode($mierda);

then miraculously, the php returns the data that I have passed so in conclusion I have:

  1. I can send the data to the php file well
  2. I can print all the data that I have sent well just accessing yo $_GET, $_POST, $_REQUEST
  3. I can’t use any value separatly like $_GET[‘dia’]

What’s going wrong there?

PS. The include php files are functions that access to my database so there’s no interaction with them.

Answer by Starx

If you are returning a json value use json to read that.

See
http://api.jquery.com/jQuery.getJSON/

http://pinoytech.org/blog/post/How-to-Use-JSON-with-jQuery-AJAX

Here is an example to read json value

$('document').ready(function(){
    $('#submit).click(function(){
        $.post('your_php_page.php', {employee_id: '456'},
            function(data){
                console.log(data.first_name);
                console.log(data.last_name);
            }, 'json');
    })
});

Hope it helps

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!