<?
mysql_connect('localhost','root','231088') or die("Koneksi gagal");
mysql_select_db('penjualan') or die("Database tidak bisa dibuka");
$jsArray = "var idxhrg = new Array();";
echo "<table width='60%' border='1'>
<tr><td>Nama Baju</td><td>Harga Baju</td></tr>
<tr>
<td><select name='baju' name='baju' onchange='changeValue(this.value)'>
<option value='pilih'>Pilih Baju</option>";
$tampil=mysql_query("SELECT * FROM baju ORDER BY nm_baju");
while($r=mysql_fetch_array($tampil)){
echo "<option value=$r[id]>$r[nm_baju]</option>";
$jsArray .= "idxhrg['" . $r[id] . "'] = {index:'" . addslashes($r['hrg'])."'};";
}
echo " </select>
</td>
<td><input type=text name='hrg' id='hrg'></td>
</tr>";
echo " <tr>
<td><select name='baju' name='baju' onchange='changeValue(this.value)'>
<option value='pilih'>Pilih Baju</option>";
$tampil=mysql_query("SELECT * FROM baju ORDER BY nm_baju");
while($r=mysql_fetch_array($tampil)){
echo "<option value=$r[id]>$r[nm_baju]</option>";
$jsArray .= "idxhrg['" . $r[id] . "'] = {index:'" . addslashes($r['hrg'])."'};";
}
echo " </select>
</td>
<td><input type=text name='hrg' id='hrg1'></td>
</tr>";
echo " <tr>
<td><select name='baju' name='baju' onchange='changeValue(this.value)'>
<option value='pilih'>Pilih Baju</option>";
$tampil=mysql_query("SELECT * FROM baju ORDER BY nm_baju");
while($r=mysql_fetch_array($tampil)){
echo "<option value=$r[id]>$r[nm_baju]</option>";
$jsArray .= "idxhrg['" . $r[id] . "'] = {index:'" . addslashes($r['hrg'])."'};";
}
echo " </select>
</td>
<td><input type=text name='hrg' id='hrg2'></td>
</tr>
</table>";
?>
<script type="text/javascript">
<?php echo $jsArray; ?>
function changeValue(id){
document.getElementById('hrg').value = idxhrg[id].index;
document.getElementById('hrg1').value = idxhrg[id].index;
document.getElementById('hrg2').value = idxhrg[id].index;
};
</script>
-- phpMyAdmin SQL Dump
-- version 2.10.2
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Mar 31, 2013 at 06:09 PM
-- Server version: 5.0.45
-- PHP Version: 5.2.3
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
--
-- Database: `penjualan`
--
-- --------------------------------------------------------
--
-- Table structure for table `baju`
--
CREATE TABLE `baju` (
`id` int(11) NOT NULL auto_increment,
`nm_baju` varchar(30) NOT NULL,
`hrg` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=4 ;
--
-- Dumping data for table `baju`
--
INSERT INTO `baju` VALUES (1, 'Baju MU', 19000);
INSERT INTO `baju` VALUES (2, 'Baju Juventus', 30000);
INSERT INTO `baju` VALUES (3, 'Baju Real Madrid', 35000);
<?
include "koneksi.php";
$jsArray1 = "var idxhrg1 = new Array();\n";
$jsArray2 = "var idxhrg2 = new Array();\n";
$jsArray3 = "var idxhrg3 = new Array();\n";
echo "<table width='60%' border='1'>
<tr><td>Nama Baju</td><td>Harga Baju</td></tr>
<tr>
<td><select name='baju' id='baju1' onchange='document.getElementById(\"hrg1\").value = idxhrg1[this.value]'>
<option value='0'>Pilih Baju</option>";
$tampil=mysql_query("SELECT * FROM baju ORDER BY nm_baju");
while($r=mysql_fetch_array($tampil)){
echo "<option value=$r[id]>$r[nm_baju]</option>";
//$jsArray1 .= "idxhrg1['" . $r[id] . "'] = {hrg1:'" . addslashes($r['hrg'])."'};";
$jsArray1 .= "idxhrg1['" . $r[id] . "'] = '" . addslashes($r['hrg']) . "';\n";
}
echo " </select>
</td>
<td><input type=text name='hrg1' id='hrg1'></td>
</tr>";
echo " <tr>
<td><select name='baju2' id='baju2' onchange='document.getElementById(\"hrg2\").value = idxhrg2[this.value]'>
<option value='0'>Pilih Baju</option>";
$tampil=mysql_query("SELECT * FROM baju ORDER BY nm_baju");
while($r=mysql_fetch_array($tampil)){
echo "<option value=$r[id]>$r[nm_baju]</option>";
//$jsArray2 .= "idxhrg2['" . $r[id] . "'] = {hrg2:'" . addslashes($r['hrg'])."'};";
$jsArray2 .= "idxhrg2['" . $r[id] . "'] = '" . addslashes($r['hrg']) . "';\n";
}
echo " </select>
</td>
<td><input type=text name='hrg2' id='hrg2'></td>
</tr>";
echo " <tr>
<td><select name='baju3' id='baju3' onchange='document.getElementById(\"hrg3\").value = idxhrg3[this.value]'>
<option value='0'>Pilih Baju</option>";
$tampil=mysql_query("SELECT * FROM baju ORDER BY nm_baju");
while($r=mysql_fetch_array($tampil)){
echo "<option value=$r[id]>$r[nm_baju]</option>";
//$jsArray3 .= "idxhrg3['" . $r[id] . "'] = {hrg3:'" . addslashes($r['hrg'])."'};";
$jsArray3 .= "idxhrg3['" . $r[id] . "'] = '" . addslashes($r['hrg']) . "';\n";
}
echo " </select>
</td>
<td><input type=text name='hrg3' id='hrg3'></td>
</tr>
</table>";
?>
<script type="text/javascript">
<?php echo "$jsArray1 $jsArray2 $jsArray3";?>
</script>
<?
include "koneksi.php";
$jsArray1 = "var idxhrg1 = new Array();\n";
$jsArray2 = "var idxhrg2 = new Array();\n";
$jsArray3 = "var idxhrg3 = new Array();\n";
echo "<table width='60%' border='1'>
<tr><td>Nama Baju</td><td>Harga Baju</td></tr>
<tr>
<td><select name='baju' id='baju1' onchange='document.getElementById(\"hrg1\").value = idxhrg1[this.value] ? idxhrg1[this.value] : 0'>
<option value='0'>Pilih Baju</option>";
$tampil=mysql_query("SELECT * FROM baju ORDER BY nm_baju");
while($r=mysql_fetch_array($tampil)){
echo "<option value=$r[id]>$r[nm_baju]</option>";
//$jsArray1 .= "idxhrg1['" . $r[id] . "'] = {hrg1:'" . addslashes($r['hrg'])."'};";
$jsArray1 .= "idxhrg1['" . $r[id] . "'] = '" . addslashes($r['hrg']) . "';\n";
}
echo " </select>
</td>
<td><input type=text name='hrg1' id='hrg1'></td>
</tr>";
echo " <tr>
<td><select name='baju2' id='baju2' onchange='document.getElementById(\"hrg2\").value = idxhrg2[this.value] ? idxhrg2[this.value] : 0'>
<option value='0'>Pilih Baju</option>";
$tampil=mysql_query("SELECT * FROM baju ORDER BY nm_baju");
while($r=mysql_fetch_array($tampil)){
echo "<option value=$r[id]>$r[nm_baju]</option>";
//$jsArray2 .= "idxhrg2['" . $r[id] . "'] = {hrg2:'" . addslashes($r['hrg'])."'};";
$jsArray2 .= "idxhrg2['" . $r[id] . "'] = '" . addslashes($r['hrg']) . "';\n";
}
echo " </select>
</td>
<td><input type=text name='hrg2' id='hrg2'></td>
</tr>";
echo " <tr>
<td><select name='baju3' id='baju3' onchange='document.getElementById(\"hrg3\").value = idxhrg3[this.value] ? idxhrg3[this.value] : 0'>
<option value='0'>Pilih Baju</option>";
$tampil=mysql_query("SELECT * FROM baju ORDER BY nm_baju");
while($r=mysql_fetch_array($tampil)){
echo "<option value=$r[id]>$r[nm_baju]</option>";
//$jsArray3 .= "idxhrg3['" . $r[id] . "'] = {hrg3:'" . addslashes($r['hrg'])."'};";
$jsArray3 .= "idxhrg3['" . $r[id] . "'] = '" . addslashes($r['hrg']) . "';\n";
}
echo " </select>
</td>
<td><input type=text name='hrg3' id='hrg3'></td>
</tr>
</table>";
?>
<script type="text/javascript">
<?php echo "$jsArray1 $jsArray2 $jsArray3";?>
</script>
<?
include "koneksi.php";
echo "<script type='text/javascript'>";
echo "function startCalculate(){";
echo "interval=setInterval('Calculate()',10)";
echo "}";
echo "function Calculate(){\n";
echo "var total=0;\n";
for ($i=1; $i<=20; $i++){
echo "document.getElementsByName('Sub_totalharga[$i]')[0].value=(document.getElementsByName('harga[$i]')[0].value*document.getElementsByName('jumlah[$i]')[0].value).toFixed(0);\n";
echo "total+=parseFloat(document.getElementsByName('Sub_totalharga[$i]')[0].value);\n";
}
echo "document.getElementsByName('totalongkos_ptgbhn')[0].value=total.toFixed(0)\n";
echo "}";
echo "function stopCalc(){";
echo "clearInterval(interval)";
echo "}";
echo "</script>";
$jsArray1 = "var idxhrg1 = new Array();\n";
echo "<form name='form3' id='form3' method='post' action='lima.php'>
<table border='1' width='90%' cellspacing='0' cellpadding='3'>
<tr><td align='center' width='20%'>Nama Baju</td>
<td align='center' width='20%'>Harga Baju</td>
<td align='center' width='20%'>Jumlah Beli (/ Pcs)</td>
<td align='center' width='10%'>Sub Total Harga</td>
<td align='center' width='10%'>Action</td>
</tr>";
echo " <tr id='last'>
<td colspan='5' align='right'><button type='button' id='addRow'>Add Rows</button></td>
</tr>
<tr>
<td colspan='3' align='center'>Grand Total harga</td>
<td align='center'><input type='text' class='input-small' name='totalongkos_ptgbhn' id='totalongkos_ptgbhn' style='text-align:right'></td>
</tr>
</table>
<br>
<input type='hidden' name='jum' value='$n'>
<input type='submit' name='submit' value='Submit'>
</form>";
?>
<script type="text/javascript" src=" jquery-1.7.2.min.js"></script>
<script type="text/javascript">
var i = 1;
$(function(){
$("#addRow").click(function(){
row = '<tr>'+
//Baju
'<td><select name="baju['+i+']" onchange="document.getElementById("harga['+i+']").value = idxhrg1[this.value] ? idxhrg2[this.value] : 0"> <option value="0">Pilih Item</option><?
$tampil=mysql_query("SELECT * FROM baju");
while($r=mysql_fetch_array($tampil)){
echo "<option value=$r[id]>$r[nm_baju]</option>";
$jsArray1 .= "idxhrg1['" . $r[id] . "'] = '" . addslashes($r['hrg']) . "';\n";
}?></select></td>'+
//Harga
'<td align="center"><input type="text" name="harga['+i+']" id="harga['+i+']" style="text-align:right" onFocus="startCalculate()" onBlur="stopCalc()"/></td>'+
//jumlah
'<td align="center"><select name="jumlah['+i+']" style="width:100px; text-align:center" onFocus="startCalculate()" onBlur="stopCalc()"><?
for ($a=0; $a<=50; $a++) {
echo "<option value=$a>$a</option>";
}
?></select></td>'+
//sub Total Harga
'<td align="center"><input type="text" name="Sub_totalharga['+i+']" style="text-align:right" onFocus="startCalculate()" onBlur="stopCalc()"/></td>'+
'<td align="center"><button type="button" class="del">Del</button></td>'+
'</tr>';
$(row).insertBefore("#last");
i++;
});
});
$(".del").live('click', function(){
$(this).parent().parent().remove();
});
<?php echo "$jsArray1";?>
</script>