Datatables + Codeigniter POST data

Datatables + Codeigniter POST data

baytesbaytes Posts: 11Questions: 0Answers: 0
edited April 2013 in General
I am trying to populate my DataTable based on a dropdownbox selection in a Codeigniter view, however in my controller i have my instantiation of my datatable. So in a nutshell im trying to send post data from my view to my controller to have my results show up in the datatable itself. Below is the code for the views and controllers:

query.php (first view):

[code]
.....
.....




Select a product category




<?php

$server="xxxxx";
$username="xxxxx";
$password="xxxxx";
$conn=mysql_connect($server, $username, $password) or die ("Cannot connect to mysql server: ".mysql_error());

$dbname = 'xxxx';

@mysql_select_db($dbname) or die( "Unable to select database");

$query="SELECT childCategoryID,childCategoryName FROM Categories1";

$result = mysql_query ($query);

echo "";
echo "";

while($nt=mysql_fetch_assoc($result)){
echo "$nt[childCategoryName]";
}
echo "";// Closing of list box
echo "

";

echo "";
echo "Enter your search critera:
";
echo "";
echo "";
echo "";


?>


...
....
[/code]

Once the user selects a category from the drop down and hits search it displays the view 'data.php' Here is the code for it:


data.php (Second View)

[code]
....
....












Product ID:
Product Name:
Child Category:
Order Number:
Brand:
Mfg Number:
Product Description:





Loading data from server




Product ID:
Product Name:
Child Category:
Order Number:
Brand:
Mfg Number:
Product Description:




...
....
[/code]

Here is my controller:

query.php (controller)

