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 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)