Skip to content Skip to sidebar Skip to footer

Submit Form To Another Page (which Is Different From The Page Used In ACTION)

I have a form like this: index.php

Solution 1:

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>

Solution 2:

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

<input type="submit" formaction=”differentThanNormalAction.php”>

Solution 3:

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


Solution 4:

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.


Solution 5:

<form onreturn="someJavascriptFunction()" action="" method="">

creating a js function able to open this preview page


Post a Comment for "Submit Form To Another Page (which Is Different From The Page Used In ACTION)"