ส่งค่าจาก form ผ่าน Ajax ไปอีกหน้าเพื่อแสดง datatable

ถาม-ตอบ แนะนำไอเดียว โค้ดตัวอย่าง แนวทาง วิธีแก้ปัญหา ส่งค่าจาก form ผ่าน Ajax ไปอีกหน้าเพื่อแสดง datatable

ส่งค่าจาก form ผ่าน Ajax ไปอีกหน้าเพื่อแสดง datatable
จะส่งค่า id_partner จากหน้า showEachCom.php ไปที่หน้า po-grid-data.php
จากหน้า showEachCom.php ถ้าลอง <? echo $_POST['id_partner'];?> ก็มีค่าขึ้นปกติ แต่จะส่งไปหน้า po-grid-data.php มันไม่ไปให้ค่ะ

หน้า index.php
1
2
3
4
5
6
7
8
9
10
11
<form id="myForm" action="showEachCom.php" method="post">
     
    <input name="name_partner" type="text" id="name_partner"/>
    <input name="id_partner" type="hidden" id="id_partner"/>
    <input type="submit" name="submit" id="submit" value="submit">
    <br/><br/>
    <div id="showdata">
        <?/* if($_POST['submit'])
        {  include('showEachCom.php');} */?>
  </div>
  </form>

หน้า showEachCom.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<script type="text/javascript" src="fancybox/fancybox/jquery.fancybox2.1.5.js"></script>
 
 
            <script type="text/javascript" language="javascript" >
            $(document).ready(function() {
                var dataTable = $('#employee-grid').DataTable( {
                    "processing": true,
                    "serverSide": true,
                    "Paginate": true,
                    "LengthChange": false,
                    "Filter": false,
                    "Info": false,
                    var data2 = "<? echo $_POST['id_partner'];?>";
                    "ajax":{
                        url :"po-grid-data.php", // json datasource
                        type: "post"// method  , by default get
                        data: data2,
                        error: function(){  // error handling
                            $(".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");
                             
                        }
                    },
                    "fnDrawCallback": function () {
                      //alert('test');
                        $(".example1").fancybox({
                        'maxWidth'  : 800,
                        'maxHeight' : 600,
                        'fitToView' : false,
                        'width'     : '100%',
                        'height'        : '100%',
                        'autoSize'  : false,
                        'closeClick'    : false,
                        'openEffect'    : 'none',
                        'closeEffect'   : 'none',
                        'type'          : 'iframe'
                                });
                                $(".example2").attr("rel","gallery").fancybox({
                                 
                            /*'width'           : '50%',
                            'height'        : '50%',
                            'autoScale'     : false,
                            'transitionIn'      : 'none',
                            'transitionOut'     : 'none',
                            'type'          : 'iframe',*/
    ////////print//////////
    'afterShow' : function(){
    var win=null;
    var content = $('.fancybox-inner');
    $('.fancybox-wrap')
    // append print button
    .append('<div id="fancy_print"></div>')
    // use .on() in its delegated form to bind a click to the print button event for future elements
    .on("click", "#fancy_print", function(){
      win = window.open("width=200,height=200");
      self.focus();
      win.document.open();
      win.document.write('<' 'html' '><' 'head' '><' 'style' '>');
      win.document.write('body, td { font-family: Verdana; font-size: 10pt;}');
      win.document.write('<' '/' 'style' '><' '/' 'head' '><' 'body' '>');
      win.document.write(content.html());
      win.document.write('<' '/' 'body' '><' '/' 'html' '>');
      win.document.close();
      win.print();
      win.close();
    }); // on
  } //  afterShow
                        });
                     
                    }
                } );
            } );
        </script>

หน้า po-grid-data.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php
/* Database connection start */
$servername = "localhost";
$username = "root";
$password = "4321";
$dbname = "dbsalesl";
 
$conn = mysqli_connect($servername, $username, $password, $dbname) or die("Connection failed: " . mysqli_connect_error());
mysqli_set_charset($conn,"utf8");
/* Database connection end */
 
$id_partner=$_POST['data2'];
echo $id_partner;
// storing  request (ie, get/post) global array to a variable 
$requestData= $_REQUEST;
 
$columns = array(
// datatable column index  => database column name
    0 =>'ptype',
    1 => 'com',
    2 =>'date',
    3 =>'po_no',
    4 => 'qt_no',
    5 =>'name_partner',
    6 => 'all_file',
    7 => 'Ename'
);
 
