admin管理员组文章数量:1414852
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!
本文标签: phpCapture form data in one function and use it in another function (same file)
版权声明:本文标题:php - Capture form data in one function and use it in another function (same file) 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745210221a2647839.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论