Script 1 Load data dan menyimpan tanpa AJAX.
<?php
require_once( '../opendb.php' );
require_once( '../setudf.php' );
if( isset( $_POST['btn_load'] ) ) {
if( $_POST['btn_load'] == 'Load File' ) {
$file = "/barang/ambil.txt";
$fp = fopen( $file, "r" );
$data = fread( $fp, filesize( $file ) );
fclose( $fp );
$output = str_replace( "\t|\t", ",", $data );
$output = explode( "\n", $output );
$date_trans = $POST['txt_date_transfer'];
//--- Check Double Data
$double = mysql_query( "SELECT * FROM tb_tes WHERE tgl ='$date_trans'" );
$hasil = mysql_num_rows( $double );
if( $hasil>=1 ) {
$hapus = "DELETE FROM tb_tes WHERE tgl = '$date_trans'";
mysql_query( $hapus );
} else {
foreach($output as $var) {
$tmp = explode(",", $var);
//-- Fields yang diambil dari file *.txt
$tanggal = $tmp[0];
$id = $tmp[1];
$remaks = $tmp[2];
$sql = "INSERT INTO tb_tes SET tgl='$tanggal', mem_id='$id', remaks='$remaks'";
$trans = mysql_query($sql);
if( $trans )
echo 'Proses transfer ok! :)'.date_db_to_normal( $date_trans );
else
echo 'Proses transfer gagal! :(';
}
}
}
}
?>
<link type="text/css" rel="stylesheet" href="/barang/css/radius.css" media="screen"/>
<div id="Radius_1">
<form enctype="multipart/form-data" method="post">
<table cellpadding="0" cellspacing="0" width="100%">
<tr>
<th height="25" colspan="3">:: Tes Transfer Data ::</th>
</tr>
<tr>
<td>Date Transfer :
<input type="text"
id="txt_date_transfer"
name="txt_date_transfer"
maxlength="10"
size="8"
value="<?php echo date('d/m/Y');?>"/>
</td>
<td>File Location :
<input type="hidden"
name="MAX_FILE_SIZE"
value="2000000"/>
<input type="file"
name="nama_file"/>
</td>
</tr>
<tr>
<td colspan="2" align="left">
<input type="submit"
name="btn_load"
id="btn_load"
value="Load File"/>
</td>
</tr>
</table>
</form>
</div>
Script 2 Load data dan menyimpan menggunakan AJAX.
<link type="text/css" rel="stylesheet" href="/barang/css/radius.css" media="screen"/>
<div id="Radius_1">
<form enctype="multipart/form-data" method="post">
<table cellpadding="0" cellspacing="0" width="100%">
<tr>
<th height="25" colspan="3">:: Tes Transfer Data ::</th>
</tr>
<tr>
<td>Date Transfer :
<input type="text"
id="txt_date_transfer"
name="txt_date_transfer"
maxlength="10"
size="9"
value="<?php echo date('d/m/Y');?>"/>
</td>
<td>File Location :
<input type="hidden"
name="MAX_FILE_SIZE"
value="2000000"/>
<input type="file"
name="nama_file"
value="Browse"/>
</td>
<td>
<input type="submit"
name="btn_load"
id="btn_load"
value="Load File"
onclick="Save_n_transfer_data(); return false;"/>
</td>
</tr>
<tr>
<td colspan="3" id="_show_progress" align="center"> </td>
</tr>
</table>
</form>
</div>
ajax.js
var sendReq = getXmlHttpRequestObject(), _save_n_trans='_show_progress';
function getXmlHttpRequestObject() {
if (window.XMLHttpRequest) {
return new XMLHttpRequest();
} else if(window.ActiveXObject) {
return new ActiveXObject("Microsoft.XMLHTTP");
} else {
alert('Status: Cound not create XmlHttpRequest Object. Consider upgrading your browser.');
}
}
function Save_n_transfer_data() {
document.getElementById( _save_n_trans ).innerHTML = '<img src="/barang/skin/proses_load.gif">';
if ( sendReq.readyState == 4 || sendReq.readyState == 0 ) {
sendReq.open( "POST", "/barang/tes/transfer_data_.php", true );
sendReq.setRequestHeader( "Content-Type", "application/x-www-form-urlencoded" );
sendReq.onreadystatechange=function() {
if ( sendReq.readyState == 4 && sendReq.status == 200 ) {
document.getElementById( 'txt_date_transfer' ).value;
document.getElementById( 'nama_file' ).value;
alert( sendReq.responseText );
document.getElementById( _save_n_trans= ).innerHTML ='';
}
}
var param = 'txt_date_transfer=' + document.getElementById( 'txt_date_transfer' ).value;
param += '&nama_file=' + document.getElementById( 'nama_file' ).value;
sendReq.send( param );
}
}
transfer_data_.php
<?php
require_once( '../opendb.php' );
require_once( '../setudf.php' );
$date_transfer = date_to_db( $_POST['txt_date_transfer'] );
$filename = $_FILES['nama_file']['name'];
$tmpName = $_FILES['nama_file']['tmp_name'];
$fp = fopen( $tmpName, "r" );
$data = fread( $fp, filesize( $tmpName ) );
fclose( $fp );
$output = str_replace( "\t|\t", ",", $data );
$output = explode( "\n", $output );
//--- Check Double Data ----
$double = mysql_query( "SELECT * FROM tb_tes WHERE tgl ='$date_transfer'" );
$hasil = mysql_num_rows( $double );
if( $hasil>=1 ) {
$hapus = "DELETE FROM tb_tes WHERE tgl = '$date_transfer'";
mysql_query( $hapus );
} else {
foreach($output as $var) {
$tmp = explode(",", $var);
//-- Fields yang Diambil Dari File *.txt
$tanggal = $tmp[0];
$id = $tmp[1];
$remaks = $tmp[2];
$sql = "INSERT INTO tb_tes SET tgl='$tanggal', mem_id='$id', remaks='$remaks'";
$trans = mysql_query($sql);
if( $trans )
echo 'Proses transfer Oke! :) '.date_db_to_normal( $date_transfer );
else
echo 'Proses transfer gagal! :(';
}
?>
Jika
Script 1 dijalankan, hasilnya
sempurna, namun jika
Script 2 dijalankan datanya
ga kesimpen ke tabel MySql. Mohon bantuannya ya mas
untuk
script yang kedua...