Skip to main content
Inspiring
February 18, 2019
Answered

Fillable Form: How to Include Multiple Actions for Submit Button?

  • February 18, 2019
  • 4 replies
  • 3123 views

Right now when a customer hits "SUBMIT", the form he filled out gets emailed to me and a paypal page opens up.

But I added several options so what I need (ideally) is if a customer checks only one field and hits SUBMIT, he is taken to paypal page one.

If he checks two fields and hits SUBMIT he is taken to paypal page two.

And the same goes for 3 other options.

Any ideas?

    This topic has been closed for replies.
    Correct answer osgood_

    OK, I simplified the procedure (hopefully). This is the order page and when SUBMIT is hit, the user is directed to the appropriate payment page. However I ALSO need the filled out form sent to my email address. Any ideas?

    Order Form


    sneedbreedley  wrote

    OK, I simplified the procedure (hopefully). This is the order page and when SUBMIT is hit, the user is directed to the appropriate payment page. However I ALSO need the filled out form sent to my email address. Any ideas?

    Order Form

    The only way to do that is to use a very simple cart. You only need the form sent to you because at the moment you have no way of finding out what combination of products have been selected by the user, that is the job of paypal and paypal will send you a confirmation email detailing the product/s selected AFTER a payment has been made.

    Below is the form code you need. Each product is stored in a php array - name="products[]" - see below, if selected.

    <form name="products" action="product_process.php" method="post">

    <input type="checkbox" value="Window Sticker" id="window_sticker" name="products[]"><label for="window_sticker">Window Sticker</label>

    <br>

    <input type="checkbox"  value="Car Shipper" id="car_shipper" name="products[]"><label for="car_shipper">Car Shipper</label>

    <br>

    <input type="checkbox" value="Dealer Invoice" id="dealer_invoice" name="products[]"><label for="dealer_invoice">Dealer Invoice</label>

    <br>

    <input type="checkbox" value="Retail Order Form" id="retail_order_form" name="products[]"><label for="retail_order_form">Retail Order Form </label>

    <br>

    <input type="checkbox" value="Tank Sticker (1967-1973 Corvette Only)" id="tank_sticker" name="products[]"><label for="tank_sticker"> Tank Sticker (1967-1973 Corvette Only) </label>

    <br>

    <input type="submit" name="submit" value="Submit">

    </form>

    Below is the php code that gets the chosen products from the form checkboxes. Insert this in a file named 'product_process.php' as that is the file name the form points to. Style it visually to suit what you need.

    The products chosen are appended to the $products variable. We count the number of products chosen and append that to the variable $numberOfProducts - this is used to determine what the value should be for the paypal amount field.

    We then loop through the selected products and assign the values to the appropriate paypal hidden fields. You need to use your paypal business email address in place of YOUR-PAYPAL-EMAIL-ADDRESS and your countries currency code in place of YOUR-COUNTRY-CURRENCY-CODE

    This negates the necessity to have 5 seperate pages with 5 different paypal buttons.

    TEST THIS FUNCTIONALITY OUT BEFORE DEPOLYING IT LIVE TO MAKE SURE IT DOES WHAT YOU NEED.

    <?php

    $products = $_POST['products'];

    $numberOfProducts = count($products);

    echo "<h3>Selected Products $numberOfProducts</h3>";

    echo "<ul>";

    foreach($products as $selectedProducts) {

    echo "<li>$selectedProducts</li>";

    }

    echo "</ul>";

    ?>

    <form action="https://www.paypal.com/cgi-bin/webscr" method="post">

    <input type="hidden" name="cmd" value="_cart">

    <input type="hidden" name="upload" value="1">

    <input type="hidden" name="business" value="YOUR-PAYPAL-EMAIL-ADDRESS">

    <input type="hidden" name="currency_code" value="YOUR-COUNTRY-CURRENCY-CODE">

    <?php

    $ii=1;

    foreach($products as $item) {

    echo "

    <input type='hidden' name='quantity_$ii' value='1' >

    <input type='hidden' name='item_name_$ii' value='{$item}' >";

    ?>

    <?php if($numberOfProducts === 1) {

    echo "<input type='hidden' name='amount_$ii' value='95' >";

    }

    elseif($numberOfProducts === 2) {

    echo "<input type='hidden' name='amount_$ii' value='90' >";

    }

    elseif($numberOfProducts === 3) {

    echo "<input type='hidden' name='amount_$ii' value='75' >";

    }

    elseif($numberOfProducts === 4) {

    echo "<input type='hidden' name='amount_$ii' value='75' >";

    }

    elseif($numberOfProducts === 5) {

    echo "<input type='hidden' name='amount_$ii' value='75' >";

    }

    ?>

    <?php

    $ii++;

    }

    ?>

    <input type="submit" id="check_out" value="CHECK OUT">

    </form>

    4 replies

    Legend
    February 18, 2019

    sneedbreedley  wrote

    Right now when a customer hits "SUBMIT", the form he filled out gets emailed to me and a paypal page opens up.

    But I added several options so what I need (ideally) is if a customer checks only one field and hits SUBMIT, he is taken to paypal page one.

    If he checks two fields and hits SUBMIT he is taken to paypal page two.

    And the same goes for 3 other options.

    Any ideas?

    When you say 'paypal page one' what do you mean - normally a paypal account is associated with an email address and will go through to the checkout gateway, according to that email address, not according to how many checkboxes are checked.

    If you mean you want to be taken to an internal paypal page on your website:

    paypal_1.php

    paypal_2.php

    paypal_3.php

    paypal_4.php

    paypal_5.php

    Then use the solution Energize provided, although you could streamline it a bit as below:

    $( document ).ready(function() {

    $('#checkbox_container input').change(function() {

    var countChecked = $("#checkbox_container input:checkbox:checked").length;

    $('form').attr('action', 'paypal_' + countChecked + '.php');

    });

    });

    Energizes if/else if code is a bit more 'human' readable and understandable if you're not too familiar with coding.

    So just to clarify either solution will work:

    if 1 random checkbox is checked and the form button is clicked paypal_1.php will be evoked

    if 2 random checkboxes are checked and the form button is clicked paypal_2.php will be evoked

    if 3 random checkboxes are checked and the form button is clicked paypal_3.php will be evoked

    etc

    Inspiring
    February 18, 2019

    Where does the script go?

    In the following section near the top of the page?:

    <script src="css-winvoices/respond.min.js"></script>

    </head>

    <body>

    <div class="gridContainer">

    <div id="logo"><a href="index.php"><img src="images/logo-59corvette.jpg" alt="1959 Corvette Window Sticker Options"></a></div>

        <?php include('navigation.php'); ?>  

    <div>

      <div id="text1">

    <form action="contact.php" method="post" name="form4">

    or somewhere on the contact.php page?

    Legend
    February 18, 2019

    The script would go in the same page as your form that has the checkboxes:

    You can insert the <script></script> block before the closing </head> tag or even include it before the closing </body> tag of your page. It's assumed you have the jQuery library linked to the page.

    <script>

    $( document ).ready(function() {

    $('#checkbox_container input').change(function() {

    var countChecked = $("#checkbox_container input:checkbox:checked").length;

    $('form').attr('action', 'paypal_' + countChecked + '.php');

    });

    });

    </script>

    Wrap your form in a <div> with the id of 'checkbox_container': (You can rename this if you want but then dont forget to re-name the reference to it in the <script> block. You obviously leave the 'action' of the form blank as that will be generated automatically by the <script>

    <div id="checkbox_container">

    <form action="" method="post" name="form4">

    CHECKBOXES GO HERE

    </form>

    </div>

    <!-- end checkbox_container -->

    Inspiring
    February 18, 2019

    Here is an example page. At the very bottom of the page, (before the Personal Info section), are the check boxes are for the documents requested. When SUBMIT is hit, I need the page to jump to one of 5 paypal pages depending on how many boxes are checked.

    1959 Corvette Options

    Legend
    February 18, 2019

    Just the five checkboxess are we talking about - there's a larger batch of checkboxed further up too?

    Paul-M - Community Expert
    Legend
    February 18, 2019

    Another jQuery example (put your checkboxes inside a container with the id=checkbox_container), checks for more than 1 checkbox checked:

    $('#checkbox_container input').change(function() {

       if ($("#checkbox_container input:checkbox:checked").length > 1) {

          $('form').attr('action', 'action1.php');

       } else {

          $('form').attr('action', 'action2.php');

       }

    });

    Post your full code or llink then we give you a full solution ....

    Paul-M - Community Expert
    Legend
    February 18, 2019

    this does it based on whether one checkbox is checked or not:

    <script>
    $
    ('#checkboxid').on('change', function(){
      
    if ($(this).is(':checked')) {
      $
    ('form').attr('action', 'action1.php');
      
    } else {
      $
    ('form').attr('action', 'action2.php');
      
    }
    });
    </script>

    Are we talking about a specefic checkbox or any one of several ??? Also it'll be be easier for people to help if you can post the entire page or a link to the page....

    Paul-M - Community Expert