1

I have a three-button form. First button "only Save document", second button "only generate PDF" and third button "Save document and Generate PDF".

only Save button - just submitting form - works fine

<form method="post">
  <button type="submit" name="save">
  <input>....
</form>

only Generate PDF button - change form action and target with simple jquery and send it to PDF generator - also works fine

<form method="post">
   <button type="submit" name="generate" id="generate">
   <input>....
</form>

<script>
$('#generate').click(function(){
   $('form').get(0).setAttribute('action', './library/pdf/generator.php');
   $('form').get(0).setAttribute('target', 'blank'); 
   });
</script>

Save document and Generate PDF button - here i need merge both in one on one click... submit + save data and open PHP generator in new tab(window) .. both with same POST data ... Any ideas? I'm stuck here

EDIT: Tried this callback without luck.. it open only pdf generator (im begginer in jquery, so maybe its nonsence)

$(function() {
$('#saveandgenerate').click(function(){
   $('form').get(0).setAttribute('action', './library/pdf/generator.php');
   $('form').get(0).setAttribute('target', 'blank');
   $('form').get(0).submit(function(){ 
      $('form').get(0).setAttribute('action', '');
      $('form').get(0).setAttribute('target', '');
      $('form').get(0).submit();
      }); 
   });
});

2 Answers2

0

I suggest using a simple ajax post than javascript to submit your form.

<form method="post" id="myForm">
   <button type="button"  id="generate" onclick="save_data();">
         Generate PDF
   </button>
   <input>....
</form>
<script>
     function save_data(){
            $.ajax({
                url: 'first/post/location/url',
                type: 'post',
                data: $("#myForm").serialize(),
                success: function (response) {
                    setTimeout(function (){
                       document.getElementById("myForm").submit();
                    }, 1000);
                }

            });
        }
</script>

Hope this helps.

-1

Working solution:

$(function() {
$('#save_and_generate').click(function()
    {
    $('form').get(0).setAttribute('action', './library/pdf/generator.php');
    $('form').get(0).setAttribute('target', 'blank');
    $('form').get(0).submit();
    setTimeout(function() {
        $('form').get(0).setAttribute('action', '');
        $('form').get(0).setAttribute('target', '');
        $("button#save").trigger('click');
        },100); 
    });
});

Submit with "Save and generate PDF" > change form atributes and open corectly new window with PDF > After 100 timeout change form atributes to default > trigger "only Save" button