doudi2431 2016-10-17 15:16
浏览 45

jQuery ajax和php iframe。 形成目标问题

overflowers here is the daily question. =)

Use case & problem description:

  1. I'm submitting a form via jQuery $.ajax({...})
    • The ajax post call trigger an SQL insert done by php.
    • The SQL query is executed successfully and can be checked on the DB.
  2. The iframe which is used to display the result didn't get update.
    • original button attached to form used target="id_of_iframe"*
  3. The post destination is in the same url, same target (my_iframe)

So,

  • How do I update the iframe with the result data of the $.ajax post?

  • Should I add some other controls?

  • Some other form submission method with editable post data compliant with iframe?

Thanks to all in advance, have a nice coding-day :)

The code:

HTML

<html>
  <head>
    ...
  </head>
  <body>
    <!--the iframe -->
    <iframe src ="iframe.php" 
      class="my_iframe" id="my_iframe" 
      name="my_iframe" frameborder="0" allowtransparency="true">
    </iframe>
    <!--the form -->
    <form id="my_form" action="iframe.php" 
     method="post" target="my_iframe">
       <!-- some input fields -->
       <button onclick="my_function"></button>
    </form>
  </body>
</html>

JAVASCRIPT - JQUERY

function my_function(){
   var my_data = .../*go collect form data*/
   $.ajax({
     type: "POST",
     url: "iframe.php",
     data: my_data,
     success:function(response){
         $("#my_frame").html(response);
     }
   });
}

Partial Result:

  1. The chrome console log this warning about jquery.min.js:4

Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help, check https://xhr.spec.whatwg.org/.

  1. Some CSS get buggy and some font/font-weight change

PS In the ajax.success function I've tried this alternative but the issue is the same:

$('#my_iframe').append(result);
  • 写回答

1条回答 默认 最新

  • doutang7707 2016-10-17 15:31
    关注

    To update the iframe you can just update it's src attribute like so:

    $('iframe#my_iframe').attr('src', 'iframe.php');
    

    put this in your ajax success callback. and the iframe will reload.

    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大