ช่วยดู code ให้หน่อยคับ ทำเมนู select option แบบ3 เมนูผันแปรตามกัน ให้ค่า ที่เราเลือก หลังจากที่กดปุ่ม submit แล้ว ค่าที่เราเลือก มัน selectde เอาไว้
จะทำยังไง ให้ค่าที่เราเลือก selectde เอา ไว้ตอนที่กดปุ่ม submit
code index.php
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<?php
include_once '../../controllers/class.connect.php';
$pdo = new PDOConnectionFactory();
$conn=$pdo->getConnection();
include_once '../../controllers/core.php';
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
<link rel="stylesheet" type="text/css" href="../../css/content.css">
<!--
<script type="text/javascript" src="../../frameworks/jquery-1.4.2.min.js"></script>
<script type="text/javascript">
/*jQuery(function(){
jQuery("#umper").attr("disabled",true);
})
function changphylum(id_phylum){
if(id_phylum == 0){
jQuery("#id_subphylum").attr("disabled",true);
}else{
//var u = jQuery("#id_phylum2").val();
jQuery.ajax({
url:"option_order.php",
type:"post",
data:"id_phylum="+id_phylum,
success:function(r){
document.getElementById("subphylum").innerHTML = r;
}
});
jQuery("#id_subphylum").attr("disabled",false);
}
}
function changsubphylum(id_subphylum){
if(id_subphylum == 0){
jQuery("#id_class").attr("disabled",true);
}else{
changphylum(id_phylum);
jQuery.ajax({
url:"option_class.php",
type:"post",
data:"id_phylum="+id_phylum+"id_subphylum="+id_subphylum,
success:function(r){
document.getElementById("class").innerHTML = r;
}
});
jQuery("#id_class").attr("disabled",false);
}
}
*/
</script>
<script language="javascript">
/*
function fncSubmit()
{
if(document.form1.id_phylum.value == "")
{
alert('กรุณาเลือกเภท Phylum');
document.form1.id_phylum.focus();
return false;
}
if(document.form1.id_subphylum.value == ""){
alert('กรุณาเลือกประเภท subphylum');
document.form.id_subphylum.focus();
return false;
}
if(document.form1.id_class.value == ""){
alert('กรุณาเลือก class ');
document.form.id_class.focus();
return false;
}
if(document.form1.name_order.value == ""){
alert('กรุณาป้อนชื่อ order');
document.form.name_order.focus();
return false;
}
document.form1.btnSubmit();
}
*/
</script>-->
</head>
<body onload="loadList('1','1')">
<script language="JavaScript">
var HttPRequest = false;
if (window.XMLHttpRequest) { // Mozilla, Safari,...
HttPRequest = new XMLHttpRequest();
if (HttPRequest.overrideMimeType) {
HttPRequest.overrideMimeType('text/html');
}
} else if (window.ActiveXObject) { // IE
try {
HttPRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
HttPRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
function doCallAjax(type,refer,display) {
if (!HttPRequest) {
alert('Cannot create XMLHTTP instance');
return false;
}
var url = 'getdata.php';
var pmeters = "type="+type+"&refer="+refer;
HttPRequest.open('POST',url,true);
HttPRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
HttPRequest.setRequestHeader("Content-length", pmeters.length);
HttPRequest.setRequestHeader("Connection", "close");
HttPRequest.send(pmeters);
HttPRequest.onreadystatechange = function()
{
if(HttPRequest.readyState == 3) // Return Request
{
document.getElementById(display).innerHTML = HttPRequest.responseText;
document.getElementById('loading').style.display = "none";
} else
{
//document.getElementById('loading').style.display = "";
}
}
}
function loadList(idx,refer)
{
switch(idx)
{
case '2':idx2 = parseInt(idx)+1;
obj = document.getElementById('mySpan'+idx2);
obj.innerHTML = '<select name="list'+idx2+'" id="list'+idx2+'"><option value="0">choose...</option></select>';
idx2 = parseInt(idx)+2;
/*
obj = document.getElementById('mySpan'+idx2);
obj.innerHTML = '<select name="list'+idx2+'" id="list'+idx2+'"><option value="0">choose...</option></select>';
*/
break;
/*case '3':idx2 = parseInt(idx)+1;
obj = document.getElementById('mySpan'+idx2);
obj.innerHTML = '<select name="list'+idx2+'" id="list'+idx2+'"><option value="0">choose...</option></select>';
break;*/
}
doCallAjax(idx,refer,'mySpan'+idx);
}
</script>
<h1>order</h1>
<form method="post" id="form1" name="form1" action="<?php $_SERVER['PHP_SELF']?>">
<?php
/*
if(isset($_REQUEST['id_phylum'])){
echo "<input type='hidden' value='".$_REQUEST['id_phylum']."' id='id_phylum2' name='id_phylum2' />";
}else{
echo "<input type='hidden' value='' id='id_phylum2' name='id_phylum2' />";
}
*/
?>
<div id="register">
<fieldset>
<legend><span class="akey">รายละเอียด</span></legend>
<div class="Clear"></div>
<?php
//foreach(${"_".$_SERVER['REQUEST_METHOD']} as $key => $val)
//{
//echo "\$_".$_SERVER['REQUEST_METHOD']."['$key'] = $val <br>";
//print_r($val)."<br>";
//}
//$_REQUEST["date_ca"]=date("y-m-d");
if($_REQUEST["btnSubmit"]!=""){
$table="rc_order";//ชื่อตาราง
$fiele="id_order,id_phylum,id_subphylum,id_class,name_order,detail_order";// ชื่อ fiele
$value="'','".$_REQUEST["list1"]."','".$_REQUEST["list2"]."','".$_REQUEST["list3"]."','".$_REQUEST["name_order"]."','".$_REQUEST["detail_class"]."'";// ค่าที่ต้องการการ insert
$obj = new core();
$setinsertdb = $obj->setinsertdb($table, $fiele,$value);
}
?>
<table width="782">
<tr>
<td width="119" height="30">Phylum :</td>
<td width="157">
<span id="mySpan1">
<?php echo $_REQUEST["list1"];?>
<select name="list1" id="list1">
<?php
$table="rc_phylum";//ชื่อตาราง
$obj = new core();
$reault=$obj->setquery($table);
while($row=$reault->fetch(PDO::FETCH_OBJ)){
?>
<option value="<?php echo $row->id_phylum;?>"<?php if($row->id_phylum == $_REQUEST["list1"])echo "selected";?>><?php echo $row->name_phylum;?>
</option>
<?php }?>
</select>
</span>
</td>
<td width="111">subpylum : </td>
<td width="455">
<span id="mySpan2">
<select name="list2" id="list2">
<option value="0">กรุณาเลือก</option>
</select>
</span>
</td>
</tr>
<tr>
<td>class:</td>
<td>
<span id="mySpan3">
<select name="list3" id="list3">
<option value="0">กรุณาเลือก</option>
</select>
</span>
</td>
<td>order:</td>
<td><input type="text" name="name_order" id="name_order" size="60"></td>
</tr>
</table>
</fieldset>
</div>
<div id="register">
<fieldset>
<legend><span class="akey">คำอธิบาย</span></legend>
<div class="Clear"></div>
<textarea name="detail_class" id="detail_class" cols="60"
rows="6"></textarea>
<div class="Clear"></div>
</fieldset>
<div>
<input type="submit" name="btnSubmit" id="btnSubmit" value="บันทึกข้อมูล!"/>
</div>
</div>
</form>
</body>
</html>
<div id="loading" style="display:none;">LOADING...</div>
code getdata.php
<?php
include_once '../../controllers/class.connect.php';
$pdo = new PDOConnectionFactory();
$conn=$pdo->getConnection();
include_once '../../controllers/core.php';
?>
<?php
$type = $_POST['type'];
$refer = $_POST['refer'];
//echo $_POST["id_phylum"];
$referField = "";
$dataTable = "";
$optionValueField = "";
$optionTextField = "";
$listName = "list";
$nextType = "";
$displayName = "mySpan";
$actionEvent = "onchange";
$action = "";
switch($type)
{
case "0":exit();
case "1": $referField = "1";
$dataTable = "rc_phylum";
$optionValueField = "id_phylum";
$optionTextField = "name_phylum";
break;
case "2":$referField = "id_phylum";
$dataTable = "rc_subphylum";
$optionValueField = "id_subphylum";
$optionTextField = "name_subphylum";
break;
case "3":$referField = "id_subphylum";
$dataTable = "rc_class";
$optionValueField = "id_class";
$optionTextField = "name_class";
break;
/*
case "4":$referField = "AMPHUR_ID";
$dataTable = "district";
$optionValueField = "DISTRICT_ID";
$optionTextField = "DISTRICT_NAME";
break;
*/
default : $referField = "";
$dataTable = "rc_phylum";
$optionValueField = "id_phylum";
$optionTextField = "name_phylum";
$type = "1";
break;
}
$listName .= $type;
$nextType = $type+1;
$obj = new core();
$sql = "SELECT * FROM $dataTable where $referField = $refer";
//echo $sql;
$result=$obj->query($sql);
//$result = mysql_query($sql);
if($type < 3)
$action = "$actionEvent=\"JavaScript:loadList('$nextType',this.value)\"";
echo "<select name=\"$listName\" id=\"$listName\" $action><option value=\"0\">กรุณาเลือก...</option>";
while($row = $result->fetch(PDO::FETCH_ASSOC))
{
echo "<option value=\"$row[$optionValueField]\">$row[$optionTextField]</option>";
}
echo "</select>";
?>Tag : PHP, MySQL, JavaScript, Ajax
Date :
2010-08-23 22:34:21
By :
amnauysudsuk
View :
1261
Reply :
5
เหมือนผมจะงงกับคำถาม
Date :
2010-08-24 00:22:40
By :
reda_008
ให้ค่าที่เราเลือก ไว้ แบบตัวอยางข้างบนนะ คับ มันยัง อยู่หลังจาก กดปุ่ม บันทึกข้อมูลแล้ว
Date :
2010-08-24 12:31:16
By :
amnauysudsuk
คือผม บันทึกข้อมูลให้หน้าเดียวกันกับ ฟอร์มนะคับ
Date :
2010-08-24 12:32:16
By :
amnauysudsuk
เช็คค่า $_POST ถ้า ตรงกับ value option ไหน ก็ echo "selected" ที่ option นั้น
Date :
2010-08-24 12:39:38
By :
ultrasiam
<option value="<?php echo $row->id_phylum;?>"<?php if($row->id_phylum == $_REQUEST["list1"])echo "selected";?>><?php echo $row->name_phylum;?>
ทำแล้วคับแต่ค่า ก็ยังเหมือนเดิม ขอบคุณคับ
Date :
2010-08-24 15:59:08
By :
amnauysudsuk
Load balance : Server 03