|
|
|
การแสดงข้อมูลใน ListBox จากฐานข้อมูล (Listbox ใช้ Ajax) |
|
|
|
|
|
|
|
1.ถ้าจะให้คลิกที่รูปดินสอแล้วส่งข้อมูลมายัง textbox ต้องทำอย่างไรบ้าง (ปกติเคยใช้แต่ Button ถ้าจะส่งค่า Id ต้องทำอย่างไรค่ะ)
2.หนูขอนุญาติถามค่ะ พอดีโหลด ตัวอย่างการใช้ listbox การเลือกจังหวัด-อำเภอ-ตำบล จากเว็ป http://www.codetukyang.com/php/scripts/ajaxprovincecitytambon.htm
แต่หนูไม่มีความรู้ในการใช้ AJAX เลยขอถามว่าต้องทำอย่างไรเมื่อต้องการดึงข้อมูลขึ้นมาแก้ไขค่ะให้แสดง List ตามที่บันทึกใน SQL ค่ะ
File customer.php
Code (PHP)
<?php //include 'navbar.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>TMS PLASE</title>
<style type="text/css">
<!--
body {
background-color: #FFFFFF;
}
.style2 {color: #000000}
-->
</style>
<script language=Javascript>
function Inint_AJAX() {
try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch(e) {} //IE
try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) {} //IE
try { return new XMLHttpRequest(); } catch(e) {} //Native Javascript
alert("XMLHttpRequest not supported");
return null;
};
function dochange(src, val) {
var req = Inint_AJAX();
req.onreadystatechange = function () {
if (req.readyState==4) {
if (req.status==200) {
document.getElementById(src).innerHTML=req.responseText; //รับค่ากลับมา
}
}
};
req.open("GET", "locale.php?data="+src+"&val="+val); //สร้าง connection
req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8"); // set Header
req.send(null); //ส่งค่า
}
window.onLoad=dochange('province', -1);
</script>
</head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
<body>
<p> </p>
<form id="sel" name="form1" method="post" action="">
<?php
Include_once ("../tmsplate/db/connectDatabase.php");
$buttonss=$_POST['s'];
$buttonsave=$_POST['save'];
$buttondell=$_POST['delete'];
$buttonedit=$_POST['edit'];
$buttonclear=$_POST['clear'];
$s1=$_POST['ss'];
$s2="'$s1'";
if ($buttonss=="s")
{
$strSQL = "SELECT * FROM customer where idcus=$s2 or namecus=$s2";
$strSQL .= "ORDER BY idcus asc ";
$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
$objResult1 = mysql_fetch_array($objQuery);
}
if ($buttonsave=="save")
{
$strSQL = "SELECT * FROM customer ORDER BY idcus Desc";
$objQuery = mysql_query($strSQL) or die ("0|Error Query [".$strSQL."]");
$objResult = mysql_fetch_array($objQuery);
$strSQL = "INSERT INTO customer ";
$strSQL .="(idcus,namecus,surcus,bcus,mcus,tcus,acus,jcus,pcus,tellcus) ";
$strSQL .="VALUES (";
$strSQL .="". "'". $_POST["id"] ."'" . " ," . "'". $_POST["name"] ."'" . ",";
$strSQL .="". "'". $_POST["sur"] ."'" . " ," . "'". $_POST["bcus"] ."'" . ",";
$strSQL .="". "'". $_POST["mcus"] ."'" . " ," . "'". $_POST["district"] ."'" . ",";
$strSQL .="". "'". $_POST["amphur"] ."'" . " ," . "'". $_POST["province"] ."'" . ",";
$strSQL .="". "'". $_POST["pcus"] ."'" . " ," . "'". $_POST["tell"] ."'" . ")";
$objQuery = mysql_query($strSQL) or die ("0|Error Query [".$strSQL."]");
}
if ($buttondell=="delete")
{
$id = $_POST['id'];
$strSQL = "delete from customer where idcus="."'".$id."'";
$objQuery = mysql_query($strSQL) or die ("0|Error Query [".$strSQL."]");
}
if ($buttonedit=="edit")
{
$strSQL = "INSERT INTO customer ";
$strSQL .="(idcus,namecus,surcus,bcus,mcus,tcus,acus,jcus,pcus,tellcus) ";
$strSQL = "update customer set namecus=". "'". $_POST["name"] ."'". ",";
$strSQL .= "surcus=". "'".$_POST["sur"] ."'" . "," . "bcus=". "'".$_POST["bcus"] ."'" .",";
$strSQL .= "mcus=". "'".$_POST["mcus"] ."'" . "," . "tcus=". "'".$_POST["tcus"] ."'" .",";
$strSQL .= "acus=". "'".$_POST["acus"] ."'" . "," . "jcus=". "'".$_POST["jcus"] ."'" .",";
$strSQL .= "pcus=". "'". $_POST["pcus"] ."'" .","."tellcus=". "'".$_POST["tell"] ."'" . " where idcus=". "'". $_POST["id"] ."'";
$objQuery = mysql_query($strSQL) or die ("0|Error Query [".$strSQL."]");
}
?>
<p> </p>
<table width="709" height="273" border="0" align="center">
<tr>
<td height="269" valign="top"><ul class="nav nav-tabs">
<li role="presentation" class="active"><a href="#">ข้อมูลลูกค้า</a></li>
</ul>
<table width="554" height="136" border="0" align="center">
<tr>
<td width="66" height="43">ค้นหา</td>
<td width="110"><label>
<input type="text" name="ss" id="ss" />
</label></td>
<td width="114"><label>
<input type="submit" name="s" id="s" value="s" />
</label></td>
<td width="8"> </td>
<td width="77"><div align="left">Id</div></td>
<td width="139"><input type="text" name="id" id="id" value="<?php echo $objResult1["idcus"];?>" /></td>
</tr>
<tr>
<td height="42">ชื่อ</td>
<td><input type="text" name="name" id="name" value="<?php echo $objResult1["namecus"];?>" /></td>
<td>นามสกุล</td>
<td><input type="text" name="sur" id="sur" value="<?php echo $objResult1["surcus"];?>"/></td>
<td>เบอร์</td>
<td><input type="text" name="tell" id="tell" value="<?php echo $objResult1["tellcus"];?>"/></td>
</tr>
<tr>
<td>บ.</td>
<td><input type="text" name="bcus" id="bcus" value="<?php echo $objResult1["bcus"];?>" /></td>
<td>หมู่</td>
<td><input type="text" name="mcus" id="mcus" value="<?php echo $objResult1["mcus"];?>"/></td>
<td>จังหวัด</td>
<?php //*****************************************************************ในส่วนของอำเภอตำบล จังหวัด ให้แสดงข้อมุลถ้าจะแก้ไข ?>
<td><!--<input type="text" name="tcus" id="tcus" value="<?php echo $objResult1["tcus"];?>"/></td>-->
<font id="province">
<select name="select2">
<option value="0">=========================</option>
</select>
</font></tr>
<tr>
<td>อำเภอ</td>
<td>
<font id="amphur"><select><option value='0'>=========================</option></select></font>
<!-- <input type="text" name="acus" id="acus" value="<?php echo $objResult1["acus"];?>"/></td>-->
<td>ตำบล</td>
<td><!-- <input type="text" name="jcus" id="jcus" value="<?php echo $objResult1["jcus"];?>"/></td>-->
<font id="district">
<select name="select">
<option value="<?php echo $objResult1["tcus"];?>">=========================</option>
</select>
</font>
<td>รหัสไปรษณีย์</td>
<td><input type="text" name="pcus" id="pcus" value="<?php echo $objResult1["pcus"];?>"/></td>
</tr>
</table>
<table width="413" height="49" border="0" align="center">
<tr>
<td width="76" align="center" valign="middle"><input type="submit" name="edit" id="edit" value="edit" /></td>
<td width="77" height="43" align="center" valign="middle"><input type="submit" name="delete" id="delete" value="delete" />
<label></label></td>
<td width="88" align="center" valign="middle"><input type="submit" name="save" id="save" value="save" />
<label></label></td>
<td width="74"></ul>
<label></label>
<input type="submit" name="save2" id="save2" value="clear" /> </tr>
<?php
?>
</table></td>
</tr>
</table>
<table width="1000" border="1">
<tr>
<table class="table table-striped" width="708" height="1064" border="0" align="center">
<tr>
<td height="1058" valign="top">
<ul class="nav nav-tabs">
<li role="presentation" class="active"><a href="#">รายชื่อลูกค้า</a></li>
</ul>
<?php
$strSQL = "SELECT * FROM customer ";
$strSQL .= "ORDER BY idcus asc ";
$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
?>
<table class="table table-striped" width="90%" height="48" border="0" align="center">
<td width="83" align="center" bgcolor="#CCCCCC">ลำดับ</td>
<td width="146" bgcolor="#CCCCCC"><div align="center" class="style2">
<div align="left">ID</div>
</div></td>
<td width="147" bgcolor="#CCCCCC"><div align="center" class="style2">
<div align="left">ชื่อ</div>
</div></td>
<td width="157" bgcolor="#CCCCCC"><div align="center" class="style2">
<div align="left">นามสกุล</div>
</div></td>
<td width="189" bgcolor="#CCCCCC"><div align="center" class="style2">
<div align="left">อำเภอ</div>
</div></td>
<td width="204" bgcolor="#CCCCCC"><div align="center" class="style2">
<div align="left">จังหวัด</div>
</div></td>
<td width="171" bgcolor="#CCCCCC"><div align="center" class="style2">
<div align="left">เบอร์โทร</div>
</div></td>
<td width="171" bgcolor="#CCCCCC"><div align="center" class="style2">
<div align="left">เลือก</div>
</div></td>
<?php
$nu1=1;
while($objResult = mysql_fetch_array($objQuery))
{
?>
<tr>
<td width="83" align="center" bgcolor="#FFFFFF"><?php echo $nu1++;?></td>
<td width="146" bgcolor="#FFFFFF"><?php echo $objResult["idcus"];?></td>
<td width="147" bgcolor="#FFFFFF"><?php echo $objResult["namecus"];?></td>
<td width="157" bgcolor="#FFFFFF"><?php echo $objResult["surcus"];?></td>
<td width="189" bgcolor="#FFFFFF"><?php echo $objResult["acus"];?></td>
<td width="204" bgcolor="#FFFFFF"><?php echo $objResult["jcus"];?></td>
<td width="171" bgcolor="#FFFFFF"><?php echo $objResult["tellcus"];?></td>
<td width="171" bgcolor="#FFFFFF">
<?php //*****************************************************************กดตรงนี้ให้ส่งข้อมูลไป Textbox ?>
<button type="button" class="btn btn-default btn-sm" onclick="window.location='customershow.php?idcus=<?php echo $objResult["idcus"];?>'" style="margin:3px;"><span class='glyphicon glyphicon-pencil'></span></button></td>
</tr>
<?php
}
?>
</table>
<p> </p>
<p> </p>
</form>
<p> </p>
<script type="text/javascript"
src="js/jquery.min.js">
</script>
<script type="text/javascript"
src="js/bootstrap.min.js">
</script>
</body>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
</html>
File locale.php
Code (PHP)
<?
header("content-type: text/html; charset=utf-8");
header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header ("Cache-Control: no-cache, must-revalidate");
header ("Pragma: no-cache");
include "dbconfig.php";
conndb();
$data = $_GET['data'];
$val = $_GET['val'];
if ($data=='province') {
echo "<select name='province' onChange=\"dochange('amphur', this.value)\">\n";
echo "<option value='0'>==== เลือกจังหวัด ====</option>\n";
$result=mysql_db_query($dbname,"select * from province order by PROVINCE_NAME");
while($row = mysql_fetch_array($result)){
echo "<option value=\"$row[PROVINCE_ID]\" >$row[PROVINCE_NAME]</option> \n" ;
}
} else if ($data=='amphur') {
echo "<select name='amphur' onChange=\"dochange('district', this.value)\">\n";
echo "<option value='0'>========= เลือกอำเภอ =========</option>\n";
$result=mysql_db_query($dbname,"SELECT * FROM amphur WHERE PROVINCE_ID= '$val' ORDER BY AMPHUR_NAME");
while($row = mysql_fetch_array($result)){
echo "<option value=\"$row[AMPHUR_ID]\" >$row[AMPHUR_NAME]</option> \n" ;
}
} else if ($data=='district') {
echo "<select name='district'>\n";
echo "<option value='0'>========= เลือกตำบล =========</option>\n";
$result=mysql_db_query($dbname,"SELECT * FROM district WHERE AMPHUR_ID= '$val' ORDER BY DISTRICT_NAME");
while($row = mysql_fetch_array($result)){
echo "<option value=\"$row[DISTRICT_ID]\" >$row[DISTRICT_NAME]</option> \n" ;
}
}
echo "</select>\n";
echo mysql_error();
closedb();
?>
ช่วยแนะนำหนูด้วยค่ะ
Tag : PHP, MySQL
|
|
|
|
|
|
Date :
2016-03-30 16:30:19 |
By :
pornwilaip |
View :
1174 |
Reply :
1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 04
|