PHP 表单提交会导致整个文件刷新吗? [英] Do PHP form submissions cause the whole file to refresh?
本文介绍了PHP 表单提交会导致整个文件刷新吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
将使用 导致用户点击提交时整个页面刷新?还是只是 PHP 部分?
Will using <form action = "<?php echo $_SERVER['PHP_SELF']; ?>" method = "post">
cause the whole page to refresh when the user clicks submit? Or just the PHP part?
推荐答案
页面通常会在提交表单后重新加载以显示提交后收到的回复形式.
The page usually will reload after submitting a form to display the response that is received after submitting the form.
为了防止这种情况,您有两种方法:
To prevent that you got two ways:
- 在表单标签中使用
target=_blank
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" target="_blank">
- 使用
event.preventDefault();
来阻止表单的默认操作,然后使用fetch
手动发送数据,或者如果您使用 JQuery,您可能会使用ajax
.(我将使用 fetch 标准 API)
- Use
event.preventDefault();
to prevent the default action of form and then send data manually withfetch
or if you use JQuery you might go withajax
. (I will go with fetch standard API)
document.getElementByTagName("form")[0].addEventListener('submit', function(event) {
event.preventDefault();
const url = 'https://randomuser.me/api';
// The data we are going to send in our request
const data = {
name: 'Sara'
}
// The parameters we are gonna pass to the fetch function
const fetchData = {
method: 'POST',
body: data,
headers: new Headers()
}
fetch(url, fetchData)
.then(function() {
// Handle response you get from the server
});
});
这篇关于PHP 表单提交会导致整个文件刷新吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文