0

I'm trying to figure out how to capture data from a form that exists inside a function, and then use it inside another php function. Here's the setup for my .php file:

function setPrice( ) { I want to store the form data inside $variables here }

function shortcode( ) { the form is inside this function; there is an input field whose data I would like to use in setPrice }

The form is in one page of my website. When the user submits the form, they are taken to another page (a cart page).

I've tried using $_POST['input-name'], but that hasn't worked. I read about using sessions, but I don't know how to implement that in my case since I'm using two functions there.

Can anyone help me out? Thanks!

1

You could use Wordpress Transients to temporarily set a variable in the WP database with one function, and get that variable with another function.

function setPrice(){
    $myPrice = $_POST['price'];
    set_transient( 'ex1_temp_price', $myPrice, 28800 ); // Site Transient
}

// this function can be kicked off the shortcoe, within the transient timeout
function use_price(){
    $price = get_transient('ex1_temp_price')*1;
    echo "<p>Your price is <input type=\"text\" name=\"price\" value=\"$price\" />.</p>";
}

Transients are only good for as many seconds as you designate. This example presents an 8hr expiry. If you need to get that value any later than 8hrs, you should create a custom meta_value and store that value to the post_meta table.

If, at some point, you decide to look into php sessions, W3Schools has a very nice example that would probably fit your need. Just remember to destroy your session when it is no longer needed.

Hope this gives you the answer you needed!

  • Thank you for the response. I actually solved the problem on my own using sessions, but I marked your response as correct because you mentioned it as well. Not sure if the community would allow that, but whatever haha. For the mods. Feel free to remove my post if you'd like. I've never heard of get_transient (only 4 months into wordpress), so perhaps this might help others in the future? – sansae Aug 27 at 1:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.