woff 發表於 2012-8-9 23:42:50

jquery 透過select 把值給MYSQL撈資料 JSON傳值JAVASCRIPT TEXT秀出資

有用過SELECTBOXES都會覺得很方便
但是本人想撈出值直接秀在TEXT欄位裡
因為很多大網站都有此功能
但是就是找不到相關資料
於是本人花了不少時間測試

INDEX.PHP
<script type="text/javascript" src="jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="selectcostomer.js"></script>
<?php
include "global.php";
$sql1 = "SELECT * FROM net_costomer WHERE fristsellid=".$row;
$query1 = $db->query($sql1);
while($row_costomerselect=$db->fetch_array($query1)) {
   $sm_costomerselect[]=array("cmid"=>$row_costomerselect,"name"=>$row_costomerselect);
}
$smarty->assign("sm_costomerselect",$sm_costomerselect);
$smarty->display('index.htm');先引入JQUERY jquery-1.3.2.min.js及selectcostomer.js
如果沒有jquery-1.3.2.min.js請到關網下載
global.php只是DB的基本設定<?php
$mydbhost = "localhost";
$mydbuser = "";
$mydbpw = "";
$mydbname = "";
$mydbcharset = "UTF8";
$db = @mysql_connect($mydbhost,$mydbuser,$mydbpw);
if (!$db) {
exit('Unable connect MYSQL at this time');
}
if (!@mysql_select_db($mydbname)) {
exit ('Unable connect DB at this time');
}
//宣告資料庫要寫入的格式
mysql_query("SET NAMES $mydbcharset", $db);
?>$smarty->assign("sm_costomerselect",$sm_costomerselect);
$smarty->display('index.htm');
這兩行是SMARTY傳值到INDEX.HTM的基本方法


selectcostomer.js$(document).ready(function(){
$("#select1").change(function(){
$.get(
'selectcostomer.php',   //拋值給selectcostomer.php接收
{ 'cmid': $(this).val(), 'lv': 2 },//傳出 cmid及lv LV可不需要
function(data) {
$("#name").val(data.name); //當值傳回時id="name"接收數據
$("#tel").val(data.phone);
$("#address").val(data.address);
},
'json'
);
}).trigger('change');//當id="select1"改變時執行
});
index.htm
<table width="600" border="0" align="center" cellpadding="0" cellspacing="2">
<tr><td> </td></tr>
<tr><td colspan="2"><img src="{$NY_ROOT_PATH}/modules/shopcart/templates/images/shopcart1.jpg" /></td></tr>
<tr><td><table width="600" border="0" align="center" cellpadding="0" cellspacing="2">
<form name="form1" method="post" action="shopcart2.php" onSubmit="return chkinput(this)">
<tr><td height="30" align="right">客戶:</td><td align="left"><select id="select1" name="name"><option value="0">請選擇</option>
{section name="net" loop=$sm_costomerselect}<option value="{$sm_costomerselect.cmid}">{$sm_costomerselect.name}</option>{/section}</select></td></tr>
<tr><td width="250" height="30" align="right">收貨人姓名:</td>
<td width="350" align="left"><input type="text" name="name" size="25" id="name"></td>
</tr>
<tr><td height="30" align="right">   詳細地址:</td>
<td height="30" align="left"><input name="address" type="text" id="address"></td>
</tr>
<tr><td height="30" align="right">   郵遞區號:</td>
<td height="30" align="left"><input type="text" name="post" size="25"></td>
</tr>
<tr><td height="30" align="right">   連絡電話:</td>
<td height="30" align="left"><input type="text" name="tel" size="25" id="tel"></td>
</tr>
</table>selectcostomer.php<?php

// 資料庫設定
include_once ('config.php');
// 預設選項
$data['0'] = '請選擇';

// 只有在 parentId 與 levelNum 都存在的情況下,才進行資料庫的搜尋
if (0 !== (int) $_GET['cmid'] && 0 !== (int) $_GET['lv']) {
$cmid = (int)$_GET['cmid'];
$levelNum = (int) $_GET['lv'];

$query = sprintf("SELECT * FROM net_costomer WHERE cmid = %d" , $cmid);

$result = mysql_query($query);
while ($row = mysql_fetch_array($result)) {

// 將取得的資料放入陣列中
$data[$row['cmid']] = $row['name'];
$data = $row['phone1'];
$data = $row['address1'];
echo json_encode(array('cmid'=>$row['cmid'],'name'=>$row['name'],'phone'=>$row['phone1'],'address'=>$row['address1']));//用JSON接收
}
}
?>這樣就可以讓原本已經註冊的客戶直接把值放到TEXT裡面
就不需要再打一次了


頁: [1]
查看完整版本: jquery 透過select 把值給MYSQL撈資料 JSON傳值JAVASCRIPT TEXT秀出資