ecmall練習2:使用jQuery:getJSON傳遞資料

來源:互聯網
上載者:User

標籤:src   val   jquer   set   test   ext   oct   調用   指示   

模板檔案:

ecm_member資料表新增一個欄位:balance

<!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>

<script type="text/javascript" src="{lib file=jquery.js}" charset="utf-8"></script>
</head>

<body>

<
table><tr><td>增加餘額:&nbsp;<input type="text" onBlur="add_balance(this,‘{$info.user_id}‘);" size="10" maxlength="5" value="0"></td><td>增加數量:<span id="test">&nbsp;</span></td></tr>
</table>

<script>function add_balance(obj,user_id) {  obj.value = obj.value.replace(/[^\d]/g, ‘‘);//將非數字替換為空白  var balance_add = jQuery(obj).val();//擷取輸入框的值
//調用控制器裡面的函數,傳遞兩個參數 jQuery.getJSON(
‘index.php?app=member&act=add_balance&user_id=‘+user_id+‘&balance_add=‘+balance_add,‘‘,function(json){ if(json.result==1){ //使用控制器傳回來的值,data.variable的形式讀取 alert(‘設定成功‘); jQuery("#test").html(balance_add); } });}</script>
</body>
</html>

控制器裡面定義函數:

 1 function add_balance() { 2         $model_member = & m(‘member‘); 3         $user_id=$_REQUEST[‘user_id‘]?$_REQUEST[‘user_id‘]:‘‘; //GET方式擷取傳遞過來的值 4         $balance_add=$_REQUEST[‘balance_add‘]?$_REQUEST[‘balance_add‘]:‘‘; 5         $user_info = $model_member->get(‘user_id=‘.$user_id); 6         $balance = $user_info[‘balance‘] + $balance_add; 7         $data = array(‘balance‘ => $balance); 8         $result = $model_member->edit($user_id, $data); //寫入資料庫,返回一個值指示是否寫入成功 9         if($result) {10             echo json_encode(array(‘result‘=>1,‘balance‘ =>$balance,)); //傳出兩個參數11         }

 

ecmall練習2:使用jQuery:getJSON傳遞資料

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.