Reputation: 2589
I had implemented a preview button and a save button in a profile page, submitting a form to two different pages. One with the code to save data, and one where is a fake page that show the informations as the original page will show. This is the code
<form id="personal-information-form" method="post" enctype="multipart/form-data">
<!--Some code to recollect info -->
<script type="text/javascript">
function submitForm(action)
{
document.getElementById('personal-information-form').action = action;
document.getElementById('personal-information-form').setAttribute("target", "_blank");
document.getElementById('personal-information-form').submit();
}
</script>
<p><input type="submit" name="preview-info" class="my-buttons" onclick="submitForm('url_to_preview')" value="Preview" >
<span><input type="submit" name="save-info" class="my-buttons" value="Save"></span></p>
</form>
If I click once on one of the two buttons, all work fine. If I click in the preview first and then in the save button, the save button don't save the info, instead show me the preview page. Why? How can I solve it?
Upvotes: 0
Views: 32
Reputation: 6684
When you click the Preview button you are permanently setting the action
attribute of the form
to the preview URL.
Generally I would prefer to do something like this with AJAX rather than with form hackery, but you could amend your current solution like this:
function submitForm(action, target) {
var form = document.getElementById('personal-information-form');
var originalTarget = form.target;
var originalAction = form.action;
form.setAttribute("target", target || "_self");
form.action = action;
form.submit();
form.target = originalTarget;
form.action = originalAction;
return false;
}
This will restore the current action and target after submitting. (Haven't tested; let me know how this goes).
Then your onclick
for the Preview button should say submitForm('url_to_preview', '_blank')
.
Upvotes: 1