[code]

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Query extends CI_Controller {

private $the_user; // <-------<--------<--------<-------<--------
// ^
public function __construct() // ^
{ // ^
parent::__construct(); // ^
// ^
// if the person accessing this controller is logged in // ^
if($this->ion_auth->logged_in()) { // ^
// ^
// get the user object // ^
$data->the_user = $this->ion_auth->user()->row(); // ^
// ^
// put the user object in class wide property--->---->-----
$this->the_user = $data->the_user;

// load $the_user in all displayed views automatically
$this->load->vars($data);
}
else // person not logged in
{
// send back to the root site
redirect('/');
}
}

public function index()
{
$this->load->view('query');
}


public function logout()
{
// log current user out and send back to public root
$this->ion_auth->logout();
redirect('/');
}

public function data()
{
$this->load->view('data');
}

public function getdatabyajax()
{
$childCategoryID=$_POST['childCategoryID'];

$this->load->library('Datatables');
$this->datatables->select('productID,productName,childCategoryName,orderNum,brand,mfgNum,productDescription');
/*$this->datatables->from('Products1');*/
$this->datatables->where('childCategoryID', '$_POST['$childCategoryID']);

echo $this->datatables->generate();
}
}

[/code]

Im getting an error: "Undefined index: childCategoryID" when i fire up firebug.

ANY HELP IS GREATLY APPRECIATED!!!!!!

Thanks,

Brad

Replies

  • baytesbaytes Posts: 11Questions: 0Answers: 0
    **Bump**

    I figured out that in my controller i can do

    $this->input->post(childCategoryID);

    but im still not able to pull the correct data for some reason, Anyone have any thoughts?
  • numberonenumberone Posts: 86Questions: 0Answers: 0
    $childCategoryID = $this->input->post('childCategoryID');

    $this->datatables->where('childCategoryID', $childCategoryID );
  • baytesbaytes Posts: 11Questions: 0Answers: 0
    numberone,

    Thanks for your reply. I switched the two lines i had with the two you suggested. It now loads the grid, however the grid says: " No Data Available In The Table".

    I've ran the sql query seperatetly in phpmyadmin to make sure there are results returning and there are but for some reason its not pulling into the grid.

    Any thoughts as to why this would be happening?

    Thanks again for your help!!!!
  • baytesbaytes Posts: 11Questions: 0Answers: 0
    numberone,

    Here are my updated files, im struggling to get this wrapped up for a client of mine.

    here is the function in my controller that is supposed to pull the POST data.
    [code]
    ....
    ....
    public function getdatabyajax()
    {
    $searchCritera = $this->input->post('searchCritera');
    $childCategoryID = $this->input->post('childCategoryID');

    /*$productName=$this->input->post('$_POST["productName"]');*/

    $this->load->library('Datatables');
    $this->datatables->select('productID,productName,childCategoryName,childCategoryID,orderNum,brand,mfgNum,productDescription');
    $this->datatables->from('Products1');
    $array = array('productName' => '$searchCritera%', 'childCategoryID' => '$childCategoryID');
    $this->datatables->where($array);

    /*$this->datatables->like('productName', $this->input->post('searchCritera'));*/
    /*$this->datatables->where('childCategoryID', $this->input->get_post('childCategoryID'));*/

    echo $this->datatables->generate();


    }
    ....
    ....
    [/code]


    here is the main view [query.php]
    [code]


    Welcome to Blu Lynx Communications

    @import "/css/demo_page.css";
    @import "/css/demo_table.css";



    ::selection{ background-color: #E13300; color: white; }
    ::moz-selection{ background-color: #E13300; color: white; }
    ::webkit-selection{ background-color: #E13300; color: white; }

    body {
    background-color: #fff;
    margin: 40px;
    font: 13px/20px normal Helvetica, Arial, sans-serif;
    color: #4F5155;
    }

    a {
    color: #003399;
    background-color: transparent;
    font-weight: normal;
    }

    h1 {
    color: #444;
    background-color: transparent;
    border-bottom: 5px solid #D0D0D0;
    font-size: 19px;
    font-weight: normal;
    margin: 0 0 14px 0;
    padding: 14px 15px 10px 15px;
    }

    code {
    font-family: Consolas, Monaco, Courier New, Courier, monospace;
    font-size: 12px;
    background-color: #f9f9f9;
    border: 1px solid #D0D0D0;
    color: #002166;
    display: block;
    margin: 14px 0 14px 0;
    padding: 12px 10px 12px 10px;
    }

    #body{
    margin: 0 15px 0 15px;
    }

    p.footer{
    text-align: right;
    font-size: 11px;
    border-top: 5px solid #D0D0D0;
    line-height: 32px;
    padding: 0 10px 0 10px;
    margin: 20px 0 0 0;
    }

    #container{
    margin: 10px;
    border: 5px solid #D0D0D0;
    -webkit-box-shadow: 0 0 8px #D0D0D0;
    }











    Select a product category




    <?php

    $server="xxxx";
    $username="xxxx";
    $password="xxxx";
    $conn=mysql_connect($server, $username, $password) or die ("Cannot connect to mysql server: ".mysql_error());

    $dbname = 'blulynx_new';

    @mysql_select_db($dbname) or die( "Unable to select database");

    $query="SELECT childCategoryID,childCategoryName FROM Categories1";

    $result = mysql_query ($query);

    echo "";
    echo "";

    while($nt=mysql_fetch_assoc($result)){
    echo "$nt[childCategoryName]";
    }
    echo "";// Closing of list box
    echo "
    ";
    echo "Enter your search critera:
    ";
    echo "";
    echo "";
    echo "";


    ?>




    Page rendered in {elapsed_time} seconds | version 0.1.0





    [/code]

    Here is the second view that loads [data.php]
    [code]


    Welcome to Blu Lynx Communications





    @import "/css/demo_page.css";
    @import "/css/demo_table.css";




    var oTable;

    $(document).ready(function() {
    $('#example').click( function() {
    $(this).toggleClass('row_selected');
    } );

    /* Initialise the DataTable */
    oTable = $('#example').dataTable( {
    "bProcessing": true,
    "bServerSide": true,
    "sAjaxSource": "getdatabyajax",
    "fnServerData": function(sSource, aoData, fnCallback)
    {

    aoData.push( { "childCategoryID": "childCategoryID", "value": $( "#childCategoryID" ).val() });
    $.ajax
    ({
    'dataType': 'json',
    'type' : 'POST',
    'url' : sSource,
    'data' : aoData,
    'success' : fnCallback
    });
    },


    } );

    } );







    ::selection{ background-color: #E13300; color: white; }
    ::moz-selection{ background-color: #E13300; color: white; }
    ::webkit-selection{ background-color: #E13300; color: white; }

    body {
    background-color: #fff;
    margin: 40px;
    font: 13px/20px normal Helvetica, Arial, sans-serif;
    color: #4F5155;
    }

    a {
    color: #003399;
    background-color: transparent;
    font-weight: normal;
    }

    h1 {
    color: #444;
    background-color: transparent;
    border-bottom: 5px solid #D0D0D0;
    font-size: 19px;
    font-weight: normal;
    margin: 0 0 14px 0;
    padding: 14px 15px 10px 15px;
    }

    code {
    font-family: Consolas, Monaco, Courier New, Courier, monospace;
    font-size: 12px;
    background-color: #f9f9f9;
    border: 1px solid #D0D0D0;
    color: #002166;
    display: block;
    margin: 14px 0 14px 0;
    padding: 12px 10px 12px 10px;
    }

    #body{
    margin: 0 15px 0 15px;
    }

    p.footer{
    text-align: right;
    font-size: 11px;
    border-top: 5px solid #D0D0D0;
    line-height: 32px;
    padding: 0 10px 0 10px;
    margin: 20px 0 0 0;
    }

    #container{
    margin: 10px;
    border: 5px solid #D0D0D0;
    -webkit-box-shadow: 0 0 8px #D0D0D0;
    }














    Product ID:
    Product Name:
    Child Category:
    Order Number:
    Brand:
    Mfg Number:
    Product Description:





    Loading data from server




    Product ID:
    Product Name:
    Child Category:
    Order Number:
    Brand:
    Mfg Number:
    Product Description:









    Page rendered in {elapsed_time} seconds | version 0.1.0





    [/code]


    Any help is more than appreciated. Ive been racking my brain trying to figure this simple issue out with no avail. Thanks again for all your help!!!!!
  • numberonenumberone Posts: 86Questions: 0Answers: 0
    instead
    [code]
    $array = array('productName' => '$searchCritera%', 'childCategoryID' => '$childCategoryID');
    $this->datatables->where($array);
    [/code]

    use:
    [code]
    $this->datatables->where('childCategoryID', $childCategoryID);
    $this->datatables->like('productName', $searchCritera);
    [/code]

    or at least use double quotes for variables;
    [code]
    $array = array('productName' => "$searchCritera%", 'childCategoryID' => "$childCategoryID");
    $this->datatables->where($array);
    [/code]
  • baytesbaytes Posts: 11Questions: 0Answers: 0
    Numberone,

    Thanks again for your reply, this works and doesn't produce any errors in firebug however the grid displays that there is no data in the table. I'm completly stuck on this and can't figure out how to get past it. If firebug isn't producing any errors and the json request shows no rows or no data what else could be the issue. I'm thinking it has to be something with the way the sql statement is getting formed. Any other thoughts as to why?

    Sorry to keep bugging you
  • numberonenumberone Posts: 86Questions: 0Answers: 0
    "sAjaxSource": "getdatabyajax",

    is your ajax source right ? Be sure your source is like "class/method"

    Open your ajax source in your browser, it should return a json output.
  • baytesbaytes Posts: 11Questions: 0Answers: 0
    Yes, it does return JSON data and the sAjaxSource is set correctly.
    What else could cause this?
  • baytesbaytes Posts: 11Questions: 0Answers: 0
    The JSON data that is returned is as follows:

    {"sEcho":0,"iTotalRecords":0,"iTotalDisplayRecords":0,"aaData":[],"sColumns":"productID,productName,childCategoryName,childCategoryID,orderNum,brand,mfgNum,productDescription"}
This discussion has been closed.