doupeng2253 2015-01-27 19:55
浏览 21
已采纳

如何使用AJAX刷新PHP文件

Okay, so I searched the web as much as I could and I couldn't find the solution on my problem. I also typed the question and searched for an answer as I saw similar questions to mine. Didn't help. I tried numerous solutions.

Well I have an index page that loads includes/data.php which loads the data from database and echo the .js format that is then loaded by function and display data on the page, so I have at the end of my index.php something like this:

<script type="text/javascript" src="includes/data.php"></script>

On the same index page I have a form that inserts data to database. If you refresh the page I will see refreshed includes/data.php along with new data I just inputted.

I am trying to implement AJAX so that when I click on the button I insert the data to database (already achieved this) and to refresh content of includes/data.php and index.php so it shows data right away without refreshing the index.php. This is my AJAX code:

$('#addtocal').submit( function() {

    $.ajax({
        url     : $(this).attr('action'),
        type    : $(this).attr('method'),
        data    : $(this).serialize(),
        success : function( data ) {    
                   // This is the part where I am stuck.
                  },
        error   : function(){
                     $(".error").fadeIn(2000);
                     $(".error").fadeOut(2000);
                  }
    });

    return false;
});

Just to mention that #calendar is where main jquery function is loading the html content based on the info from /includes/data.php. Thank you in advance for any help you can provide and let me know if you need any other information from me in order to better assist me.

P.S. I saw many suggested using .load() to load content from includes/data.php but that is not working in my case as the content from includes/data.php needs to serve other jquery function that creates html on the fly and and place it in #calendar

  • 写回答

1条回答 默认 最新

  • dongyan7851 2015-01-27 20:57
    关注

    I got it to work. What I did I inser aditional $.ajax inside the $.ajax and on completion and I called again again the jquery that uses /includes/data.php

    so the code would be something like this:

    $('#addtocal').submit( function() {
    
    $.ajax({
        url     : $(this).attr('action'),
        type    : $(this).attr('method'),
        data    : $(this).serialize(),
        success : function( data ) {    
                   $.ajax({
                            url: "../includes/data.php",
                            dataType: "script",
                            cache: true
                            }).done(function() {
                              // Here I called other jquery function that uses ../includes/data.php
                            });
                  },
        error   : function(){
                     $(".error").fadeIn(2000);
                     $(".error").fadeOut(2000);
                  }
    });
    
    return false;
    });
    

    Thanks for your help anyway @Half Crazed gave me a clue so I get the $.getScript function and saw the way to call .js again so I tried and it worked.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?