作者petercilee (葉慈)
看板PHP
標題[請益] codeigniter中以ajax方式讀取資料的問題
時間Mon Feb 24 18:49:25 2014
各位高手,對不起,這個問題似乎有點古怪,想請教各位該如何解決。
這是一個簡單的功能,假設今天我要為金庸人物做統計,於是我制定了
四個欄位,分別是「編號」、「姓名」、「幫派」、「武功」,以及兩
個按鈕,分別是「登陸」與「新增」。
然後,當我按下登錄時,這四個欄位的值分別會被記錄在mysql 中,如
果我要新增一筆新資料的話,那麼當我按下新增按鈕時,會去資料庫裡
面搜索出最大值,然後顯示在「武林編號」的那個input框中。
只是我怎麼寫,都沒有作用,對不起,小弟真的不知道是哪裡出錯了,
希望各位高手大德指正,感謝!
(其實我常常一直很擔心這種問題實在是非常簡單,總是羞於啟齒就是
了......只是真的在網路上實在找不太到資訊,只好叨擾各位,恕罪,
恕罪)
以下是view檔案的寫法,將程式碼條列於下(views/kungfu.php):
<div class="hero-unit">
<div style="width:250px;float:left;">
<form id="pr_form" action="<?php echo site_url();?>/static_data/kungfu_act" method="post">
編號:<input id="num" name="num" type="text" class="field_set"><br>
姓名:<input id="name" name="name" type="text" class="field_set"><br>
所屬:<input id="local" name="local" type="text" class="field_set"><br>
武術:<input id="kungfu" name="kungfu" type="text" class="field_set"><br>
</div>
<div style="float:left;">
<span id="loading" style="display:none">Loading!!</span><br>
<span id="complete" style="display:none">Complete!!</span>
</div>
<div style="clear:both;height:50px;padding-top:10px">
<input id="go" name="go" class="btn" type="submit" value="登錄">
<input id="newone" name="newone" class="btn" type="button" value="新增">
</div>
</form>
</div>
以下是controller的寫法,將程式碼條列於下(controllers/static_data.php):
class Static_data extends CI_Controller {
public function kungfu_maxquery()
{
$this->load->model("pr_model");
$data = $this->pr_model->pr_maxquery();
echo json_encode($data);
}
}
以下是model的寫法,將程式碼條列於下(models/pr_model.php):
class Pr_model extends CI_Model {
function __construct()
{
parent::__construct();
$this->load->helper('form');
$this->load->helper('html');
$this->load->database();
}
function pr_maxquery()
{
$this->db->select_max("num");
$maxquery=$this->db->get("kungfu_table");
return $maxquery;
}
以下是js的寫法,將程式碼條列於後(js/try.js):
$("#newone").click(function(){
$.getJSON("<?php echo site_url('static_data/kungfu_maxquery')?>", function (data) {
$("#num").val(data.num);
});
});
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 114.40.121.169
→ DiAdo:js函數加上參數e,getJSON前面加上e.preventDefault();看看 02/24 21:44
推 ayabehaori:感謝您 03/23 02:26