ユーザーの選択に基づいて特定のdivを更新するために、ページにJavaScriptを追加しました。
この方法を選択したのは、PHPを介してデータベースから情報をロードするだけではなく、AJAXを介してページを開いているときにユーザーがその場で情報を変更できるためです。
divは<div id='fight'>
divです。それが呼び出すupdate_fight()
関数は、以下にある関数です。何か足りないものがある場合に備えて、参照用に.jsファイル全体を含めました。
window.onload = (function(){
update_fight();
});
function get_main(page){
$.ajax({
type: "POST",
async: false,
url: 'http://rickymason.net/d4beta/welcome/loadmain',
dataType: 'json',
data: { page: page },
success: function(content){
var html = '';
html += content['content'];
$('#main').html(html);
}
});
}
function pop_zone(page){
$.ajax({
type: "POST",
async: false,
url: 'http://rickymason.net/d4beta/welcome/popzone',
dataType: 'json',
data: { page: page },
success: function(content){
var html = $(content['content'])
var $dialog = $('<div></div>')
.html(html)
.dialog({
autoOpen: false,
title: 'Select Zone',
modal: true,
width: 800,
resizable: false
});
$dialog.dialog('open');
}
});
}
$(document).ready(function() {
$("#pop_zone").dialog({autoOpen:false});
});
function update_fight() {
$.ajax({
type: "POST",
async: false,
url: 'http://rickymason.net/d4beta/welcome/loadmain',
dataType: 'json',
data: { page: 'get_zone' },
success: function(content){
var html = '';
html += content['content'];
$('#fight').html(html);
}
});
}
function sql_zone(zone) {
$.ajax({
type: "POST",
async: false,
url: 'http://rickymason.net/d4beta/welcome/sql_zone',
dataType: 'json',
data: { zone: zone },
success: function(){
update_fight();
}
});
}
$("#changezone").live('click', function() {
var page = 'change';
pop_zone(page);
});
$(".zone").live("click",function() {
var zone = this.id;
sql_zone(zone);
alert ( "Updated: Zone " + this.id );
});
update_fight()
私のコントローラーを呼び出しますloadmain
:これはここで見ることができます:
public function loadmain()
{
$content = $this->main_model->get_zone();
$page['content'] = $content;
echo json_encode($page);
}
get_zone()
ここで見ることができます:
public function get_zone()
{
$user_id = $this->tank_auth->get_user_id();
$itemroll = $this->db->query("SELECT current_zone, z.name FROM user_load ul
INNER JOIN zones z
ON z.id = ul.current_zone
WHERE ul.user_id = $user_id");
$result = $itemroll->row_array();
$zone['current_zone']= $result['current_zone'];
$zone['name'] = $result['name'];
//HTML Formatting
$content = '';
$content .= "<div id='change'><input type='button' id='changezone' value='Zone'></input></div> ";
$content .= "<div id='zonename'><a href='". base_url()."welcome/fight'>". $zone['name']."</a></div>";
return $content;
}
時折のページの読み込み中、または新しいページの読み込み中にこれが機能しない原因となる可能性のある明白な問題はありますか?
これは間違いなく3つのブラウザーすべてで発生しますが、IE7ではより一貫性があります。