[英]Jquery load Php file
我目前正在使用Jquery加载功能将php内容加载到Div中。 但是,当我使用php电子邮件形式时,它变成了白色,并且索引页丢失了(我的意思是主要布局)。 无论如何,即使需要执行,我也可以使用Jquery将Insch.php加载回div中吗?
<form id="contactform" method="POST" action="insch.php"> <--- this is my Form
$('.menut').click(function () {
var phpFile = $(this).attr('id') + '.php';
/* alert(phpFile)*/
$.get(phpFile, function (data) {
$('#box1').html(data);
});
return false;
}); <---- my Jquery load function(on index)
我看到您将返回数据直接添加到$("#box1")
。 如果您的php文件返回Html
,则应在ajax请求中设置dataType:'html'
。
抱歉,您的问题有点难以理解。 我有点猜测,但这听起来像是您在询问如何在不刷新页面的情况下提交表单。 如果是这样,您可以执行以下操作:
$('.menut').click(function() {
var phpFile = $(this).attr('id') +'.php';
$('#box1').load(phpFile, function() { // <-- this is your ready function
// once the form has been added to the page,
// add the 'submit' event listener
$('#contactForm').submit(function(e) {
// prevent the form from submitting regularly (causing a page refresh)
e.preventDefault();
// get the data from the form
var data = $(this).serialize();
// submit the form via AJAX and put the response in #box1
$('#box1').load($(this).attr('action'), data);
});
});
});
更新:
ready函数是您在AJAX调用中创建的函数,当内容加载完成时(内容准备就绪时 )将执行该函数。 它也可以称为回调函数, 完成函数或成功函数。
看看我在上面的第四行代码中添加的注释。 我已指出我指的是您的就绪功能。
在您的问题中,您使用了以下方法:
$.get(phpFile, function (data) {
$('#box1').html(data);
});
等效于:
$('#box1').load(phpFile);
这会将来自phpFile
的响应(您的表单) phpFile
到#box1
。 function (data) { ... }
是您可以使用的函数。 那是您应该将submit
事件绑定到表单的地方。
如果您按照我的建议切换到load()
方法,则只需将一个新函数(将是您准备好的函数)作为第二个参数传递给load()
方法,这是我在其中给出的解决方案我的原始答案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.