// getting total number records without any search
$sql = "SELECT a.id AS idPO, a.com, a.date, a.po_no, a.qt_no, c.name_partner, d.Ename, d.Enname, a.ptype, GROUP_CONCAT( b.file ) AS all_file
FROM po a
LEFT JOIN partner c ON a.id_partner = c.id_partner
LEFT JOIN employee d ON a.id_author = d.Eid
LEFT JOIN po_detail b ON a.id = b.id_po
WHERE c.id_partner = '$id_partner' AND
b.id_po IS NOT NULL
GROUP BY a.id";
$query=mysqli_query($conn, $sql) or die("po-grid-data.php: get employees");
$totalData = mysqli_num_rows($query);
$totalFiltered = $totalData// when there is no search parameter then total number rows = total number filtered rows.
 
 
$sql = "SELECT a.id AS idPO, a.com, a.date, a.po_no, a.qt_no, c.name_partner, d.Ename, d.Enname, a.ptype, GROUP_CONCAT( b.file ) AS all_file
FROM po a
LEFT JOIN partner c ON a.id_partner = c.id_partner
LEFT JOIN employee d ON a.id_author = d.Eid
LEFT JOIN po_detail b ON a.id = b.id_po
WHERE c.id_partner = '$id_partner' AND
b.id_po IS NOT NULL";
if( !empty($requestData['search']['value']) ) {   // if there is a search parameter, $requestData['search']['value'] contains search parameter
    $sql.=" AND ( ptype LIKE '%".$requestData['search']['value']."%' ";   
    $sql.=" OR com LIKE '%".$requestData['search']['value']."%' ";
    $sql.=" OR date LIKE '%".$requestData['search']['value']."%' ";
    $sql.=" OR po_no LIKE '%".$requestData['search']['value']."%' ";
    $sql.=" OR qt_no LIKE '%".$requestData['search']['value']."%' ";
    $sql.=" OR name_partner LIKE '%".$requestData['search']['value']."%' ";
    $sql.=" OR Ename LIKE '%".$requestData['search']['value']."%' )";
     
}
$sql.=" AND 1=1 GROUP BY a.id";
$query=mysqli_query($conn, $sql) or die("po-grid-data.php: get employees");
$totalFiltered = mysqli_num_rows($query); // when there is a search parameter then we have to modify total number filtered rows as per search result.
$sql.=" ORDER BY ". $columns[$requestData['order'][0]['column']]."   ".$requestData['order'][0]['dir']."  LIMIT ".$requestData['start']." ,".$requestData['length']."   ";
/* $requestData['order'][0]['column'] contains colmun index, $requestData['order'][0]['dir'] contains order such as asc/desc  */   
$query=mysqli_query($conn, $sql) or die("po-grid-data.php: get employees");
 
$data = array();
while( $row=mysqli_fetch_array($query) ) {  // preparing an array
    $idPO= $row["idPO"];
    $nestedData=array();
    if($row['ptype']=='po') { $nestedData[] = "PO";}
    if($row['ptype']=='ใบเคลม') { $nestedData[] = "ใบเคลม";}
     
    if($row['com']=='CC') { $nestedData[] = "<div style='color: red;opacity:0.0;'>$row[com]</div><img src='images/central.png' height='40px'/>";}
            if($row['com']=='CL') { $nestedData[] = "<div style='color: red;opacity:0.0;'>$row[com]</div><img src='images/clwheel.png' height='40px'/>";}
            if($row['com']=='DS') { $nestedData[] = "<div style='color: red;opacity:0.0;'>$row[com]</div><img src='images/dreamshop.png' height='40px'/>";}
 
    list($Year,$month,$day) = explode("-",$row['date']);
    $nestedData[] = (int)$day."/".(int)$month."/".($Year*1 + 543);
     
    $nestedData[] = $row["po_no"];
    if($row['qt_no']!= "ประวัติลูกค้า" && $row['qt_no']!= "ข้อมูลSale"){
    if (strpos($row['qt_no'],'Rev'))
    { $nestedData[] = "<a class='example1' href='viewEdit$row[com].php?idx=$row[qt_no]'>$row[qt_no]</a>";}
    else{ $nestedData[] = "<a class='example1' href='view$row[com].php?idx=$row[qt_no]'>$row[qt_no]</a>";}
    } else { $nestedData[] = $row['qt_no'];}
    $nestedData[] = $row["name_partner"];
    /*$nestedData[] = $row["all_file"];*/
    $array = explode(',', $row["all_file"]);
     $strFile = '';
foreach(array_unique($array ) as $val){
list($name,$type) = explode(".",$val);
if ($type =='pdf') { $strFile .= "<a class='example1' href='images/po/$val'><img src='images/po/PDF-icon.png' align='left' width='60px'/></a>";}
else $strFile .= "<a class='example1' href='preview.php?type=po&images=$val'><img src='images/po/$val' width='60px'/></a>";}
}
$nestedData[]=$strFile;
    /*list($name,$type) = explode(".",$row['file']);
     if ($type =='pdf') { $nestedData[] = "<a class='example1' href='images/po/$row[file]'><img src='images/po/PDF-icon.png' align='left' width='60px'/></a>";}
     else {  $nestedData[] = "<a class='example1' href='preview.php?type=po&images=$row[file]'><img src='images/po/$row[file]' width='60px'/></a>";}
    */$nestedData[] = $row['Ename']." (".$row['Enname'].")";
    $nestedData[] = "<a class='example1' href='checkEdit.php?idx=$idPO&po=po' target='_blank'><img src='../images/edit.png' width='30' title='แก้ไข' border='0' ></a>";
    $data[] = $nestedData;
}
 
 
 
