สอบถามเรื่อง การ insert จากหน้า formให้แสดงค่าที่เลือกที่ dropdownlist จาก database เช่น เลือกระดับการศึกษาแล้วให้แสดงข้อมูลชั้นปี เทอม วิชา จาก database น่ะค่ะ
ต้องการแบบ Refresh หน้าหรือไม่ครับ
Date :
2013-04-09 11:26:33
By :
Ex-[S]i[L]e[N]t
Code (PHP).....อันนี้ code หน้า register.php น่ะค่ะ และก็จะมีหน้า combo.php กำหนดเงื่อนไขจาก function
<?php require_once('connect.php'); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#username").change(function(){
$("#suser").empty();
$.ajax({
url: "returnCustomer2.php" ,
type: "POST",
data: 'suser=' + $("#username").val()
})
.success(function(result) {
var obj = jQuery.parseJSON(result);
if(obj != '')
{
$.each(obj, function(key, inval) {
if($("#username").val() == inval["user_st"])
{
$("#suser").html(" <font color='red'> user นี้มีคนใช้แล้ว</font>");
}
});
}
});
});
});
</script>
<script type="text/javascript">
$(document).ready(function(){
$("#txtCustomerID").change(function(){
$("#sCusID").empty();
$.ajax({
url: "returnCustomer1.php" ,
type: "POST",
data: 'sCusID=' + $("#txtCustomerID").val()
})
.success(function(result) {
var obj = jQuery.parseJSON(result);
if(obj != '')
{
$.each(obj, function(key, inval) {
if($("#txtCustomerID").val() == inval["id_card"])
{
$("#sCusID").html(" <font color='red'>รหัสนี้มีคนใช้แล้ว</font>");
}
});
}
});
});
});
</script>
<script >
function list(tagNext, val, txtCon) {
$.getJSON('combo.php', {name:txtCon,value:val}, function(data) {
var select = $(tagNext);
var options = select.attr('options');
$('option', select).remove();
$(select).append('<option value=""> ------------- เลือก ------------- </option>');
$.each(data, function(index, array) {
$(select).css("display","inline");
$(select).append('<option value="' + array[0] + '">' + array[1] + '</option>');
});
});
}
</script>
<script >
function list2(tagNext, val, txtCon) {
$.getJSON('combo2.php', {name:txtCon,value:val}, function(data) {
var select = $(tagNext);
var options = select.attr('options');
$('option', select).remove();
$(select).append('<option value=""> ------------- เลือก ------------- </option>');
$.each(data, function(index, array) {
$(select).css("display","inline");
$(select).append('<option value="' + array[0] + '">' + array[1] + '</option>');
});
});
}
</script>
</head>
<body>
<form name="formname" id="login" action="insert.php" method="post">
<center>
<div class="divf">
<center>
<div >
<h1>Registered </h1>
</div>
<table>
<tr>
<td align="right" width="166">รหัสบัตร</td>
<td align="left" width="341">
<input type="text" name="card" id="txtCustomerID" width="200" class="tb5" />
<span id="sCusID"></span></td>
</tr>
<tr>
<td align="right" width="166">ชื่อที่ใช้งาน</td>
<td align="left" width="341">
<input type="text" name="user" id="username" width="200" class="tb5" />
<span id="suser"></span></td>
</tr>
<tr>
<td align="right" width="166"> รหัสผ่าน </td>
<td align="left" width="341"><input type="password" name="pass" width="200" class="tb5"/></td>
</tr>
<tr>
<td align="right" width="166">ยืนยันรหัสผ่าน</td>
<td align="left" width="341"><input type="password" name="pass2" width="200" class="tb5"/></td>
</tr>
<tr>
<td align="right" width="166">ชื่อ</td>
<td align="left" width="341"><input type="text" name="names" width="200" class="tb5"/></td>
</tr>
<tr>
<td align="right" width="166">นามสกุล</td>
<td align="left" width="341"><input type="text" name="last" width="200" class="tb5"/></td>
</tr>
<tr>
<td align="right" width="166">ที่อยู่</td>
<td align="left" width="341"><select id="add" name="add" class="wrapper-dropdown" onchange="list2('#amphur',this.value,'add')">
<option value="" >------------- เลือก -------------</option>
<?php
mysql_select_db($database_connect, $connect);
$query_q = "SELECT * FROM province ORDER BY PROVINCE_ID ASC";
$q = mysql_query($query_q, $connect) or die(mysql_error());
while($row_q = mysql_fetch_array($q)){
$id = $row_q["PROVINCE_ID"];
$name = $row_q["PROVINCE_NAME"];
//echo"<option value='$id' selected>$name</option>";?>
<option value="<?=$id?>"><?=$name?></option><?
};
?>
</select>
<select name="amphur" id="amphur" style="display:none" class="wrapper-dropdown" onchange="list2('#tumbon', this.value, 'amphur')"></select>
<select name="tumbon" id="tumbon" style="display:none" class="wrapper-dropdown"></select></td>
</tr>
<tr>
<td align="right" width="166">เบอร์โทร</td>
<td align="left" width="341"><input type="text" name="tel" width="200" class="tb5" /></td>
</tr>
<tr>
<td align="right" width="166">ช่วงชั้นที่</td>
<td align="left" width="341">
<select id="type" name="type" class="wrapper-dropdown" onchange="list('#subType',this.value,'type')">
<option value="" >------------- เลือก -------------</option>
<?php
mysql_select_db($database_connect, $connect);
$query_q = "SELECT * FROM level ORDER BY id_level ASC";
$q = mysql_query($query_q, $connect) or die(mysql_error());
while($row_q = mysql_fetch_array($q)){
$id = $row_q["id_level"];
$name = $row_q["name_level"];
//echo"<option value='$id' selected>$name</option>";?>
<option value="<?=$id?>"><?=$name?></option><?
};
?>
</select></td>
</tr>
<tr>
<td align="right" width="166">ระดับชั้น</td>
<td align="left" width="341">
<select name="subType" id="subType" style="display:none" class="wrapper-dropdown" onchange="list('#subType2', this.value, 'subType')"></select></td>
</tr>
<tr>
<td align="right" width="166">ภาคการศึกษา</td>
<td align="left" width="341">
<select name="subType2" id="subType2" style="display:none" class="wrapper-dropdown" onchange="list('#data', this.value, 'subType')"></select></td>
</tr>
<tr>
<td align="right" width="166">รายวิชา</td>
<td align="left" width="341">
<select name="data" id="data" style="display:none" class="wrapper-dropdown"></select></td>
<select name="data2" id="data2" style="display:none" class="wrapper-dropdown"></select></td>
</tr>
</table>
<div align="center" >
<p>
<input type="submit" class="classname" value="สมัคร" />
</p>
</div>
<p> </p>
<p> </p>
</center>
</div>
<div>
</div>
</center>
</form>
</body>
</html>
ประวัติการแก้ไข 2013-04-09 12:03:53 2013-04-09 12:04:33
Date :
2013-04-09 11:59:24
By :
laongdow
ตอบความคิดเห็นที่ : 3 เขียนโดย : laongdow เมื่อวันที่ 2013-04-09 11:59:24
รายละเอียดของการตอบ ::
combo.php
Code (PHP)
<?php require_once('connect.php'); ?>
<?
$aD = array();
$aB = array();
$id = $_GET['value'];
$nameType = $_GET['name'];
if($nameType == 'type') {
if($id!=""){
mysql_select_db($database_connect, $connect);
$query_subType = "SELECT * FROM grade_level WHERE id_level like '%$id%'";
$subType = mysql_query($query_subType, $connect) or die(mysql_error());
$totalRows_subType = mysql_num_rows($subType);
if($totalRows_subType>0){
while($row_subType = mysql_fetch_array($subType)){
$aD[] = array($row_subType["id_grade"],$row_subType["name_grade"]);
}
}
}
}
if($nameType == "subType"){
if($id){
mysql_select_db($database_connect, $connect);
$query_data = "SELECT * FROM term WHERE id_grade like '%$id%'";
$data = mysql_query($query_data, $connect) or die(mysql_error());
$totalRows_data = mysql_num_rows($data);
if($totalRows_data>0){
while($row_data = mysql_fetch_array($data)){
$aD[] = array($row_data["id_term"],$row_data["name_term"]);
}
}
}
}
if($nameType == "subType2"){
if($id){
mysql_select_db($database_connect, $connect);
$query_data = "SELECT * FROM subject WHERE id_term like '%$id%'";
$data = mysql_query($query_data, $connect) or die(mysql_error());
$totalRows_data = mysql_num_rows($data);
if($totalRows_data>0){
while($row_data = mysql_fetch_array($data)){
$aD[] = array($row_data["id_sub"],$row_data["name_sub"]);
}
}
}
}
if($nameType == 'data') {
if($id!=""){
mysql_select_db($database_connect, $connect);
$query_subType = "SELECT * FROM subject WHERE id_level like '$id%'";
$subType = mysql_query($query_subType, $connect) or die(mysql_error());
$totalRows_subType = mysql_num_rows($subType);
if($totalRows_subType>0){
while($row_subType = mysql_fetch_array($subType)){
$aD[] = array($row_subType["id_sub"],$row_subType["name_sub"]);
}
}
}
}
if($nameType == 'data2') {
if($id!=""){
mysql_select_db($database_connect, $connect);
$query_subType = "SELECT * FROM subject WHERE id_level 'L003'";
$subType = mysql_query($query_subType, $connect) or die(mysql_error());
$totalRows_subType = mysql_num_rows($subType);
if($totalRows_subType>0){
while($row_subType = mysql_fetch_array($subType)){
$aB[] = array($row_subType["id_sub"],$row_subType["name_sub"]);
}
}
}
}
echo json_encode($aD);
echo json_encode($aB);
?>
Date :
2013-04-09 12:05:33
By :
laongdow
apply json example ตัวนี้ดูครับ
form
Code (PHP)
เขต :
<select id="amphur">
<option value="">กรุณาเลือก</option>
<?
$amphur=mysql_query("select * from amphur") or die (mysql_error());
while($result=mysql_fetch_array($amphur)){
?>
<option value="<?=$result['GEO_ID'];?>"><?=$result['AMPHUR_NAME'];?></option>
<?
}
?>
</select><br>
จังหวัด:
<select id="province"></select>
<script src="admin/module/inc/scripts/jquery-1.8.1.min.js"></script>
<script>
$(document).ready(function(e) {
$('#amphur').change(function(){
var province=$('#province');
var val=this.value;
$.ajax({url:"access.php",
async:false,
cache:false,
type:'POST',
data:'geo='+val,
success: function(result){
var obj=jQuery.parseJSON(result);
$.each(obj,function(key,val){
province.append('<option value='+val['PROVINCE_ID ']+'>'+val['PROVINCE_NAME']+'</option>');
})
}
});
});
});
</script>
access.php
Code (PHP)
<?
$geo=$_POST['geo'];
$arrResult=array();
$s_province=mysql_query("select * from province WHERE GEO_ID='".$geo."'") or die (mysql_error());
$num_field=mysql_num_fields($s_province);
while($result=mysql_fetch_array($s_province)){
$arrCol=array();
for($i=0;$i<=$num_field;$i++){
$arrCol[mysql_field_name($s_province,$i)]=$result[$i];
}
array_push($arrResult,$arrCol);
}
echo json_encode($arrResult);
?>
Date :
2013-04-09 12:34:36
By :
Ex-[S]i[L]e[N]t
Load balance : Server 00