ผลลัพท์ที่ได้ ดังรูปข้างล่างค่ะ พอดาวน์โหลดเอกสารแล้ว กับได้ ไฟล์ getfile.htm แทนที่จะเป็นเอกสารที่ต้องการ
( code ไม่ได้เขียนเอง กำลังฝึกอ่าน code อยู่ค่ะ รบกวนช่วยดูให้ทีว่าผิดพลาดตรงไหน ขอบคุณค่ะ)
code ค่ะ
select_list.php
โค้ด: เลือกทั้งหมด
<?php
if(!isset($_SESSION)) session_start();
$server = 'localhost';
$user = 'root';
$pass = '';
$dbase = 'sarabun';
$table = 'sites';
$ar_cols = array('dep', 'unit', 'categ', 'links');
$preid = 'slo_';
$col = $ar_cols[0];
$re_html = '';
if(isset($_POST['col']) && isset($_POST['wval'])) {
$col = trim(strip_tags($_POST['col']));
$wval = "'".trim(strip_tags($_POST['wval']))."'";
}
$key = array_search($col, $ar_cols);
$wcol = $key===0 ? $col : $ar_cols[$key-1];
$_SESSION['ar_cols'][$wcol] = isset($wval) ? $wval : $wcol;
$last_key = count($ar_cols)-1;
$next_col = $key<$last_key ? $ar_cols[$key+1] : '';
$conn = new mysqli($server, $user, $pass, $dbase);
mysqli_set_charset($conn, "utf8");
if (mysqli_connect_errno()) { exit('Connect failed: '. mysqli_connect_error()); }
for($i=1; $i<=$key; $i++) {
$ar_where[] = '`'.$ar_cols[$i-1].'`='.$_SESSION['ar_cols'][$ar_cols[$i-1]];
}
$where = isset($ar_where) ? ' WHERE '. implode($ar_where, ' AND ') : '';
$sql = "SELECT DISTINCT `$col` FROM `$table`".$where;
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$onchg = $next_col!==null ? " onchange=\"ajaxReq('$next_col', this.value);\"" : '';
if($col!=$ar_cols[$last_key]) $re_html = $col. ': <select name="'. $col. '"'. $onchg. '><option>- - -</option>';
while($row = $result->fetch_assoc()) {
if($col==$ar_cols[$last_key]) $re_html .= '<br/>'. $row[$col];
else $re_html .= '<option value="'. $row[$col]. '">'. $row[$col]. '</option>';
}
if($col!=$ar_cols[$last_key]) $re_html .= '</select> ';
}
else { $re_html = '0 results'; }
$conn->close();
if($col==$ar_cols[0]) {
for($i=1; $i<count($ar_cols); $i++) {
if($ar_cols[$i]===null) continue;
if($i==$last_key) $re_html .= '<div id="'. $preid.$ar_cols[$i]. '"> </div>';
else $re_html .= '<span id="'. $preid.$ar_cols[$i]. '"> </span>';
}
$re_html .= '<script type="text/javascript">var ar_cols = '.json_encode($ar_cols).'; var preid = "'. $preid. '";</script>';
}
else echo ($col == $ar_cols[$last_key]) ? '<a href="file/getfile.php?link='. $re_html .'">'. $re_html .'</a>' : $re_html;
ajax_select.js
โค้ด: เลือกทั้งหมด
function removeLists(colid) {
var z = 0;
for(var i=1; i<ar_cols.length; i++) {
if(ar_cols[i]==null) continue;
if(ar_cols[i]==colid) z = 1;
if(z==1) document.getElementById(preid+ar_cols[i]).innerHTML = '';
}
}
function ajaxReq(col, wval) {
removeLists(col);
if(wval!='- - -' && wval!='') {
var request = (window.XMLHttpRequest) ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP');
var php_file = 'select_list.php'; // path and name of the php file
var data_send = 'col='+col+'&wval='+wval;
request.open("POST", php_file, true);
document.getElementById(preid+col).innerHTML = 'Loadding...';
request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
request.send(data_send);
request.onreadystatechange = function() {
if (request.readyState==4) {
document.getElementById(preid+col).innerHTML = request.responseText;
}
}
}
}
ไฟล์ getfile.php จะเก็บไว้ในพาธเดียวกับเอกสาร
getfile.php
โค้ด: เลือกทั้งหมด
$server = 'localhost';
$user = 'root';
$pass = '';
$dbase = 'sarabun';
$name = $_GET['links'];
$sql = "SELECT file FROM site WHERE links='$name' LIMIT 1";
$result = mysqli_query($sql);
header("Content-Disposition: attachment; filename=$name");
echo $name;