form dinamis one to many

daniel · Nov 27, 2012
form input_surat_jalan
<form name="form1" method="post" action="proses_surat_jalan.php">
                    <center><strong><h1>Input Form Surat Jalan</h1></strong></center><br><br>
                    <table width="433" border="1">
  <tr>
    <td width="104">No. Surat Jalan</td>
    <td width="0">:</td>
    <td width="307"><input name="no_sj" type="text">
      <select name="bln_sj" id="bln_sj">
        <option>/SP-JMI/I/</option>
        <option>/SP-JMI/II/</option>
        <option>/SP-JMI/III/</option>
        <option>/SP-JMI/IV/</option>
        <option>/SP-JMI/V/</option>
        <option>/SP-JMI/VI/</option>
        <option>/SP-JMI/VII/</option>
        <option>/SP-JMI/VIII/</option>
        <option>/SP-JMI/IX/</option>
        <option>/SP-JMI/X/</option>
        <option>/SP-JMI/XI/</option>
        <option>/SP-JMI/XII/</option>
      </select>
      <? $now=date("Y");
echo "<select name=thn_sj>
<option value=2000 selected>2000</option>";
for($thn=2001; $thn<=$now; $thn++){
echo "<option value=$thn>$thn</option>";}
echo "</select>";
?></td>
  </tr>
  <tr>
    <td>Tanggal</td>
    <td>:</td>
    <td><?php
//array yang digunakan pada ComboBox bulan
$bln=array(1=>"Januari","Februari","Maret","April","Mei",
"Juni","July","Agustus","September","Oktober",
"November","Desember");

//membuat tanggal 1-31 pada ComboBox
echo "<select name=tgl>
<option value=01 selected>01</option>";
for($tgl=2; $tgl<=31; $tgl++){
$tgl_leng=strlen($tgl);
if ($tgl_leng==1)
$i="0".$tgl;
else
$i=$tgl;
echo "<option value=$i>$i</option>";}
echo "</select>";

//membuat bulan ComboBox
echo "<select name=bln>
<option value=Januari selected>Januari</option>";
for($bulan=2; $bulan<=12; $bulan++){
echo "<option value=$bulan>$bln[$bulan]</option>";}
echo "</select>";

$now=date("Y");
echo "<select name=thn>
<option value=2000 selected>2000</option>";
for($thn=2001; $thn<=$now; $thn++){
echo "<option value=$thn>$thn</option>";}
echo "</select>";
?></td>
  </tr>
  <tr>
    <td>Nama Perusahaan</td>
    <td>:</td>
    <td><select name="nama_perusahaan" id="nama_perusahaan">
    <option value="0">---Pilih Perusahaan---</option>
    <?php
    include "koneksi.php";
	$query="SELECT * FROM data_perusahaan";
	$hasil=mysql_query($query);
	while ($data=mysql_fetch_array($hasil))
	{
	echo"<option value='$data[nama_perusahaan]'>$data[nama_perusahaan]</option>";
	}
	?>
    </select></td>
  </tr>
  <tr>
    <td>Alamat Perusahaan</td>
    <td>:</td>
    <td><label for="alamat_perusahaan"></label>
      <textarea type=text name="alamat_perusahaan" id="alamat_perusahaan" cols="45" rows="5"></textarea></td>
  </tr></table>
  <table width="433" border="1">
  <tr>
    <td>Nama Part</td>
    <td>Nomor Part</td>
    <td>Jumlah</td>
  </tr>
    <td>
      <input type="text" name="part_name[0]" id="part_name"></td>
    <td>
      <input type="text" name="part_number[0]" id="part_number">
	</td>
    <td>
      <input type="text" name="qty[0]" id="qty"></td>
    <td><button type="button" class="del">Del</button></td>  
  </tr>
  <tr id="last">  
        <td colspan="4" align="right"><button type="button" id="addRow">Add</button></td>  
    </tr>
</table>
                    <br>
<input name="simpan" type="submit" value="Simpan"><input name="cancel" type="reset" value="Cancel">

                      </form>
