Load data dengan Ajax

Bella · Sep 7, 2012
PERTANYAAN PERTAMA
<div id="bingkai_1">
	<form name="info_barang_1" method="post">
	<table cellpadding="0" cellspacing="0" width="100%">
    	<tr>
        	<th colspan="2" align="center">Info Penjualan Barang</th>
        </tr>
        <tr>
        	<td>Tanggal :
            	<input type="text"
                       name="txtTanggal"
                       id="txtTanggal"
                       size="8"
                       maxlength="10"
                       value="<?=date('Y-m-d');?>"
                       onblur="LoadJual_1(); return false;"/>
                	
            </td>
            <td>Total Penjualan Rp. :
            	<input type="text"
                        name="txtJual_1"
                       id="txtJual_1"
                       value="0"
                       size="15"
                       style="text-align:right"
                       readonly="readonly" />
            </td>
        </tr>
    </table>
	</form>
</div>



var recReq = getXmlHttpRequestObject();
var showdata_jual_1='txtJual_1'

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 LoadJual_1() {
document.getElementById( showdata_jual_1 ).value = 'Loading ...';
if (recReq.readyState == 4 || recReq.readyState == 0) {
  	recReq.open("GET", '/barang/report/info_jual_1.php', true );
   	recReq.onreadystatechange = function() {
   	if (recReq.readyState == 4 && recReq.status == 200 ) {
		document.getElementById( showdata_jual_1 ).value = recReq.responseText;
   	}
}
recReq.send(null);
}
}



<?php
require_once( '.././opendb.php' );
$tgl = $_POST['txtTanggal'];
$sql = mysql_query( "SELECT SUM( tot_gab ) as tot_jual FROM trs WHERE tgl='$tgl'" );
while( $data = mysql_fetch_array( $sql ) ) {
echo $data['tot_jual'];
}
?>

Permasalahannya pada file PHP, jika variable $tgl saya ganti menjadi :
$tgl = "2012-09-05" pada saat dijalankan gak ada masalah,
namun jika saya ganti Variable $tgl menjadi :
$tgl = $_POST; jika dikalankan, Nilai Totalnya
nggak tampil ... :(




PERTANYAAN KEDUA

<?php if( $_POST['btn_show'] == Show ) { ?>
	  <div id="info_jual"></div>
<? } else { ?>

<div id="bingkai_1">
<form name="info_barang_2" method="post">
<table cellpadding="1" cellspacing="1" width="100%">
	<tr>
		<th>Info Penjualan Barang</th>
        </tr>
        <tr>
        	<td align="center">
            	       <input type="submit"
                       name="btn_show"
                       id="btn_show"
                       value="Show"
                       onclick="LoadJual_2(); return false;" />
		</td>
	</tr>
    </table>
    </form>
</div>
<? } ?>


var recReq = getXmlHttpRequestObject();
var showdata_jual_2='info_jual'

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 LoadJual_2() {
document.getElementById( showdata_jual_2 ).innerHTML = 'Loading ...';
if (recReq.readyState == 4 || recReq.readyState == 0) {
  	recReq.open("GET", '/barang/report/info_jual_2.php', true );
   	recReq.onreadystatechange = function() {
   	if (recReq.readyState == 4 && recReq.status == 200 ) {
		document.getElementById( showdata_jual_2 ).innerHTML = recReq.responseText;
   	}
}
recReq.send(null);
}
}


<?php
require_once( '.././opendb.php' );
$sql = mysql_query( "SELECT SUM( tot_gab ) as tot_jual FROM trs" );
while( $data = mysql_fetch_array( $sql ) ) {
echo $data['tot_jual'];
}
?>

Permasalahan pada pertanyaan Kedua :
Jika tombol Show diklick maka, tombol tersebut akan Hide,
dan akan tampil <div id="info_jual"></div> untuk menampilkan data Total
keseluruhan penjualan ..

Mohon pencerahannya mas,
dan maap jika saya borong semua pertanyaan ...
Silahkan login untuk menjawab!
0
Loading...
Bella · Sep 14, 2012 · 0 Suka · 0 Tidak Suka
mas ellyx...,, mohon pencerahannya dooong ... :(
0
Loading...
Ellyx Christian · Sep 17, 2012 · 0 Suka · 0 Tidak Suka
pertama ubah fungsi LoadJual_1() jadi
function LoadJual_1() {
document.getElementById( showdata_jual_1 ).value = 'Loading ...';
if (recReq.readyState == 4 || recReq.readyState == 0) {
  	recReq.open("POST", '/barang/report/info_jual_1.php', true );
   	recReq.onreadystatechange = function() {
   	if (recReq.readyState == 4 && recReq.status == 200 ) {
		document.getElementById( showdata_jual_1 ).value = recReq.responseText;
   	}
}
recReq.send('txtTanggal='+document.getElementById('txtTanggal').value);
}
}
yang kedua ubah html menjadi
<div id="bingkai_1">
<form name="info_barang_2" method="post">
<table cellpadding="1" cellspacing="1" width="100%">
	<tr>
		<th>Info Penjualan Barang</th>
        </tr>
        <tr>
        	<td align="center">
            	       <input type="submit"
                       name="btn_show"
                       id="btn_show"
                       value="Show"
                       onclick="LoadJual_2(); return false;" />
		</td>
	</tr>
    </table>
    </form>
</div>
<div id="info_jual"></div> <!-- penting -->
lalu ubah fungsi LoadJual_2() jadi
function LoadJual_2() {
document.getElementById( showdata_jual_2 ).innerHTML = 'Loading ...';
if (recReq.readyState == 4 || recReq.readyState == 0) {
  	recReq.open("GET", '/barang/report/info_jual_2.php', true );
   	recReq.onreadystatechange = function() {
   	if (recReq.readyState == 4 && recReq.status == 200 ) {
		document.getElementById( showdata_jual_2 ).innerHTML = recReq.responseText;
		document.getElementById('btn_show').style.display='none';
   	}
}
recReq.send(null);
}
}
0
Loading...
Bella · Sep 18, 2012 · 0 Suka · 0 Tidak Suka
Maap sebelumya mas, untuk pertanyaan yg kedua ( Fungsi LoadJual2() ) udah berhasil mas.
namun untuk Fungsi LoadJual_1() yg belum berhasil mas ... :(
0
Loading...
Ellyx Christian · Sep 30, 2012 · 0 Suka · 0 Tidak Suka
ubah fungsi LoadJaul_1() menjadi
function LoadJual_1() {
document.getElementById( showdata_jual_1 ).value = 'Loading ...';
if (recReq.readyState == 4 || recReq.readyState == 0) {
  	recReq.open("POST", 'info_jual_1.php', true );
  	recReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
   	recReq.onreadystatechange = function() {
   	if (recReq.readyState == 4 && recReq.status == 200 ) {
		document.getElementById( showdata_jual_1 ).value = recReq.responseText;
   	}
}
recReq.send('txtTanggal='+document.getElementById('txtTanggal').value);
}
}
mengirim dengan menthod post butuh tambahkan kode
recReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
0
Loading...
Bella · Oct 1, 2012 · 0 Suka · 0 Tidak Suka
Thanks ya mas ...