简体   繁体   中英

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:

<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. It's an external source. Which means, I cannot edit the "APageICanNotEdit.php" file.

Here is what I want:

  • User will click on submit button (Labeled as Go)
  • action="http://www.example.com/APageICanNotEdit.php" - this action must be performed, if possible, without displaying the contents of it.
  • I want the user to reach "MyPage.php" safely after "APageICanNotEdit.php" is executed.
  • I need a solution without changing the URL in action, cause that defeats the purpose.

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:

 if ($_POST['action'] == "1")
       require_once('ThePHPFileIDoNotWantToBeLoadedOnBrowser.php");

Seeing your comment, you can do it with an AJAX call:

$(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
 });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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