简体   繁体   English

阻止HTML表单操作显示在浏览器上,或在执行操作后重定向到另一个页面

[英]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: 这是我想要的:

  • User will click on submit button (Labeled as Go) 用户将单击提交按钮(标记为Go)
  • 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"如果可能的话,必须执行此操作而不显示其内容。
  • I want the user to reach "MyPage.php" safely after "APageICanNotEdit.php" is executed. 我希望用户在执行“ APageICanNotEdit.php”之后安全地到达“ MyPage.php”。
  • I need a solution without changing the URL in action, cause that defeats the purpose. 我需要一个解决方案,而不用更改URL,因为这样做会破坏目的。

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.

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