2 combobox dan 1 atau 2 textbox

sarmin · Apr 20, 2013
Salam,
Saya ada problem dengan situasi 2 combobox dan 2 textbox :
1. tabel saya :
- Mak : id_makul, id_pengajar, makul,sks
- pengajar : id_pengajar, nama-pengajar.
2. yang saya inginkan :
- combobox1 : tampilkan Makul, otomatis isi combobox2 nama-pengajar sesuai tabel mak, kemudian textbox tampil sks.
berikut coding saya,
<h4> Tambah Mata Kuliah </h4>
<form method=post action=?menu=create_makul>
<table border=1>
	<tr>
		<td> ID Mata Kuliah </td>
		<td> : </td>
		<td>
	<?php    
// Koneksi  
//mysql_connect("localhost","root","");    
//mysql_select_db("db_ebsof");   
include "koneksi/koneksi.php"; 
$result = mysql_query("select * from mak1");  
$jsArray = "var prdName = new Array();\n";    

echo '<select name="prdId" onchange="changeValue(this.value)">';    
echo '<option>-------</option>';    

while ($row = mysql_fetch_array($result)) {    
    echo '<option value="' . $row['id_makul'] . '">' . $row['id_makul'] . '</option>';    
    $jsArray .= "prdName['" . $row['id_makul'] . "'] = {name:'" . addslashes($row['makul']) . "',desc:'".addslashes($row['sks'])."'};\n";    
}    
echo '</select>' ; 
 echo $makul; 
?>    
</td>
</td>
</tr>
<tr>
<td> Mata Kuliah </td>
<td> : </td>
<td> <input type="text" name="makul" id="makul"/> </td>  
</tr>
</td>
</tr>
<tr>
<td> SKS </td>
<td> : </td>
<td> <input type="text" name="sks" id="sks"/>  </td>
</tr>

<script type="text/javascript">    
<?php echo $jsArray; ?>  
function changeValue(id){  
document.getElementById('makul').value = prdName[id].name;  
document.getElementById('sks').value = prdName[id].desc;  
};  
</script>  
		
	<tr> 
		<td> Hari </td>
		<td> : </td>
		<td> <input type=text name=hari maxlength=100> </td>
	</tr>
	<tr>
			<td> Jam Tayang </td>
		<td> : </td>
<?
$gula=date('H:i');
$gula1=time('h:m:s');
?>
<td> <input type=text name=gula value= <?php echo "$gula"; ?>></td>

	</tr>
	<tr>
		<td> Pengajar </td>
		<td> : </td>
		<td> <select name=id_pengajar>
			<?php
			$makul = $_GET['makul'];
			include "koneksi/koneksi.php";
			$sql = mysql_query("select nip from mak where makul='$makul' order by nip");
			while ($data = mysql_fetch_array($sql)){
				echo "<option value='$data[nip]'> $data[nip] </option>";
			}
			?>
			</select>
		</td>
	</tr>
	<tr>
		<td colspan=3> <input type=submit value=Simpan> <input type=submit value=Batal onClick=history.back()> </td>
	</tr>
</table>
</form>




mohon dibantu ya OM, Master2 sekalian
Silahkan login untuk menjawab!
0
Loading...
Ellyx Christian · Apr 21, 2013 · 0 Suka · 0 Tidak Suka
saya kasi tahu yang pake ajax & jquery, biar lebih mudah.
pertama buat file get_pengajar.php:
<?php
$id = $_GET['id'];
include "koneksi/koneksi.php"; 
$query = mysql_query("select * from mak where id_makul='$id'");
echo '<select name="id_pengajar">';
while($data = mysql_fetch_array($query)){
	$pengajar = mysql_fetch_array(mysql_query("select * from pengajar where id_pengajar='".$data['id_pengajar']."'"));
	echo '<option value="'.$data['id_pengajar'].'">'.$pengajar['nama-pengajar'].'</option>';
}
echo '</select>';
?>
<h4> Tambah Mata Kuliah </h4>
<form method=post action=?menu=create_makul>
<table border=1>
	<tr>
		<td> ID Mata Kuliah </td>
		<td> : </td>
		<td>
	<?php    
// Koneksi  
//mysql_connect("localhost","root","");    
//mysql_select_db("db_ebsof");   
include "koneksi/koneksi.php"; 
$result = mysql_query("select * from mak1");  
$jsArray = "var prdName = new Array();\n";    
 
echo '<select name="prdId" onchange="changeValue(this.value)">';    
echo '<option>-------</option>';    
 
while ($row = mysql_fetch_array($result)) {    
    echo '<option value="' . $row['id_makul'] . '">' . $row['id_makul'] . '</option>';    
    $jsArray .= "prdName['" . $row['id_makul'] . "'] = {name:'" . addslashes($row['makul']) . "',desc:'".addslashes($row['sks'])."'};\n";    
}    
echo '</select>' ; 
 echo $makul; 
