简体   繁体   English

在 ajax 发布后使用 php 页面更新 iframe

[英]Update iframe with php page after ajax post

$("form#update").submit(function() {
    var formData = $("form#update").serialize();
        $.ajax({
            type: "POST",
            url: "graph.php",
            data: formData + "&criteria=" + critSelected,
            success: function(data){
                $('div.graph').fadeOut(function(){$('div.graph').html(data).fadeIn();});
            }
        });
    return false;
});

I have this script that responds to a form submit.我有这个响应表单提交的脚本。 graph.php will be a full html page I would like to display without loading the entire page again. graph.php 将是一个完整的 html 页面我想在不再次加载整个页面的情况下显示。 Is there any way for me to change the source of an iframe on the same page as the form to graph.php (say it's inside the div 'graph')?我有什么办法可以将 iframe 的来源与表格更改为 graph.php (比如说它在 div 'graph' 内)? I've tried placing $('iframe').attr('src', url);我试过放置 $('iframe').attr('src', url); in the success function to no avail.在成功 function 无济于事。 Help is appreciated.帮助表示赞赏。 Thanks!谢谢!

You can post directly to iframe using form.target='frame name' Than the result will be loaded in iframe automaticly您可以使用 form.target='frame name' 直接发布到 iframe 比结果将自动加载到 iframe

For example例如

<form method="POST" target="framename">...</form>

If I read what you've done correctly the reason it's not working is because you're trying to use url as a variable where it's the name of a property within ajax options.如果我正确阅读了您所做的事情,那么它不起作用的原因是因为您尝试使用 url 作为变量,它是 ajax 选项中的属性名称。 If you change your code to $("iframe").attr("src", "graph.php");如果您将代码更改为$("iframe").attr("src", "graph.php"); it should work or set alternatively set the url variable.它应该工作或设置或者设置 url 变量。

Concept Fiddle: http://jsfiddle.net/XcnMt/概念小提琴: http://jsfiddle.net/XcnMt/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM