-1

Im new to ajax,no even learn from others
Now i face a problem with passing id when using ajax
index.php

<table id="employee_grid" class="display" width="100%" cellspacing="0">
                <thead>
                  <tr>
                    <th>N0</th>
                    <th>TICKET ID </th>
                    <th>CATEGORY </th>
                    <th>ORDER TYPE</th>
                    <th>RECEIVED DATE  </th>
                    <th>AGENT/STAFF NAME  </th>
                    <th>EFORM ID</th>
                    <th>LATEST ORDER STATUS</th>
                    <th style="width:170px;">Edit</th>
                  </tr>
                </thead>

</table>

<script type="text/javascript">
$( document ).ready(function() {
$('#employee_grid').DataTable({
             "bProcessing": true,
     "serverSide": true,
     "ajax":{
        url :"run.php", // json datasource

        type: "post",  // type of method  , by default would be get
        "aoColumnDefs" : [
             {
               'bSortable' : false,
               'aTargets' : [3,4]
             }],
        "dataSrc": function (jsonData) {
          for ( var i=0, len=jsonData.data.length ; i<len ; i++ ) {
            jsonData.data[i][1] = jsonData.data[i][1]+jsonData.data[i][2]+jsonData.data[i][3]+jsonData.data[i][4];
            jsonData.data[i][8] = '<a href="cus_details.php"  class="btn btn-primary btn-xs"> View Details </a>   <a href="update_details.php" class="btn btn-primary btn-xs"> Update Status </a>';
          }

          return jsonData.data;
        },
        error: function(){  // error handling code
         // $(".employee-grid-error").html("");
          //$("#employee_grid").append('<tbody class="employee-grid-error">            <tr><th colspan="3">No data found in the server</th></tr></tbody>');
          $("#employee_grid_processing").css("display","none");
        }
      }
    });   
});

</script>

I know i should add some thing a href="cus_details.php?XXXXXXX" ,but i dunno need to put what

run.php

<?php
//include connection file 
include_once("conn.php");

// initilize all variable
$params = $columns = $totalRecords = $data = array();

$params = $_REQUEST;

//define index of column(sorting)
$columns = array( 
    0 => 'NO_ID',
    1 => 'TICKET_ID', 
    2 => 'CAT_C_B',
    3 => 'ORDER_TYPE',
    4 => 'RECEIVED_DATE',
    5 => 'AGENT_STAFF_NAME',
    6 => 'EFORM_ID',
    7 => 'LATEST_ORDER_STATUS'
);

$where = $sqlTot = $sqlRec = "";

// check search value exist(search)
if( !empty($params['search']['value']) ) 
{   
    $where .=" WHERE ";
    $where .=" ( NO_ID LIKE '".$params['search']['value']."%' ";    
    $where .=" OR TICKET_ID LIKE '".$params['search']['value']."%' ";
    $where .=" OR CAT_C_B LIKE '".$params['search']['value']."%' ";
    $where .=" OR ORDER_TYPE LIKE '".$params['search']['value']."%' ";
    $where .=" OR RECEIVED_DATE LIKE '".$params['search']['value']."%' ";
    $where .=" OR AGENT_STAFF_NAME LIKE '".$params['search']['value']."%' ";
    $where .=" OR EFORM_ID LIKE '".$params['search']['value']."%' ";
    $where .=" OR LATEST_ORDER_STATUS LIKE '".$params['search']['value']."%' )";
}

// getting total number records without any search
$sql = "SELECT * FROM `cusinfo` ";
$sqlTot .= $sql;
$sqlRec .= $sql;
//concatenate search sql if value exist
if(isset($where) && $where != '') {

    $sqlTot .= $where;
    $sqlRec .= $where;
}


$sqlRec .=  " ORDER BY ". $columns[$params['order'][0]['column']]."   ".$params['order'][0]['dir']."  LIMIT ".$params['start']." ,".$params['length']." ";

$queryTot = mysqli_query($conn, $sqlTot) or die("database error:". mysqli_error($conn));


$totalRecords = mysqli_num_rows($queryTot);

$queryRecords = mysqli_query($conn, $sqlRec) or die("error to fetch cusinfo data");

//iterate on results row and create new index array of data
while( $row = mysqli_fetch_row($queryRecords) ) { 
    $data[] = $row;
}   

$json_data = array(
        "draw"            => intval( $params['draw'] ),   
        "recordsTotal"    => intval( $totalRecords ),  
        "recordsFiltered" => intval($totalRecords),
        "data"            => $data   // total data array
        );

echo json_encode($json_data);  // send data as json format
?>
NG Miow
  • 57
  • 14
  • What's your exact issue ? – Mayank Pandeyz Apr 06 '17 at 04:04
  • It looks like this is what you are looking for http://php.net/manual/en/reserved.variables.request.php $_GET, $_POST etc.., What you're referring to is a sample GET request. – Sudheesh Singanamalla Apr 06 '17 at 04:07
  • @MayankPandeyz,im asking how to pass user id to another page,just like Sudheesh Singanamalla answer.But i still cant do it,the GET seem like hard to use... – NG Miow Apr 06 '17 at 05:20
  • @SudheeshSinganamalla Singanamalla, thankyou for the ansewer,i will explore it until work – NG Miow Apr 06 '17 at 05:21
  • Possible duplicate of [jQuery Ajax POST example with PHP](http://stackoverflow.com/questions/5004233/jquery-ajax-post-example-with-php) – NG Miow Apr 06 '17 at 05:56

2 Answers2

0

You can pass the parameters as shown in the below example:

$('#employee_grid').DataTable({
  "ajax": {
    "url": "run.php",
    "data": {
        "user_id": 451
    }
  }
} );

Here 'user_id' is the assumed parameter.

MANO
  • 183
  • 1
  • 7
0

You can use data parameter which allow to add additional parameter . For know more about that you could have a look on https://datatables.net/examples/server_side/custom_vars.html

<script type="text/javascript">
$( document ).ready(function() {
$('#employee_grid').DataTable({
             "bProcessing": true,
     "serverSide": true,
     "ajax":{
        url :"run.php", // json datasource

        data:function(d){ //use for pass addtional parameter 
         d.user_id="your user id";
          },

        type: "post",  // type of method  , by default would be get
        "aoColumnDefs" : [
             {
               'bSortable' : false,
               'aTargets' : [3,4]
             }],
        "dataSrc": function (jsonData) {
          for ( var i=0, len=jsonData.data.length ; i<len ; i++ ) {
            jsonData.data[i][1] = jsonData.data[i][1]+jsonData.data[i][2]+jsonData.data[i][3]+jsonData.data[i][4];
            jsonData.data[i][8] = '<a href="cus_details.php"  class="btn btn-primary btn-xs"> View Details </a>   <a href="update_details.php" class="btn btn-primary btn-xs"> Update Status </a>';
          }

          return jsonData.data;
        },
        error: function(){  // error handling code
         // $(".employee-grid-error").html("");
          //$("#employee_grid").append('<tbody class="employee-grid-error">            <tr><th colspan="3">No data found in the server</th></tr></tbody>');
          $("#employee_grid_processing").css("display","none");
        }
      }
    });   
});

</script>
Mithu CN
  • 605
  • 5
  • 11
  • sorry,im not so clear about the code.but thank for answer me – NG Miow Apr 06 '17 at 05:22
  • data:function(d){ //use for pass addtional parameter d.user_id="your user id"; } add this to ajax portion to pass adtional data. For detail you can see documention in click on above link – Mithu CN Apr 06 '17 at 06:41