我試圖對數據庫進行ajax調用,並刷新我從數據庫中顯示的列表。其實我需要的是,我有一個數據表,我用PHP顯示它們,現在我想更新我的列表在頁面上,而無需刷新任何時候,當我的表插入一個新的行。PHP/AJAX - 在MySQL中隨時更新列表出現一個新行
示例代碼
foreach($_IL_QUERY_INTERNAL as $_IL_RESULT_DATA){
echo $_IL_RESULT_DATA->il_name; //display all the name from my table
}
例如:
在我的表,我有5名,你可以看到他們的網頁上,現在somene subit一種形式,我在表中插入第六名,並且我想用錫斯名稱更新我的列表,以實時無刷新。
我會很感激任何幫助。
我的完整代碼
<?php
session_start();
wp_enqueue_style('il_style_mode');
wp_enqueue_style('il_admin_style_mode');
wp_enqueue_scripts('jquery');
global $_IL_ID;
global $_IL_LIMIT;
global $_IL_TOTAL;
global $wpdb;
$_IL_TABLE_NAME_INTERNAL = $wpdb->prefix . "il_internal_transfer";
$_SESSION['IL_COUNT_ADMIN'] = 1;
require_once ($_SERVER["DOCUMENT_ROOT"]."/wp-content/plugins/il_mt4_client_area/il_request/il_internal_transfer/il_internal_admin/il_internal_transfer_tabs.php");
$_IL_START = 0;
$_IL_LIMIT = 10;
if(isset($_GET['paged'])){
$_IL_ID = $_GET['paged'];
$_IL_START = ($_IL_ID-1) * $_IL_LIMIT;
}
$_IL_QUERY_INTERNAL = $wpdb->get_results("SELECT * FROM $_IL_TABLE_NAME_INTERNAL ORDER BY il_id DESC LIMIT $_IL_START, $_IL_LIMIT");
$_IL_NUM_ROWS_INTERNAL_ALL = $wpdb->get_var("SELECT COUNT(*) FROM $_IL_TABLE_NAME_INTERNAL");
echo '<span class="il_admin_main_title">';
echo '<span>';
echo '<h2>Internal Transfers</h2>';
echo '</span>';
echo '<span>';
require_once ($_SERVER["DOCUMENT_ROOT"]."/wp-content/plugins/il_mt4_client_area/il_request/il_internal_transfer/il_internal_admin/il_internal_transfer_sub_tabs.php");
echo '</span>';
echo '</span>';
echo '<span class="il_admin_transactions_block" id="il_admin_transactions_block">';
echo '<ul id="list">';
if($_IL_NUM_ROWS_INTERNAL_ALL < 1){
include ($_SERVER["DOCUMENT_ROOT"]."/wp-content/plugins/il_mt4_client_area/il_include/il_messages/il_internal_transfer_sorry.php");
}else{
foreach($_IL_QUERY_INTERNAL as $_IL_RESULT_DATA)
{
?>
<span class="il_admin_transactions_block_raw_block">
<form method="post">
<input type='hidden' name='il_transaction_id' value="<?php echo $_IL_RESULT_DATA->il_id ?>" />
<span class="il_admin_transactions_block_raw">
<span class="il_admin_transactions_block_raw_count"><?php echo $_SESSION['IL_COUNT_ADMIN'] ?></span>
<span class="il_admin_internal_block_raw_full_name">
<?php echo $_IL_RESULT_DATA->il_name ?>
</span>
<span class="il_admin_internal_block_raw_from_to">
<span><?php echo $_IL_RESULT_DATA->il_from_mt4 ?></span> <?php echo $_IL_RESULT_DATA->il_from_mt4_currency ?>
<img src="/wp-content/plugins/il_mt4_client_area/il_images/il_icons/il_gray_left_icon.png" class="il_arrow_between" />
<span><?php echo $_IL_RESULT_DATA->il_to_mt4 ?></span> <?php echo $_IL_RESULT_DATA->il_to_mt4_currency ?>
</span>
<span class="il_admin_internal_block_raw_email"><?php echo $_IL_RESULT_DATA->il_email ?></span>
<span class="il_admin_internal_block_raw_date"><?php echo $_IL_RESULT_DATA->il_date ?></span>
</span>
<span class="il_admin_transactions_block_raw_extras">
<?php if($_IL_RESULT_DATA->il_comments === NULL || empty($_IL_RESULT_DATA->il_comments)){ ?>
<?php } else { ?>
<span class="il_admin_transactions_block_raw_comment">
<span>Comment:</span>
<span><?php echo $_IL_RESULT_DATA->il_comments ?></span>
</span>
<?php } ?>
<?php if($_IL_RESULT_DATA->il_status == 'approved'){ ?>
<?php } else { ?>
<span class="il_admin_transactions_block_raw_extras_status_icon">
<img src="/wp-content/plugins/il_mt4_client_area/il_images/il_icons/il_green_status_<?php echo $_IL_RESULT_DATA->il_status ?>_icon.gif" alt="Processing" title="Processing"/>
</span>
<?php } ?>
<span class="il_admin_transactions_block_raw_extras_ip">
The transaction was done from this ip <?php echo $_IL_RESULT_DATA->il_user_ip ?>
</span>
<span class="il_admin_transactions_block_raw_extras_ip_select">
<?php if($_IL_RESULT_DATA->il_status == 'approved'){ ?>
<span class="il_admin_transactions_block_raw_extras_status_<?php echo $_IL_RESULT_DATA->il_status ?>"><?php echo $_IL_RESULT_DATA->il_status ?></span>
<img src="/wp-content/plugins/il_mt4_client_area/il_images/il_icons/il_green_status_<?php echo $_IL_RESULT_DATA->il_status ?>_icon.gif" alt="Processing" title="Processing" width="20"/>
<?php } else { ?>
<span class="il_admin_transactions_block_raw_extras_update">
<select name="il_status_new">
<option>approved</option>
<option>waiting</option>
<option>rejected</option>
</select>
</span>
<span class="il_admin_transactions_block_raw_extras_update_button">
<input type="submit" name="il_status_update" value="update status" />
<input type="submit" name="il_delete" value="delete" class="il_admin_transactions_delete_button" />
</span>
<?php } ?>
</span>
</span>
<span class="il_clear"></span>
</form>
</span>
<?php
$_SESSION['IL_COUNT_ADMIN']++;
}
}
echo '</ul>';
echo '</span>';
if(isset($_POST['il_status_update'])){
$wpdb->update(
$_IL_TABLE_NAME_INTERNAL,
array(
'il_status' => $_POST['il_status_new'],
),
array('il_id' => $_POST['il_transaction_id']),
array(
'%s',
),
array('%s')
);
};
if(isset($_POST['il_delete'])){
$wpdb->delete($_IL_TABLE_NAME_INTERNAL, array('il_id' => $_POST['il_transaction_id']), array('%d'));
};
$_IL_ROWS_COUNT = mysql_num_rows(mysql_query("select * from $_IL_TABLE_NAME_INTERNAL"));
$_IL_TOTAL = ceil($_IL_ROWS_COUNT/$_IL_LIMIT);
if($_IL_LIMIT < $_IL_ROWS_COUNT){
echo '<span class="il_pagination_block_admin">';
if($_IL_ID > 1)
{
echo "<a href='?page=il_internal_transfer&tab=il_internal_transfer&paged=".($_IL_ID-1)."' class='il_pagination_prev'><span class='il_pagination_prev_icon'></span></a>";
}
echo "<ul class='il_pagination'>";
for($i = 1; $i <= $_IL_TOTAL; $i++)
{
if($i == $_IL_ID) { echo "<li class='il_pagination_current'>".$i."</li>"; }
else { echo "<li><a href='?page=il_internal_transfer&tab=il_internal_transfer&paged=".$i."'>".$i."</a></li>"; }
}
echo "</ul>";
if($_IL_ID != $_IL_TOTAL)
{
echo "<a href='?page=il_internal_transfer&tab=il_internal_transfer&paged=".($_IL_ID + 1)."' class='il_pagination_next'><span class='il_pagination_next_icon'></span></a>";
}
echo "</span>";
}else{
}
?>
<script>
jQuery(document).ready(function($) {
function fetch(){
$.ajax({
url: '/wp-content/plugins/il_mt4_client_area/il_request/il_internal_transfer/il_internal_admin/il_internal_transfer.php',
success: function(data) {
$(data).hide().prependTo("#list").slideDown("slow");
if($("#list li").length > 15){
$('#list li:gt(14)').remove();
}
setTimeout("fetch()", 5000);
}
});
}
});
</script>
你必須實現Ajax推。 – 2014-09-26 11:33:52
嘗試發送事件服務器http://www.html5rocks.com/en/tutorials/eventsource/basics/ – 2014-09-26 11:59:06