สอบถามว่า ต้องเขียน Code อย่างไร จึงจะกำหนดช่อง Excel ที่จะกรอก แบบกรอกบางช่อง ตามตัวอย่างใน link http://niik.i
ถาม-ตอบ แนะนำไอเดียว โค้ดตัวอย่าง แนวทาง วิธีแก้ปัญหา สอบถามว่า ต้องเขียน Code อย่างไร จึงจะกำหนดช่อง Excel ที่จะกรอก แบบกรอกบางช่อง ตามตัวอย่างใน link http://niik.i
สอบถามว่า ต้องเขียน Code อย่างไร จึงจะกำหนดช่อง Excel ที่จะกรอก แบบกรอกบางช่อง ตามตัวอย่างใน link http://niik.i
สอบถามว่า ต้องเขียน Code อย่างไร จึงจะกำหนดช่อง Excel ที่จะกรอกที่ Input Form แบบกรอกบางช่อง เช่น กรอกที่ช่อง <input type="text" id="selectcell2" name="selectcell2"> แต่ไม่กรอกที่ช่อง <input type="text" id="selectcell1" name="selectcell1"> ตามตัวอย่างใน link http://niik.in/que_3011_6720 ได้ ซึ่งเจอปัญหา เมื่อ กรอกบางช่อง เช่น กรอกที่ช่อง <input type="text" id="selectcell2" name="selectcell2"> แต่ไม่กรอกที่ช่อง <input type="text" id="selectcell1" name="selectcell1"> แล้วเจอ Javascript Error ตามด้านล่างครับ
Screenshot ที่กรอกบางช่อง เช่น กรอกที่ช่อง <input type="text" id="selectcell2" name="selectcell2"> แต่ไม่กรอกที่ช่อง <input type="text" id="selectcell1" name="selectcell1"> ครับ

ชุดไฟล์ + Code ทั้งหมดครับ
1. excelimport.php
2. read_excel.php
1 2 3 4 5 6 | Uncaught SyntaxError: Unexpected end of input at Object.<anonymous> (excelimport.php:52) at l (jquery.min.js:2) at Object.fireWith [as resolveWith] (jquery.min.js:2) at T (jquery.min.js:2) at XMLHttpRequest.r (jquery.min.js:2) |
Screenshot ที่กรอกบางช่อง เช่น กรอกที่ช่อง <input type="text" id="selectcell2" name="selectcell2"> แต่ไม่กรอกที่ช่อง <input type="text" id="selectcell1" name="selectcell1"> ครับ

