Question

I am using jtable to display the data for ease of access.

Here, my problem is the php page fails to get the $data value and hence I am unable to retrieve the queried data. It always goes into the else case, don't know why!!

I tried hard but no ideas were successful. Any help is appreciated.

HTML:
    <link href="../layout/jtable/jquery-ui.css" rel="stylesheet" type="text/css" />
    <link href="../layout/jtable/themes/metro/blue/jtable.css" rel="stylesheet" type="text/css" />

    <script src="../layout/scripts/jquery-latest.min.js" type="text/javascript"></script>
    <script src="../layout/scripts/jquery-ui.min.js" type="text/javascript"></script>
    <script src="../layout/jtable/jquery.jtable.js" type="text/javascript"></script>


<div align="center">
            <label class="" for="qstring"> <b class="red font-large"> <span class="icon-search"></span> Search &nbsp; </b> </label>
            <input type="text" id="qstring" name="qstring" class="search" style="height:30px; width:40%; font-size:16px" placeholder="Type Name or Department or Mobile" autofocus />
</div> 
<div id="EmployeeContainer"></div>

          <script type="text/javascript">

        $(document).ready(function () {
            $('.search').keyup(function(){
                $.ajax({
                    url: 'EMPActions.php',
                    type: 'get',
                    data: {qstr: $('input#qstring').val()},
                    success: function(response) {
                        $('#EmployeeContainer').jtable('load');
                        //$('#EmployeeContainer').html(response);
                    }
                });
            });

            //Prepare jTable
            $('#EmployeeContainer').jtable({
                title: 'Employee Details',
                actions: {
                    listAction: 'EMPActions.php?action=list'

                },
                fields: {
                    EID: {
                        title: 'EID',
                        width: '10%'
                    },
                    EName: {
                        title: 'EName',
                        width: '20%'
                    },
                    Desgn: {
                        title: 'Designation',
                        width: '10%'                        
                    },
                    Dept: {
                        title: 'Department',
                        width: '15%'
                    },                  
                    Mobile: {
                        title: 'Mobile',
                        width: '15%'                        
                    },
                    EMail1: {
                        title: 'RGUKT Mail',
                        width: '15%'                        
                    },
                    EMail2: {
                        title: 'Other EMail',
                        width: '15%'                        
                    }
                }
            });

        });//

    </script>

PHP Page: EmpActions.php
<?php

    if(isset($_GET['qstr'])){
    $data = '%'.$_GET['qstr'].'%';
    }
    else{
    $data = '%';
    }

try
{   
    //echo $data;

    $table = "employee_data";

    //Open database connection
    //echo $data;
    $con = mysql_connect("localhost","root","xampp123");
    mysql_select_db("ecelldata_2013-14", $con);

      //Getting records (listAction)
      if($_GET["action"] == "list")
      {

        //Get records from database
        $result = mysql_query("SELECT * FROM ".$table." WHERE EName LIKE '$data'");
        //echo $data;
          //Add all records to an array
          $rows = array();
          while($row = mysql_fetch_array($result))
          {
              $rows[] = $row;
          }

          //Return result to jTable
          $jTableResult = array();
          $jTableResult['Result'] = "OK";
          $jTableResult['Records'] = $rows;
          print json_encode($jTableResult);
      }


      //Close database connection
      mysql_close($con);


}
catch(Exception $ex)
{
    //Return error message
    $jTableResult = array();
    $jTableResult['Result'] = "ERROR";
    $jTableResult['Message'] = $ex->getMessage();
    print json_encode($jTableResult);
}

?>
Était-ce utile?

La solution 3

Finally got the solution. I have changed the function to

