doulizhi1247 2016-02-18 16:47
浏览 53
已采纳

在Ajax调用之后显示成功消息的问题

I have an html form, wich, Its data is sent trough an AJAX post to a php File.

After the insert to a database, I need to show to the user that, the data he just sent, has been successfully inserted.

Everything works fine, but the real problem is that, the page refreshes so fast that I don't get to even see the content of the message.

Thing's to consider:

  1. After the Ajax call I make a function call to the same page to refresh it, because if not, the page never does.
  2. I have a <div> in this html form, that is hidden, intended to just unhide itself after the response of the server.

So I have two questions:

  1. First of all, how I am doing it, is there an any better way?
  2. Is it possible to make the message persistent after the refreshing of the page?

I leave some of the JS code (to make it short since everything works just fine), and the html part that contains the div here:

$('#MiForm').on('submit', function( event )
{
   //Creating the necessary instances and data
   //Here is where I put different messages for the div "alert"

   xhttp.onreadystatechange = function()
  {  //Response from the server.
      if (xhttp.readyState < 4)
      {// Waiting response from server.
          document.getElementById('Alert').hidden = false;    
          document.getElementById('Alert').innerHTML = "Sending Request...";
      }
      else if (xhttp.readyState === 4) 
      {// 4 = Response from server has been loaded
          if (xhttp.status == 200 && xhttp.status < 300)  // (200-299) is succesful.
             document.getElementById('Alerta').innerHTML = 'Everything OK!';
      }
   };
   //the Send function and data. 

   event.preventDefault(); //-->preventing the submit of the normal form cause I use AJAX.

   window.location.href = '../Ui/WorkerForm.php' //--->this is where I load the same page where Im on, but it seems that it does it too fast.
  }); 

And the HTML:

<div id="Alert" class="alert alert-success" hidden="true" role="alert"></div>

One more thing, the php page that I send data to, does not return any value manually, It just get the data and calls the classes to make the insert.

  • 写回答

1条回答 默认 最新

  • duanquan1876 2016-02-18 16:54
    关注

    First off, you're refreshing the page as soon as you send out the AJAX. You should move the refresh instruction inside the onreadystatechange function listener, so it happens after the AJAX is done, and you have the response.

    If that doesn't help, you could add a small timed delay between displaying the message and refreshing:

    setTimeout(function(){window.location.href = '../Ui/WorkerForm.php';}, TIMEOUT_IN_MILISECONDS);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd