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!
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!
Share Improve this question asked Aug 24, 2019 at 1:54 sansaesansae 1892 silver badges10 bronze badges1 Answer
Reset to default 1You 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!