[英]Prevent HTML form action from being displayed on browser, or redirect to another page after the action being executed
Alright let's put it this way: How can I "redirect" a user to another page, "MyPage.php" after submitting a form that looks like this: 好吧,让我们这样说:在提交如下所示的表单后,如何将用户“重定向”到另一个页面“ MyPage.php”:
<form action="http://www.example.com/APageICanNotEdit.php" method="POST">
<input type="submit" name="send" value="Go" />
</form>
Please note that, I don't have control over the URL provided in the action attribute. 请注意,我无法控制action属性中提供的URL。 It's an external source. 这是一个外部来源。 Which means, I cannot edit the "APageICanNotEdit.php" file. 这意味着我无法编辑“ APageICanNotEdit.php”文件。
Here is what I want: 这是我想要的:
action="http://www.example.com/APageICanNotEdit.php"
- this action must be performed, if possible, without displaying the contents of it. action="http://www.example.com/APageICanNotEdit.php"
如果可能的话,必须执行此操作而不显示其内容。 use an hidden parameter like 使用像
<input type="hidden" name="action" value="1" />
Your form will look like this: 您的表单将如下所示:
<form action="http://www.example.com/form-manager.php" method="POST">
</form>
Yout form manager will look like this: Yout表单管理器将如下所示:
if ($_POST['action'] == "1")
require_once('ThePHPFileIDoNotWantToBeLoadedOnBrowser.php");
Seeing your comment, you can do it with an AJAX call: 看到您的评论,您可以通过AJAX调用来完成:
$(document).on('submit' , 'form[action="http://www.example.com/ThePHPFileIDoNotWantToBeLoadedOnBrowser.php"]' , function(e){
var formData = $(this).serialize(); // if you need any of the vars
$.ajax({
url:'someOtherURL.php',
type:'POST',
datatype:'json',
data: formData,
success : function(data){
for(var i = 0; i < data.length; i++){
console.log(data);
}
},
error : function(s , i , error){
console.log(error);
}
});
return true; // keep normal behavior
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.