<script type="text/javascript" src=" https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>  
<script type="text/javascript">  
var i = 1;  
$(function(){  
    $("#addRow").click(function(){  
        row = '<tr>'+ 
	'<td><input type="text" name="part_name[+i+]" id="part_name"/></td>'+
    '<td><input type="text" name="part_number[+i+]" id="part_number"/></td>'+
    '<td><input type="text" name="qty[+i+]" id="qty"/></td>'+
	'<td><button type="button" class="del">Del</button></td>'+
	 '</tr>';  
        $(row).insertBefore("#last");  
        i++;  
        });  
    });  
    $(".del").live('click', function(){  
        $(this).parent().parent().remove();  
        });  
</script> 
form proses_sj.php
<?php
error_reporting(E_ALL ^ (E_NOTICE | E_WARNING));
include "koneksi.php";
$simpan=mysql_query("INSERT INTO data_surat_jalan(no_sj,bln_sj,thn_sj,tgl,bln,thn,nama_perusahaan,alamat_perusahaan) VALUES('$_POST[no_sj]',
														 '$_POST[bln_sj]',
														 '$_POST[thn_sj]',
														 '$_POST[tgl]',
														 '$_POST[bln]',
														 '$_POST[thn]',
														 '$_POST[nama_perusahaan]',
														 '$_POST[alamat_perusahaan]')");
$sql = "select max(no_sj) as no_sj from data_surat_jalan limit 1";
$row = mysql_fetch_array(mysql_query($sql));  
$no_sj = $row['no_sj']; 
foreach($_POST['part_name'] as $key => $part_name)
{
$sql =mysql_query("INSERT INTO data_material_sj(no_sj,part_name,part_number,qty) VALUES ('{$_POST[no_sj]}','{$_POST[part_name][$key]}','{$part_number}','{$_POST[qty][$key]}')");
}
if ($simpan)
{
	echo"<script>
	window.alert('Input Surat Jalan Berhasil')
	window.location='data-surat-jalan.php'</script>";
}
else
{
	echo"<script>
	window.alert('Input Surat Jalan Gagal')
	window.location='input-surat-jalan.php'</script>";
}
?>

kq g bisa save ke database yah??
n gimana cara'a supaya bisaa ditamplkn jg..
Silahkan login untuk menjawab!
0
Loading...
Ellyx Christian · Nov 27, 2012 · 0 Suka · 0 Tidak Suka
sepertinya salah dijavascript:
<script type="text/javascript">  
var i = 1;  
$(function(){  
    $("#addRow").click(function(){  
        row = '<tr>'+ 
    '<td><input type="text" name="part_name['+i+']" id="part_name-'+i+'"/></td>'+
    '<td><input type="text" name="part_number['+i+']" id="part_number-'+i+'"/></td>'+
    '<td><input type="text" name="qty['+i+']" id="qty-'+i+'"/></td>'+
    '<td><button type="button" class="del">Del</button></td>'+
     '</tr>';  
        $(row).insertBefore("#last");  
        i++;  
        });  
    });  
    $(".del").live('click', function(){  
        $(this).parent().parent().remove();  
        });  
</script> 
0
Loading...
daniel · Nov 28, 2012 · 0 Suka · 0 Tidak Suka
kq masih belum bisa di save ya??
ap ad yang salah di form prosesnya ya??
mohon pencerahannya
:)
0
Loading...
daniel · Nov 28, 2012 · 0 Suka · 0 Tidak Suka
kalo yang diinput hanya sampai ke batas alamat perusahaan aja sudah bisa tersave,tpi waktu mau save dari bagian part name sampai qty kq msh blm bisa yah??
hehe
mohon penerahannya ya mas..
0
Loading...
Ellyx Christian · Dec 5, 2012 · 0 Suka · 0 Tidak Suka
kamu di sql untuk menyimpan datanya salah:
foreach($_POST['part_name'] as $key => $part_name)
{
$sql =mysql_query("INSERT INTO data_material_sj(no_sj,part_name,part_number,qty) VALUES ('{$_POST[no_sj]}','{$_POST[part_name][$key]}','{$part_number}','{$_POST[qty][$key]}')");

seharusnya:
foreach($_POST['part_name'] as $key => $part_name)
{
$sql =mysql_query("INSERT INTO data_material_sj(no_sj,part_name,part_number,qty) VALUES ('{$no_sj}','{$part_name}','{$_POST['part_number'][$key]}','{$_POST['qty'][$key]}')");