Submit form to another page (which is different from the page used in ACTION)

前端 未结 5 1905
[愿得一人]
[愿得一人] 2020-12-08 07:21

I have a form like this:

index.php

<
相关标签:
5条回答
  • 2020-12-08 08:06

    Use Javascript to temporarily change the action and target:

    <form method="post" action="send.php" id="idOfForm">
      <textarea name="msg" id="msg"></textarea>
      <input type="submit" value="Send" />
    </form>
    <button onclick="doPreview();">Preview</button>
    <script type="text/javascript">
        function doPreview()
        {
            form=document.getElementById('idOfForm');
            form.target='_blank';
            form.action='preview.php';
            form.submit();
            form.action='send.php';
            form.target='';
        }
    </script>
    
    0 讨论(0)
  • 2020-12-08 08:06
    <form onreturn="someJavascriptFunction()" action="" method="">
    

    creating a js function able to open this preview page

    0 讨论(0)
  • 2020-12-08 08:15

    There is now an attribute for the submit input that handles this:

    <input type="submit" formaction=”differentThanNormalAction.php”>
    
    0 讨论(0)
  • 2020-12-08 08:20

    Give your form an ID (form1). The action of the current form can be controlled like this:

    function setPreview() {
        $('#form1').attr('target','_blank')
        $('#form1').attr('action','http://yourpreviewurl.php')
        $('#form1').submit()
    }
    
    function setSubmit() {
        $('#form1').attr('target','')
        $('#form1').attr('action','http://yourposturl.php')
        $('#form1').submit()
    }
    

    Have two buttons, both type="button", one to call setPreview and another to call setSubmit

    0 讨论(0)
  • 2020-12-08 08:20

    You can use JavaScript to change the action of the form when the button is clicked and then submit it.

    Or simply submit the form via AJAX and then redirect after you get a response.

    0 讨论(0)
提交回复
热议问题