ชุดไฟล์ + Code ทั้งหมดครับ
1. excelimport.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 | <!DOCTYPE html> <html> <head> <meta charset= "UTF-8" > <title>Document</title> <style> h2,h4 {display: inline;} </style> </head> <body> <form action= "" method= "post" enctype= "multipart/form-data" name= "myform1" id= "myform1" > <details> <summary>กรอกช่อง Excel ที่ต้องการจะเลือก (กรอกเป็นตัวพิมพ์ใหญ่เท่านั้น เช่น A1)<br>(หากไม่มีช่อง Excel ที่ต้องการจะเลือก ให้กรอกเป็นตัวอักษรอะไรก็ได้ 1 ตัว)</b></summary> <br><h4 for = "selectcell1" >ช่อง Excel ที่ต้องการจะเลือกในส่วน First name : </h4><input type= "text" id= "selectcell1" name= "selectcell1" ><br><br> <h4 for = "selectcell2" >ช่อง Excel ที่ต้องการจะเลือกในส่วน Last name : </h4><input type= "text" id= "selectcell2" name= "selectcell2" > </details><br> <h2 for = "myfile1" >Select files : </h2><input type= "file" name= "excelFile" id= "excelFile" /><br><br> <h2 for = "fname" >First name : </h2><input type= "text" id= "fname" name= "fname" ><br><br> <h2 for = "lname" >Last name : </h2><input type= "text" id= "lname" name= "lname" ><br><br> <input type= "submit" name= "btnSubmit" id= "btnSubmit" value= "Submit" /> </form> <script type= "text/javascript" > $( function (){ // เมื่อฟอร์มการเรียกใช้ evnet submit ข้อมูล $( "#excelFile" ).on( "change" , function (e){ e.preventDefault(); // ปิดการใช้งาน submit ปกติ เพื่อใช้งานผ่าน ajax // เตรียมข้อมูล form สำหรับส่งด้วย FormData Object var formData = new FormData($( "#myform1" )[0]); // ส่งค่าแบบ POST ไปยังไฟล์ read_excel.php รูปแบบ ajax แบบเต็ม $.ajax({ url: 'read_excel.php' , type: 'POST' , data: formData, /*async: false,*/ cache: false, contentType: false, processData: false }).done( function (data){ console.log(data); // ทดสอบแสดงค่า ดูผ่านหน้า console /* การใช้งาน console log เพื่อ debug javascript ใน chrome firefox และ ie https://www.ninenik.com/content.php?arti_id=692 via @ninenik */ $( "#fname" ).val( eval ( "data." + $( "#selectcell1" ).val())); $( "#lname" ).val( eval ( "data." + $( "#selectcell2" ).val())); }); }); }); </script> </body> </html> |
2. read_excel.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 | <?php header( "Content-type:application/json; charset=UTF-8" ); header( "Cache-Control: no-store, no-cache, must-revalidate" ); header( "Cache-Control: post-check=0, pre-check=0" , false); /** Error reporting */ error_reporting (E_ALL); ini_set ( 'display_errors' , TRUE); ini_set ( 'display_startup_errors' , TRUE); date_default_timezone_set( 'Asia/Bangkok' ); require_once ( "PHPExcel/Classes/PHPExcel.php" ); ?> <?php if (isset( $_FILES [ 'excelFile' ][ 'name' ]) && $_FILES [ 'excelFile' ][ 'name' ]!= "" ){ $tmpFile = $_FILES [ 'excelFile' ][ 'tmp_name' ]; $fileName = $_FILES [ 'excelFile' ][ 'name' ]; // เก็บชื่อไฟล์ $_fileup = $_FILES [ 'excelFile' ]; $info = pathinfo ( $fileName ); $allow_file = array ( "csv" , "xls" , "xlsx" ); /* print_r($info); // ข้อมูลไฟล์ print_r($_fileup);*/ if ( $fileName != "" && in_array( $info [ 'extension' ], $allow_file )){ // อ่านไฟล์จาก path temp ชั่วคราวที่เราอัพโหลด $objPHPExcel = PHPExcel_IOFactory::load( $tmpFile ); // ดึงข้อมูลของแต่ละเซลในตารางมาไว้ใช้งานในรูปแบบตัวแปร array $cell_collection = $objPHPExcel ->getActiveSheet()->getCellCollection(); // วนลูปแสดงข้อมูล $v =1; $json_data = array (); foreach ( $cell_collection as $cell ) { // ค่าสำหรับดูว่าเป็นคอลัมน์ไหน เช่น A B C .... $column = $objPHPExcel ->getActiveSheet()->getCell( $cell )->getColumn(); // คำสำหรับดูว่าเป็นแถวที่เท่าไหร่ เช่น 1 2 3 ..... $row = $objPHPExcel ->getActiveSheet()->getCell( $cell )->getRow(); // ค่าของข้อมูลในเซลล์นั้นๆ เช่น A1 B1 C1 .... $data_value = $objPHPExcel ->getActiveSheet()->getCell( $cell )->getValue(); // เท่านี้เราก็สามารถแสดงข้อมูลจากการอ่านไฟล์ได้แล้ว และสามารถนำข้อมูลเหล่านี้ // ทำการบันทักลงฐานข้อมูล หรือแสดงได้เลย $json_data [ "$column$row" ] = $data_value ; // echo $v." ---- ".$data_value."<br>"; $v ++; } // แปลง array เป็นรูปแบบ json string if (isset( $json_data )){ $json = json_encode( $json_data ); if (isset( $_GET [ 'callback' ]) && $_GET [ 'callback' ]!= "" ){ echo $_GET [ 'callback' ]. "(" . $json . ");" ; } else { echo $json ; } } } } ?> |
3. excelimport.xlsx - http://doanga2007.github.io/excelimport.xlsx
4. PHPExcel ซึ่งโหลดจากเว็บ https://github.com/PHPOffice/PHPExcel/ ซึ่งมีโครงสร้างของไฟล์ทั้งหมด ดังนี้ครับ



คำแนะนำ และการใช้งาน
สมาชิก กรุณา ล็อกอินเข้าระบบ เพื่อตั้งคำถามใหม่ หรือ ตอบคำถาม สมาชิกใหม่ สมัครสมาชิกได้ที่ สมัครสมาชิก
- ถาม-ตอบ กรุณา ล็อกอินเข้าระบบ
ความคิดเห็นที่
1
ให้จำเป็นแนวทางเสมอว่า เวลาจะใช้ค่าจากตัวแปร หรือข้อมูลใดๆ เราจำเป็นต้อง
สร้างเงื่อนไขการตรวจสอบ ค่านั้นๆ ก่อนเรียกใช้ ไม่อย่างงั้น มีโอกาสและเป็นไปได้มาก
ที่เราจะอ้างอิงค่า ที่ไม่มีการกำหนด ลักษณะ อยู่ดีๆ เราก็เรียกใช้งานตัวแปร หรือค่านั้นๆ ทั้งที่ไม่มี
ค่าหรือการกำหนดเกิดขึ้น
คล้ายๆ กับรูปแบบการตรวจสอบฟอร์ม ที่เราต้องตราวจสอบข้อมูลจากฟอร์มก่อนเรียกใช้งาน
และหลักสำคัญการโปรแกรมมิ้ง คือการ ควบคุมทิศทางการทำงาน โดยเฉพาะ if else
แนวทางอยางง่าย เราก็ใช้ if ถ้ามีข้อมูล หรือข้อมูลไม่เทากับว่าง
1 2 3 | if ($( "#selectcell1" ).val() != "" ){ $( "#fname" ).val(eval( "data." + $( "#selectcell1" ).val())); } |
จริงๆ การตรวจสอบ ก็ขึ้นกับเราด้วยว่าต้องการระดับไหน เช่น ถ้าหาก ตรวจสอบถึงชนิดตัวแปร object
ก็อาจจะเพิ่มเติม เงื่อนไขเข้าไปอีก เช่น
1 2 3 4 5 6 | if ($( "#selectcell1" ).val() != "" ){ var dataOjb = eval( "data." + $( "#selectcell1" ).val()); if ( typeof (dataOjb) !== 'undefined' ){ $( "#fname" ).val(dataOjb); } } |
เงื่อนการตรวจสอบความถูกต้องของข้อมูลมีคล้ายๆ กันในทุกๆ ภาษา ลองดู ลิ้งค์นี้เป็นแนวทาง http://niik.in/que_2798

ความคิดเห็นที่
2
ขอบคุณครับ สำหรับคำตอบ Code ใช้งานได้แล้ว ซึ่งปรับปรุงเป็นฉบับของตัวเอง ซึ่งรวมถึงรองรับไม่ว่าจะกรอกเป็นตัวอักษรขนาดไหน ก็จะกลายเป็นตัวพิมพ์ใหญ่เสมอด้วย ได้ดังนี้ครับ
1. excelimport.php
2. read_excel.php
1. excelimport.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 | <!DOCTYPE html> <html> <head> <meta charset= "UTF-8" > <title>Document</title> <style> h2,h4 {display: inline;} </style> </head> <body> <form action= "" method= "post" enctype= "multipart/form-data" name= "myform1" id= "myform1" > <details> <summary>กรอกช่อง Excel ที่ต้องการจะเลือก</summary> <br><h4 for = "selectcell1" >ช่อง Excel ที่ต้องการจะเลือกในส่วน First name : </h4><input type= "text" id= "selectcell1" name= "selectcell1" ><br><br> <h4 for = "selectcell2" >ช่อง Excel ที่ต้องการจะเลือกในส่วน Middle name : </h4><input type= "text" id= "selectcell2" name= "selectcell2" ><br><br> <h4 for = "selectcell3" >ช่อง Excel ที่ต้องการจะเลือกในส่วน Last name : </h4><input type= "text" id= "selectcell3" name= "selectcell3" > </details><br> <h2 for = "myfile1" >Select files : </h2><input type= "file" name= "excelFile" id= "excelFile" /><br><br> <h2 for = "fname" >First name : </h2><input type= "text" id= "fname" name= "fname" ><br><br> <h2 for = "lname" >Middle name : </h2><input type= "text" id= "lname" name= "lname" ><br><br> <h2 for = "lname" >Last name : </h2><input type= "text" id= "mname" name= "mname" ><br><br> <input type= "submit" name= "btnSubmit" id= "btnSubmit" value= "Submit" /> </form> <script type= "text/javascript" > $( function (){ // เมื่อฟอร์มการเรียกใช้ evnet submit ข้อมูล $( "#excelFile" ).on( "change" , function (e){ e.preventDefault(); // ปิดการใช้งาน submit ปกติ เพื่อใช้งานผ่าน ajax // เตรียมข้อมูล form สำหรับส่งด้วย FormData Object var formData = new FormData($( "#myform1" )[0]); // ส่งค่าแบบ POST ไปยังไฟล์ read_excel.php รูปแบบ ajax แบบเต็ม $.ajax({ url: 'read_excel.php' , type: 'POST' , data: formData, /*async: false,*/ cache: false, contentType: false, processData: false }).done( function (data){ console.log(data); // ทดสอบแสดงค่า ดูผ่านหน้า console /* การใช้งาน console log เพื่อ debug javascript ใน chrome firefox และ ie https://www.ninenik.com/content.php?arti_id=692 via @ninenik */ $( '#selectcell1' ).val($( '#selectcell1' ).val().toUpperCase()); $( '#selectcell2' ).val($( '#selectcell2' ).val().toUpperCase()); $( '#selectcell3' ).val($( '#selectcell3' ).val().toUpperCase()); if ($( "#selectcell1" ).val() != "" ){ $( "#fname" ).val( eval ( "data." + $( "#selectcell1" ).val())); } if ($( "#selectcell2" ).val() != "" ){ $( "#lname" ).val( eval ( "data." + $( "#selectcell2" ).val())); } if ($( "#selectcell3" ).val() != "" ){ $( "#mname" ).val( eval ( "data." + $( "#selectcell3" ).val())); } }); }); }); </script> </body> </html> |
2. read_excel.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 | <?php header( "Content-type:application/json; charset=UTF-8" ); header( "Cache-Control: no-store, no-cache, must-revalidate" ); header( "Cache-Control: post-check=0, pre-check=0" , false); /** Error reporting */ error_reporting (E_ALL); ini_set ( 'display_errors' , TRUE); ini_set ( 'display_startup_errors' , TRUE); date_default_timezone_set( 'Asia/Bangkok' ); require_once ( "PHPExcel/Classes/PHPExcel.php" ); ?> <?php if (isset( $_FILES [ 'excelFile' ][ 'name' ]) && $_FILES [ 'excelFile' ][ 'name' ]!= "" ){ $tmpFile = $_FILES [ 'excelFile' ][ 'tmp_name' ]; $fileName = $_FILES [ 'excelFile' ][ 'name' ]; // เก็บชื่อไฟล์ $_fileup = $_FILES [ 'excelFile' ]; $info = pathinfo ( $fileName ); $allow_file = array ( "csv" , "xls" , "xlsx" ); /* print_r($info); // ข้อมูลไฟล์ print_r($_fileup);*/ if ( $fileName != "" && in_array( $info [ 'extension' ], $allow_file )){ // อ่านไฟล์จาก path temp ชั่วคราวที่เราอัพโหลด $objPHPExcel = PHPExcel_IOFactory::load( $tmpFile ); // ดึงข้อมูลของแต่ละเซลในตารางมาไว้ใช้งานในรูปแบบตัวแปร array $cell_collection = $objPHPExcel ->getActiveSheet()->getCellCollection(); // วนลูปแสดงข้อมูล $v =1; $json_data = array (); foreach ( $cell_collection as $cell ) { // ค่าสำหรับดูว่าเป็นคอลัมน์ไหน เช่น A B C .... $column = $objPHPExcel ->getActiveSheet()->getCell( $cell )->getColumn(); // คำสำหรับดูว่าเป็นแถวที่เท่าไหร่ เช่น 1 2 3 ..... $row = $objPHPExcel ->getActiveSheet()->getCell( $cell )->getRow(); // ค่าของข้อมูลในเซลล์นั้นๆ เช่น A1 B1 C1 .... $data_value = $objPHPExcel ->getActiveSheet()->getCell( $cell )->getValue(); // เท่านี้เราก็สามารถแสดงข้อมูลจากการอ่านไฟล์ได้แล้ว และสามารถนำข้อมูลเหล่านี้ // ทำการบันทักลงฐานข้อมูล หรือแสดงได้เลย $json_data [ "$column$row" ] = $data_value ; // echo $v." ---- ".$data_value."<br>"; $v ++; } // แปลง array เป็นรูปแบบ json string if (isset( $json_data )){ $json = json_encode( $json_data ); if (isset( $_GET [ 'callback' ]) && $_GET [ 'callback' ]!= "" ){ echo $_GET [ 'callback' ]. "(" . $json . ");" ; } else { echo $json ; } } } } ?> |
3. excelimport.xlsx - http://doanga2007.github.io/excelimport.xlsx
4. PHPExcel ซึ่งโหลดจากเว็บ https://github.com/PHPOffice/PHPExcel/ ซึ่งมีโครงสร้างของไฟล์ทั้งหมด ดังนี้ครับ

Screenshot ครับ


Screenshot ครับ


ขอบคุณทุกการสนับสนุน
![]()