I am extremely new to PHP and, although I am quite familiar to javascript, I am learning how to use the massive jqGrid plugin right now. I am trying to understand how jqGrid serializes the grid data and how PHP parses this data. Currently, I am not even connecting to MySQL, but I am simply trying to echo the serialized jqGrid data as “fake” results. I have the following code for js in the head of my PHP file:
<script type='text/javascript'>
$(function(){
$('#list').jgGrid({
url:'grid.php',
mtype:'POST',
colNames:['json'],
colModel:[{name:'j',index:'j',searchoptions:{sopt:['eq']},search:true}],
pager:'#pager',
rowNum:10,
viewrecords:true,
gridview:true,
serializeGridData:function(postData){
return postData;
}
})
});
</script>
I then send this information to my ‘grid.php’ file, which has the following code:
<?php
$jason = $_POST['postData'];
$page = $jason->{'page'};
echo '<rows>';
echo '<page>1</page>';
echo '<total>1</total>';
echo '<records>1</records';
echo '<row id="1">';
echo '<cell>'.$page.'</cell>';
echo '</row>';
echo '</rows>';
?>
When I remove the serializegriddata option from the JS, everything works fine (I also add in the default $_POST[‘page’], $_POST[‘rows’], $_POST[‘sidx’], $_POST[‘sord’] back into the PHP). The problem comes in when I add the serializegriddata.
I am looking for any examples of how to use the postData on the client side (are the any other functions I need to add to the serializegriddata or can I just return the postData) and how to properly parse this in PHP (how to $_POST the data and then how to parse and use this data). I know this is probably an extremely simple solution, but everything I find just talks about the client and says nothing about the server side. Thanks in advance.
Okay, I am a little slow, but I partially answered my own question. All of the jqGrid documentation read as if your entire postData was parsed as a JSON string if you set multipleSearch:true. I thought that I had to parse every variable into a postData JSON variable, then pass this to PHP. Although the solution took a little work to properly implement, the thing that I was missing was the fact that with multipleSearch:true, this adds just a ‘filter’ variable to the AJAX call. This filter variable is parsed as follows:
and the result of $filters is:
This is as opposed to the multipleSearch:false option of:
Once I got this, I was able to loop through all instances of my search parameters and create my $where variable. One thing that almost became a big problem for me was the old example I was able to find about properly constructing a (link below) is that you can only have WHERE is a mysql_query only 1 time.
http://blog.brzezinka.eu/webmaster-tips/jquery/how-to-enable-the-search-functionality-in-jqgrid
I hope this might help someone in the future (it drove me crazy for almost 2 days straight).