Saturday 10 November 2012

Sending data from object using jQuery ajax / post

Hi,

Recently I stumbled upon a problem to send an object created in javasctipt using jQuery ajax method. As I searched on internet, I found many answers suggesting to use JSON.stringify(object) method to send the data. But the fact is it doesn't work as you want it to work.

For example here is the object that I want to send to php.
var objReqData = new Object();
objReqData.page = 1;
objReqData.type = 1;
So this is the data you want to send to php. So ideal on php side you would like to receive it on $_POST variable (I am using post method) like $_POST['page'] and $_POST['type'].

Now if you use JSON.stringify() method, your request would be something like this

   $.ajax({ url: ajax/request.php, 
type: "POST",
      //Yes you can not simply use "data: JSON.stringify(objReqData)", if you use it that way you will not receive any post data
data: { params : JSON.stringify(objReqData) },  
success: function(data) {
     console.log(data);
},
error:  function(data) {
console.log(data);
}
});


So using this request on php side you will receive the data in $_POST['params'] variable not like you want to have it in straight $_POST['page'] and $_POST['type'] variables. Here $_POST['params'] variable will hold the json encoded values of page and type. So you again have to json_decode it in php.

But this is not the ideal solution. After much of research the best solution I found out was much simpler. You only need to use jQuery.param() method to serialize your object and that's it. Here is what I used.



   $.ajax({ url: ajax/request.php, 
type: "POST",
data: jQuery.param(objReqData) ,  //Yes this is all you need to write
success: function(data) {
console.log(data);
},
error:  function(data) {
console.log(data);
}
});

Using this request you receive the post data in variables $_POST['page'] and $_POST['type'], the way you most probably want to have them.

Thursday 15 March 2012

Calculate time taken for execution of php script or web page

Hi,

Often working on a website, it is quite necessary to check the execution speed of the script. As it's the most essential part of a website in terms of user experience that a web page loads as quick as possible.

I used to stuck to such situation, where I want to update my script functionality but not sure how it's gonna affect scripts execution speed. Here is wonderful script that I use to find out how much time it takes to execute the script.

<?php
$curTime = microtime();
$curTime = explode(' ', $curTime);
$strTime = $curTime[1] + $curTime[0];
/********************************************/
/********* PLACE YOUR CODE HERE **********/
/********************************************/ 

$curTime = microtime();
$curTime = explode(' ', $curTime);
$finTime = $curTime[1] + $curTime[0];
$totTime = round(($finTime - $strTime), 6);
echo 'Script took '.$totTime.' seconds to execute.'."\n";
?>

Monday 12 March 2012

Facebook server down today

Yes, that's right. facebook is down and not accessible today for quite sometime in india. Any official announcement or clarification are not made up till now for the problem. But it is speculated that it may be because of Indian government ban on facebook related to some content not being censored by facebook. Another speculation is that it may be because DDoS attack by hackers. Still nothing is clear.

You can access facebook on m.facebook.com, that is mobile version of facebook.

Facebook was earlier not accessible on 25th February, but that was only for short while. But today it has been down for quite long time. No clarification was given for the problem occurred on 25th February with facebook. Facebook is the most famous social networking site with more than 800 million users worldwide. It is famous for it's sleek user interface and impressive user experience. This is the first time that facebook severs are down for so long.

Hope to see facebook back again soon.