更改预加载的内容[AJAX] [英] Change preloaded content [AJAX]
问题描述
我的文件:
signup.php
,form.php
,success.php
,failure.php
.
在signup.php
中有一个区域<div id="myarea"></div>
.默认情况下,通过<?php include ("form.php"); ?>
通过<?php include ("form.php"); ?>
加载form.php
.
In signup.php
there's an area <div id="myarea"></div>
. Inside of this block I load the form.php
by default via <?php include ("form.php"); ?>
.
现在我想显示success.php
或failure.php
注册是否成功.
Now I want to show the success.php
or failure.php
whether the signup was successfull or not.
我的问题:如何替换myarea
中的内容/已加载文件?
(直到现在我还没有写出表单数据发送到的文件.)
My Question: How can I replace the content/loaded file in myarea
?
(Until now I did not wrote the file the data of the form is sent to.)
我是php的新手,依此类推
I'm new to php and so on
推荐答案
这未使用ES2017,但这是一个简单的示例,将为您提供总体思路.
This is not using ES2017, but it is a simple example and will give you the general idea.
我假设您使用的是ajax来验证登录名,所以类似下面的内容.
I assume you are using ajax to verify the login, so something like below.
当您从ajax返回时(也就是说,在ajax .done()
或.success
内部或任何您使用的功能中),可以使用$.load()
加载新内容,并使用$('#myarea').html()
替换#myarea
div的内容,甚至是像$('#someHiddenDiv').show()
这样简单的内容,即可显示以前隐藏的div.
When you come back from the ajax (that is, inside the ajax .done()
or .success
or whatever-you-use function, you can either use $.load()
to load the new content and $('#myarea').html()
to replace the content of the #myarea
div, or even something as simple as $('#someHiddenDiv').show()
to reveal a previously hidden div.
这是一个简单的例子:
var my_id = $('#loginid').val();
var my_pw = $('#loginpw').val();
$.ajax({
type: 'post',
url: 'ajax/login.php',
data: 'id=' +my_id+ '&pw=' +my_pw,
}).done(function(recd){
if (recd==1) {
var newhtml = $.load('success.php');
$('#myarea').html(newhtml);
//-OR-
//$('#myhiddendiv').show();
}else{
$('#loginid').val('');
$('#loginpw').val('');
alert('Please try logging in again');
}
});
现在您看到了一个非常基本的示例,这是我们最近的工作方式:
Now that you see a very basic example, here is how we do it these days:
这篇关于更改预加载的内容[AJAX]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!