How to open a new window on form submit

后端 未结 9 1887
说谎
说谎 2020-11-29 22:36

I have a submit form and want it to open a new window when users submits the form so i can track it on analytics.

Here is the code I\'m using:



        
相关标签:
9条回答
  • 2020-11-29 23:23

    onclick may not be the best event to attach that action to. Anytime anyone clicks anywhere in the form, it will open the window.

    <form action="..." ...
        onsubmit="window.open('google.html', '_blank', 'scrollbars=no,menubar=no,height=600,width=800,resizable=yes,toolbar=no,status=no');return true;">
    
    0 讨论(0)
  • 2020-11-29 23:30

    No need for Javascript, you just have to add a target="_blank" attribute in your form tag.

    <form target="_blank" action="http://example.com"
          method="post" id="mc-embedded-subscribe-form"
          name="mc-embedded-subscribe-form" class="validate"
    >
    
    0 讨论(0)
  • 2020-11-29 23:34

    I found a solution to this also. This page helped me today so, I am re-posting here too.

    /** This is the script that will redraw current screen and submit to paypal. */
    echo '<script>'."\n" ;
    echo 'function serverNotifySelected()'."\n" ;
    echo '{'."\n" ;
    echo '    window.open(\'\', \'PayPalPayment\');'."\n" ;
    echo '    document.forms[\'paypal_form\'].submit();'."\n" ;
    echo '    document.forms[\'server_responder\'].submit();'."\n" ;
    echo '}'."\n" ;
    echo '</script>'."\n" ;
    
    /** This form will be opened in a new window called PayPalPayment. */
    echo '<form action="https://www.sandbox.paypal.com/cgi-bin/webscr" name="paypal_form" method="post" target="PayPalPayment">'."\n" ;
    echo '<input type="hidden" name="cmd" value="_s-xclick">'."\n" ;
    echo '<input type="hidden" name="custom" value="'.$transaction_start.'">'."\n" ;
    echo '<input type="hidden" name="hosted_button_id" value="'.$single_product->hosted_button_id.'">'."\n" ;
    echo '<table>'."\n" ;
    echo '    <tr>'."\n";
    echo '        <td><input type="hidden" name="'.$single_product->hide_name_a.'" value="'.$single_product->hide_value_a.'">Local</td>'."\n" ;
    echo '    </tr>'."\n" ;
    echo '    <tr>'."\n" ;
    echo '        <td>'."\n" ;
    echo '        <input type="hidden" name="'.$single_product->hide_name_b.'" value="'.$single_product->hide_value_b.'" />'.$single_product->short_desc.' $'.$adj_price.' USD'."\n" ;
                    // <select name="os0">
                    //     <option value="1 Day">1 Day $1.55 USD</option>
                    //     <option value="All Day">All Day $7.50 USD</option>
                    //     <option value="3 Day">3 Day $23.00 USD</option>
                    //     <option value="31 Day">31 Day $107.00 USD</option>
                    // </select>
    echo '        </td>'."\n" ;
    echo '    </tr>'."\n" ;
    echo '</table>'."\n" ;
    echo '<input type="hidden" name="currency_code" value="USD">'."\n" ;
    echo '</form>'."\n" ;
    
    /** This form will redraw the current page for approval. */
    echo '<form action="ProductApprove.php" name="server_responder" method="post" target="_top">'."\n" ;
    echo '<input type="hidden" name="trans" value="'.$transaction_start.'">'."\n" ;
    echo '<input type="hidden" name="prod_id" value="'.$this->product_id.'">'."\n" ;
    echo '</form>'."\n" ;
    
    /** No form here just an input and a button.  onClick will handle all the forms */
    echo '<input type="image" src="https://www.sandbox.paypal.com/en_US/i/btn/btn_buynowCC_LG.gif" border="0" alt="PayPal - The safer, easier way to pay online!" onclick="serverNotifySelected()">'."\n" ;
    echo '<img alt="" border="0" src="https://www.sandbox.paypal.com/en_US/i/scr/pixel.gif" width="1" height="1">'."\n" ;
    

    The above code is the code for one button. You press the button and it will redraw the current screen from purchase to pre-approval. At the same time it opens a new window and hands that new window over to PayPal.

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