php - AJAX not updating the list from MySQL

524

i am inserting from a form data into my database, after i create a file with a FOR loop that i display all the records from my table. I am trying to refresh my list with data on a page without refresh every time when there was inserted new data into database.

main.php

<script type="text/javascript" >
    function fetch(){
        jQuery.ajax({
            url: '/ajax.php',
            success: function(data) {
                jQuery(data).hide().prependTo("#list").slideDown("slow");
                if(jQuery("#list li").length > 15){
                    jQuery('#list li:gt(14)').remove();
                }
                setTimeout("fetch()", 1000);
                console.log(data);
            }
        });
    }
</script>

PHP / HTML

<?php
require_once($_SERVER["DOCUMENT_ROOT"]."/wp-blog-header.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 >';
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 id="il_admin_transactions_block">';

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{

  require_once ($_SERVER["DOCUMENT_ROOT"]."/ajax.php");

}

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 >';
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{

}
?>

ajax.php

<?php
require_once($_SERVER["DOCUMENT_ROOT"]."/wp-blog-header.php");
wp_enqueue_style('il_style_mode');
wp_enqueue_style('il_admin_style_mode');
wp_enqueue_scripts('jquery');
global $wpdb;



$_IL_TABLE_NAME_INTERNAL    = $wpdb->prefix . "il_internal_transfer";
$_IL_QUERY_INTERNAL = $wpdb->get_results( "SELECT * FROM $_IL_TABLE_NAME_INTERNAL ORDER BY il_id DESC LIMIT $_IL_START, $_IL_LIMIT");
echo '<ul id="list">';
foreach($_IL_QUERY_INTERNAL as $_IL_RESULT_DATA)
{
  ?>
  <li>
    <span >

    <form method="post">
      <input type='hidden' name='il_transaction_id' value="<?php echo $_IL_RESULT_DATA->il_id ?>" />
      <span >

        <span ><?php echo $_SESSION['IL_COUNT_ADMIN'] ?></span>
        <span >
          <?php echo $_IL_RESULT_DATA->il_name ?>
        </span>
        <span >
          <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" />
          <span><?php echo $_IL_RESULT_DATA->il_to_mt4 ?></span> <?php echo $_IL_RESULT_DATA->il_to_mt4_currency ?>
        </span>
        <span ><?php echo $_IL_RESULT_DATA->il_email ?></span>
        <span ><?php echo $_IL_RESULT_DATA->il_date ?></span>

      </span>

      <span >
        <?php if( $_IL_RESULT_DATA->il_comments === NULL || empty($_IL_RESULT_DATA->il_comments) ){ ?>

        <?php } else { ?>
          <span >
            <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 >
            <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 >
          The transaction was done from this ip <?php echo $_IL_RESULT_DATA->il_user_ip ?>
        </span>

        <span >
        <?php if( $_IL_RESULT_DATA->il_status == 'approved' ){ ?>
          <span ><?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 >
            <select name="il_status_new">
              <option>approved</option>
              <option>waiting</option>
              <option>rejected</option>
            </select>
          </span>

          <span >
            <input type="submit" name="il_status_update" value="update status" />
            <input type="submit" name="il_delete" value="delete" />
          </span>

        <?php } ?>
        </span>

      </span>
      <span ></span>
    </form>

    </span>
  </li>
  <?php
  $_SESSION['IL_COUNT_ADMIN']++;

}

echo '</ul>';

actually what i am trying is every time when new data will be inserted in my table i want to display them automatically in my list using the loop from ajax.php

People are also looking for solutions to the problem: web services - PHP SOAP call issue using the SoapClient class

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.