• Resolved austintxous

    (@austintxous)


    Having spent about a day figuring out how to do this, I thought it might be helpful to others in the WordPress community.

    GOAL: To take the results from a form field and use it on another page.
    In this example, my client wanted users to be able to fill in any amount in a text field to make a Paypal payment. I’m using S2Member plugin to create buttons and hook up the Paypal payments, but they did not offer this functionality –only buttons with hard wired amounts.
    You can see this in action here:
    http://virginiabloom.com/?page_id=250 . But it is LIVE. If you start a paypal payment, it WILL deduct money from your account. Which will make Virginia very happy.
    HOW I SOLVED IT:
    First, I created a form just for this field.
    <form id="myform" action="yourURL.com/?page_id=542" method="post"><span style="font-size:1.5em;">Enter Other Payment Amount $</span><input id="amount" class="left" name="amount" type="text" /><input type="submit" value="Submit" /></form>

    Next, I found a very simple plugin on stackoverflow and adapted it to my needs:

    <?php
    /*
    Plugin name: redirect on post
    Desciption: 
    
    http://stackoverflow.com/questions/13686245/how-to-create-a-custom-url-based-on-dropdown-in-wordpress-form-submission
    I-changed-dropdown-to-field-input
    */
    function redirect_on_submit() {
      // check if the post is set
      if (isset($_POST['amount']) && ! empty ($_POST['amount'])) 
    
    {
        header( "Location: yourUrl.com/?
    
    page_id=542&amount=" . $_POST['amount'] );
      }
    }
    add_action('init', redirect_on_submit);

    NOTE: Change the page URL and ID information in the header to point to the WordPress page you want to send the information to.
    If you don’t know how to manually install a plugin, its very simple.
    Save the snippet as a .php file. Open up your hosting account and find the plugins folder (its in the wp-content folder) Copy the file into the folder.
    Go back to WordPress and look at your plugins. You should see it there. If its not activated, the activate it.

    I installed another plugin:
    Allow PHP in Posts and Pages (Version 3.0.4)

    After installing it, the plugin will appear on the WordPress menu.
    Open it and you can use their snippet maker that will put php into your post inside a short code.
    Configuration:
    Show the snippet not found message: yes (for debugging)
    Use the old (pre.2.2 code: no
    Use the advanced filter method: yes
    Remove all plugin data on install: no
    You should see the code snippet box appear at the bottom of the page.
    My snippet was simple. I just wanted to echo the “amount” parameter from the page url. So it looked like this:

    echo $_GET[‘amount’]?>
    Note that you have to put the ending php tag, but not the beginning tag.
    Now, you just use the shortcode [php function=1] on the page wherever you want to get this parameter.

    Here’s what it looks like on MY results page.

    <h2>Payment of $ [php function=1]</h2>
    <strong><em>Click Paypal button to start payment or cancel.</em></strong>
    [s2Member-PayPal-Button sp="1" ids="xxx" exp="24" desc="Payment" ps="paypal" lc="" cc="USD" dg="0" ns="1" custom="yoururl.com"
    ra="[php function=1]" image="default" output="button" /]

    To make sure that users can only enter in decimal numbers, I used jQuery validation on the form. There are other ways to do it, but this was very simple. Just put this code on the page with the form which I’ve included below.

    <form id="myform" action="http://virginiabloom.com/?page_id=542" method="post"><span style="font-size:1.5em;">Enter Other Payment Amount $</span><input id="amount" class="left" name="amount" type="text" /><input type="submit" value="Submit" /></form>
    
    <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
    <script src="http://jquery.bassistance.de/validate/jquery.validate.js"></script>
    <script src="http://jquery.bassistance.de/validate/additional-methods.js"></script>
    <script>
    $( "#myform" ).validate({
      rules: {
        amount: {
          required: false,
          number: true
        }
      }
    });
    </script>

    That’s it! I hope this helps somebody.

  • The topic ‘URL variables in WordPress — using form results on a result page in WordPress.’ is closed to new replies.