$json_data = array(
            "draw"            => intval( $requestData['draw'] ),   // for every request/draw by clientside , they send a number as a parameter, when they recieve a response/data they first check the draw number, so we are sending same number in draw.
            "recordsTotal"    => intval( $totalData ),  // total number of records
            "recordsFiltered" => intval( $totalFiltered ), // total number of records after searching, if there is no searching then totalFiltered = totalData
            "data"            => $data   // total data array
            );
 
echo json_encode($json_data);  // send data as json format
 
?>


Saranya2532 13-09-2017 09:52:03

คำแนะนำ และการใช้งาน

สมาชิก กรุณา ล็อกอินเข้าระบบ เพื่อตั้งคำถามใหม่ หรือ ตอบคำถาม สมาชิกใหม่ สมัครสมาชิกได้ที่ สมัครสมาชิก


  • ถาม-ตอบ กรุณา ล็อกอินเข้าระบบ
  • เปลี่ยน


    ( หรือ เข้าใช้งานผ่าน Social Login )

 ความคิดเห็นที่ 1
1
2
3
4
5
6
7
8
9
10
11
12
var data2 = "<? echo $_POST['id_partner'];?>";
"ajax":{
    url :"po-grid-data.php", // json datasource
    type: "post"// method  , by default get
    data: data2,
    error: function(){  // error handling
        $(".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");
          
    }
},
 
เอาโค้ดไปแทรกในลักษณะแบบนี้น่าจะ error หรือป่าว
ลองใช้แบบนี้ดู
 
1
2
3
4
5
6
7
"ajax":{
    url :"po-grid-data.php", // json datasource
    type: "post"// method  , by default get
    data: {
        data2:<? echo $_POST['id_partner'];?>
    }
},
 
ส่วนของการจัดการ error ไม่แน่ใจว่าใช้ได้ไหม แต่ปกติ ไม่น่าจะต้องมี เพราะถ้าไม่มีข้อมูล dataTable
ก็จะแสดงข้อความให้เองว่าไม่พบข้อมูล เราไม่ต้องกำหนดอะไร
 
ดูเพิ่มเติม
 


ninenik 13-09-2017
 ความคิดเห็นที่ 2
เหมือนว่ามันไม่ไปให้อ่ะค่ะ
หน้า showEachCom.php
1
2
3
4
5
6
7
8
9
10
11
"ajax":{
                       url :"po-grid-data.php", // json datasource
                       type: "post"// method  , by default get
                       data: { data2: <? echo $_POST['id_partner'];?>  }
                       error: function(){  // error handling
                           $(".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");
                            
                       }
                   },
เป็นเพราะหน้า po-grid-data.php มันมีข้อมูลdata อยู่แล้วหรือเปล่าคะ 
1
2
3
4
5
6
7
8
$json_data = array(
            "draw"            => intval( $requestData['draw'] ),   // for every request/draw by clientside , they send a number as a parameter, when they recieve a response/data they first check the draw number, so we are sending same number in draw.
            "recordsTotal"    => intval( $totalData ),  // total number of records
            "recordsFiltered" => intval( $totalFiltered ), // total number of records after searching, if there is no searching then totalFiltered = totalData
            "data"            => $data   // total data array
            );
 
echo json_encode($json_data);  // send data as json format







saranya2532 14-09-2017 16:48
 ความคิดเห็นที่ 3
ได้แล้วค่ะ หนูส่งเป็นค่า GET ไปแทน
1
2
3
4
5
6
7
8
9
10
"ajax":{
                      url: "po-grid-data.php?id_partner=" <? echo $_POST['id_partner'];?>,// json datasource
                      type: "post"// method  , by default get
                      error: function(){  // error handling
                          $(".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");
                           
                      }
                  },
ขอบคุณนะคะ 




saranya2532 14-09-2017 17:20
1






เว็บไซต์ของเราให้บริการเนื้อหาบทความสำหรับนักพัฒนา โดยพึ่งพารายได้เล็กน้อยจากการแสดงโฆษณา โปรดสนับสนุนเว็บไซต์ของเราด้วยการปิดการใช้งานตัวปิดกั้นโฆษณา (Disable Ads Blocker) ขอบคุณครับ