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:
- I can send the data to the php file well
- I can print all the data that I have sent well just accessing yo $_GET, $_POST, $_REQUEST
- 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