$('.search').keyup(function(e){ $('#EmployeeContainer').jtable('load', { qstr: $('input#qstring').val(), });

The value qstr is sent using POST. So at the other end i'll have to use $_POST['qstr']

Autres conseils

try: jquery:

 $.post('EMPActions.php',{

                data = {qstr: $('input#qstring').val()}
                },
                function(response) {
                    $('#EmployeeContainer').jtable('load');
                    //$('#EmployeeContainer').html(response);
                },"json");

PHP

if(isset($_POST['qstr'])){
$data = '%'.$_POST['qstr'].'%';
}
else{
$data = '%';
}

on ajax try this as data instead

data:$('input#qstring'),

or

data: $('input[\'name=name_of_your_field]\]'),

Your keyup call is to this php page:

EMPActions.php?qstr=foo

The results of that page are never used (it's commented out) That success handler initializes the table. However, jtable has its own system for making data calls jtable does this to enable paging, sorting, searching, etc - each makes a slightly different call to the same page Those calls all look like this

EMPActions.php?action=list

Notice that the qstr parameter is not passed to the jtable calls

What you'll need to do is pass the qstr parameter into the initializer of the jtable, so that all data calls from jtable contain the parameter

Something like this:

$('#EmployeeContainer').jtable({
    title: 'Employee Details',
    actions: {
        listAction: 'EMPActions.php?action=list&qstr='+ $('input#qstring').val()
        }
    ...
    });

Note this will only set the qstr value the initially - you'll want to use jtable searching to narrow the results that are loaded into the table

Edit

Ok, after seeing the demo, I think its a little clearer what you want to do. I initially thought you were using some filter to setup the table; instead, you want to use a non-jtable field to filter the data constantly.

A few notes on your current live code: I think you're missing a '=' in listAction, but more importantly, listAction is only being defined once in your code, when its first run (and the text box is empty). To use your filter box, you'll need to somehow update the data in jtable on each keyup.

Looking over the jtable API, I see two possibilities:

1: Define a function instead of a string for the listAction. This will use the current version of your string field to filter the data, and will allow the normal jtable features like sorting. Ok each keyup, you should call $('#EmployeeContainer').jtable('load'); which will invoke your listAction function.

listAction: function (postData, jtParams) {
    return $.Deferred(function ($dfd) {
        $.ajax({
            url: 'EMPActions.php?action=list&qstr=' + $('input#qstring').val() + '&jtStartIndex=' + jtParams.jtStartIndex + '&jtPageSize=' + jtParams.jtPageSize + '&jtSorting=' + jtParams.jtSorting,
            type: 'POST',
            dataType: 'json',
            data: postData,
            success: function (data) {
                $dfd.resolve(data);
            },
            error: function () {
                $dfd.reject();
            }
        });
    });
},

2: Call the load method with your qstr parameter on each keyup (instead of making an ajax call yourself). This is simpler, but disables the normal jtable features.

$('#EmployeeContainer').jtable('load', { qstr: $('input#qstring').val() });

Edit #2

Replace your code with this, see if it works. You will need to use $_POST or $_REQUEST with this method, since this is using jtable to make the requests.

$(document).ready(function () {
    $('.search').keyup(function(){
        $('#EmployeeContainer').jtable('load', { qstr: $('input#qstring').val() });
    });             

    //Prepare jTable
    $('#EmployeeContainer').jtable({
        title: 'Employee Details',
        actions: {
            listAction: 'EMPActions.php?action=list',
            createAction: 'EMPActions.php?action=create',
            updateAction: 'EMPActions.php?action=update',
            deleteAction: 'EMPActions.php?action=delete'
        },
        fields: {
            EID: {
                title: 'EID',
                width: '10%'
                },
            EName: {
                title: 'EName',
                width: '20%'
            },
            Desgn: {
                title: 'Designation',
                width: '10%'                        
            },
            Dept: {
                title: 'Department',
                width: '15%'
            },                  
            Mobile: {
                title: 'Mobile',
                width: '15%'                        
            },
            EMail1: {
                title: 'RGUKT Mail',
                width: '15%'                        
            },
            EMail2: {
                title: 'Other EMail',
                width: '15%'                        
            }
        }
    });
});
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top