?>    
</td>
</td>
</tr>
<tr>
<td> Mata Kuliah </td>
<td> : </td>
<td> <input type="text" name="makul" id="makul"/> </td>  
</tr>
</td>
</tr>
<tr>
<td> SKS </td>
<td> : </td>
<td> <input type="text" name="sks" id="sks"/>  </td>
</tr>
<!-- tambah jquery dari google cdn atau download di jquery.com
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">
<?php echo $jsArray; ?>  
function changeValue(id){  
	document.getElementById('makul').value = prdName[id].name;  
	document.getElementById('sks').value = prdName[id].desc;
	$('#select-pengajar').load('get_pengajar.php?id='+id);
}
</script>  
 
	<tr> 
		<td> Hari </td>
		<td> : </td>
		<td> <input type=text name=hari maxlength=100> </td>
	</tr>
	<tr>
			<td> Jam Tayang </td>
		<td> : </td>
<?
$gula=date('H:i');
$gula1=time('h:m:s');
?>
<td> <input type=text name=gula value= <?php echo "$gula"; ?>></td>
 
	</tr>
	<tr>
		<td> Pengajar </td>
		<td> : </td>
		<td id="select-pengajar">
		<select name="id_pengajar"></select>
		</td>
	</tr>
	<tr>
		<td colspan=3> <input type=submit value=Simpan> <input type=submit value=Batal onClick=history.back()> </td>
	</tr>
</table>
</form>
0
Loading...
sarmin · Apr 27, 2013 · 0 Suka · 0 Tidak Suka
maaf Mas,
saya sudah buat get_pengajar.php. tapi gak loading pengajarnya, mungkin gak load file get_pengajarnya.
berikut coding saya.
<h4> Tambah Mata Kuliah </h4>
<form method=post action=?menu=create_makul>
<table border=1>
	<tr>
		<td> ID Mata Kuliah </td>
		<td> : </td>
		<td>
	<?php    
// Koneksi  
//mysql_connect("localhost","root","");    
//mysql_select_db("db_ebsof");   
include "koneksi/koneksi.php"; 
$result = mysql_query("select * from mak1");  
$jsArray = "var prdName = new Array();\n";    

echo '<select name="prdId" onchange="changeValue(this.value)">';    
echo '<option>-------</option>';    

while ($row = mysql_fetch_array($result)) {    
    echo '<option value="' . $row['id_makul'] . '">' . $row['id_makul'] . '</option>';    
  //  $jsArray .= "prdName['" . $row['id_makul'] . "'] = {name:'" . addslashes($row['makul']) . "',desc1:'".addslashes($row['id_makul'])."',desc:'".addslashes($row['sks'])."'};\n";    
$jsArray .= "prdName['" . $row['id_makul'] . "'] = {name:'" . addslashes($row['makul']) . "',desc:'".addslashes($row['sks'])."'};\n";  
  }    
echo '</select>' ; 
 //echo $makul; 
?>    
</td>
</td>
</tr>
<tr>
<td> Mata Kuliah </td>
<td> : </td>
<td> <input type="text" name="makul" id="makul"/> </td>  
</tr>
</td>
</tr>
<tr>
<td> SKS </td>
<td> : </td>
<td> <input type="text" name="sks" id="sks"/>  </td>
</tr>

<input type=hidden name="id_makul" id="id_makul"/>

<script language="javascript" src="jquery-1.9.1.min"></script>
<script type="text/javascript">
//<script language="javascript" src="js/jquery-1.9.1.min.js">

<?php echo $jsArray; ?>  
function changeValue(id){  
document.getElementById('makul').value = prdName[id].name;  
document.getElementById('sks').value = prdName[id].desc;  
$('#select-pengajar').load('get_pengajar.php?id='+id);
//document.getElementById('id_makul').value = prdName[id].desc1;  
};  
</script>  
		
	<tr> 
		<td> Hari </td>
		<td> : </td>
		<td> <input type=text name=hari maxlength=100> </td>
	</tr>
	<tr>
			<td> Jam Tayang </td>
		<td> : </td>
<?
$gula=date('H:i');
$gula1=time('h:m:s');
?>
<td> <input type=text name=gula value= <?php echo "$gula"; ?>></td>

		<tr>
<td> Pengajar </td>
<td> : </td>
<td id="select-pengajar">
<select name="id_pengajar"></select>
</td>
</tr>
	<tr>
		<td colspan=3> <input type=submit value=Simpan> <input type=submit value=Batal onClick=history.back()> </td>
	</tr>
</table>
</form>

0
Loading...
sarmin · May 1, 2013 · 0 Suka · 0 Tidak Suka
ok,mantap...thx ya...Master Indonesia is the Best...

Regards,
Sarmin Ikoni
0
Loading...
sarmin · May 3, 2013 · 0 Suka · 0 Tidak Suka
mas,
kalau combobox pencarian diganti dengan textbox (auto complete) gimana ya, Mohon dibantu ya

BestRegards,