|
|
|
ขอความช่วยเหลือครับ หาค่า Max ...Where ...(มีเงิ่อนไข) คลิกเลือกและมีการ Refresh ทำได้แล้ว แต่แบบไม่ Refresh แต่ใช้เทคนิค JavaScript ทำไม่ถูกครับ |
|
|
|
|
|
|
|
ลองทดสอบดูนะครับ ใช้ jQuery Ajax มาช่วย
example.php
<?php include('config.php');?>
<html>
<head>
<title>ThaiCreate.Com Tutorial</title>
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function(){
$("#lmName1").change(function(){
var val = $(this).val(); //รับค่าจาก select option
$.post('test.php',{'item':val},function(data){ //ส่งค่าไป query database
$("#resp").html(data); //รับค่ากลับเป็น html
});
});
});
</script>
</head>
<body>
<div id="resp"></div>
<form action="page.php" method="post" name="frmMain">
List Menu
<select name="lmName1" OnChange="resutName(this.value);" id="lmName1">
<option value="">-- Please Select Item --</option>
<?php
$strSQL = "SELECT * FROM license_table ORDER BY ID ASC";
$objQuery = mysql_query($strSQL);
while($objResult = mysql_fetch_array($objQuery))
{
if($_GET["item"] == $objResult["license"])
{
$sel = "selected";
}
else
{
$sel = "";
}
?>
<option value="<?php echo $objResult["license"];?>" <?php echo $sel;?>><?php echo $objResult["license"];?></option>
<?php
}
?>
</select>
<input name="txtName" id="txtName" type="text" value="">
</form>
</body>
</html>
<?php
mysql_close();
?>
test.php
<?php
include('config.php');
$strSQL ="SELECT MAX(milesin) as max_milesin FROM ots_table WHERE carlicense = '".$_POST["item"]."' ORDER BY carlicense ASC";
$objQuery = mysql_query($strSQL);
while($objResult = mysql_fetch_array($objQuery)){
$res = $objResult["max_milesin"];
//ส่งค่ากลับ
echo "<script>
$(function(){
$('#txtName').val('$res');
});
</script>";
}
?>
|
|
|
|
|
Date :
2016-06-10 17:48:05 |
By :
Manussawin |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ไม่ได้ลองนะ
exam1.php
Code (PHP)
<html>
<head>
<title>ThaiCreate.Com Tutorial</title>
</head>
<body>
<?php include('config.php');?>
<form action="page.php" method="post" name="frmMain">
List Menu
<select name="lmName1" id="list_name">
<option value=""><-- Please Select Item --></option>
<?php
$str_sql = "SELECT * FROM license_table ORDER BY ID ASC";
$rs = mysql_query($str_sql);
//check ตัวแปร $_GET["item"]
$selected = '';//กำหนดค่าว่างไปก่อน ไปตรวสอบเองทีหลัง
while($row = mysql_fetch_array($rs))
{
//$selected = ($_GET["item"] == $row["license"]) ? ' selected' : '';
echo '<option value="'.$row["license"].'"'.$selected.'>'.$row["license"].'</option>';
}
mysql_close();
?>
</select>
<input name="txtName" type="text" id="txt_name">
</form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#list_name").change(function (){
$.post('get_data.php',{'item': $(this).val()},function(data){
$("#txt_name").val(data);
});
});
});
</script>
</body>
</html>
get_data.php
Code (PHP)
<?php
include('config.php');
$result= mysql_query("SELECT MAX(milesin) as max_milesin FROM ots_table WHERE carlicense = '".$_POST["item"]."' ORDER BY carlicense ASC");
$row = mysql_fetch_row($result);
echo $row[0];//อาจจะ return echo json_encode(array('max' => $row[0]));
?>
json->java script
Code (PHP)
$.post('get_data.php',{'item': $(this).val()},function(data){
$("#txt_name").val(data.max);
});
|
|
|
|
|
Date :
2016-06-11 08:28:29 |
By :
fossil31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Date :
2016-06-11 10:22:04 |
By :
fossil31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Date :
2016-06-13 10:26:03 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 05
|