admin管理员组文章数量:1323729
I have a page with a list of webinars, and a shortcode that displays a form button labeled "Enroll". The form should simply update the user's WordPress profile and redirect them to a page. I don't understand why I'm not getting this to work:
/*
* Usage: [enroll_form_button redirect="#" webinar="ABC"]
*/
if (!function_exists('enroll_form_button_function')) {
function enroll_form_button_function($atts){
$atts = shortcode_atts(array('redirect' => '#', 'webinar' => '',), $atts);
$redirect_to = $atts['redirect'];
$webinar_code = $atts['webinar'];
$results .='<form id="form-'.$webinar_code.'" method="post" action="'.$redirect_to.'">
<input type="submit" value="Enroll" name="form-'.$webinar_code.'" >
</form>';
if (isset($_POST['form-'.$webinar_code])){
update_user_meta( get_current_user_id(), $webinar_code, 'true');
}
return $results;
}
add_shortcode('enroll_form_button', 'enroll_form_button_function');
} else {
echo "enroll_form_button_function is not available.<br />\n";
}
The parameters, the redirect, and the function to update the user's profile is working fine, but the if-statement doesn't seem to be working. If I take the function out of the if-statement, it updates all webinars in the user's profile, not just the one they are submitting. So, I need to identify the form and only call the action to that form. What am I missing here?
I have a page with a list of webinars, and a shortcode that displays a form button labeled "Enroll". The form should simply update the user's WordPress profile and redirect them to a page. I don't understand why I'm not getting this to work:
/*
* Usage: [enroll_form_button redirect="#" webinar="ABC"]
*/
if (!function_exists('enroll_form_button_function')) {
function enroll_form_button_function($atts){
$atts = shortcode_atts(array('redirect' => '#', 'webinar' => '',), $atts);
$redirect_to = $atts['redirect'];
$webinar_code = $atts['webinar'];
$results .='<form id="form-'.$webinar_code.'" method="post" action="'.$redirect_to.'">
<input type="submit" value="Enroll" name="form-'.$webinar_code.'" >
</form>';
if (isset($_POST['form-'.$webinar_code])){
update_user_meta( get_current_user_id(), $webinar_code, 'true');
}
return $results;
}
add_shortcode('enroll_form_button', 'enroll_form_button_function');
} else {
echo "enroll_form_button_function is not available.<br />\n";
}
The parameters, the redirect, and the function to update the user's profile is working fine, but the if-statement doesn't seem to be working. If I take the function out of the if-statement, it updates all webinars in the user's profile, not just the one they are submitting. So, I need to identify the form and only call the action to that form. What am I missing here?
Share Improve this question edited Sep 14, 2020 at 16:36 Michael asked Sep 14, 2020 at 16:17 MichaelMichael 2811 silver badge14 bronze badges 1 |1 Answer
Reset to default 0Few problems I see here.
1.) You're using concatenation for $results
(ie $results .=
should be $results =
)
2.) You're assuming that $webinar_code
will always be available. If the page they are redirected to is not the same exact page, that will not be the case, or if the page redirected to has the shortcode but different webinar in attributes, will update the wrong one.
I would instead do it like this:
add_shortcode( 'enroll_form_button', 'enroll_form_button_function' );
function enroll_form_button_function( $atts ) {
$atts = shortcode_atts( array( 'redirect' => '#', 'webinar' => '', ), $atts );
$redirect_to = $atts['redirect'];
$webinar_code = $atts['webinar'];
$results = "<form id=\"form-{$webinar_code}\" method=\"post\" action=\"{$redirect_to}\"><input type=\"submit\" value=\"{$webinar_code}\" name=\"webinar_code\" /></form>";
return $results;
}
add_action( 'wp', 'check_enroll_form_submit' );
function check_enroll_form_submit(){
if( ! isset( $_POST['webinar_code'], $_POST['webinar_nonce'] ) ){
return;
}
$webinar_code = sanitize_text_field( $_POST['webinar_code'] );
update_user_meta( get_current_user_id(), $webinar_code, 'true' );
}
I did a couple things here:
- Moved the check to update the user's meta to the
wp
action - Sanitized the value sent in
$_POST
(even if you know it's secure, it's ALWAYS good to be in the habit of sanitizing any kind of$_POST
$_GET
or$_REQUEST
data) - Instead of passing the value in the name, just pass it in the value (i think you were overthinking that part)
本文标签: functionsPHP multiple formssame pageisset(POST) not working
版权声明:本文标题:functions - PHP multiple forms, same page, isset($_POST[]) not working? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742120446a2421668.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
$webinar_code
even exist on the page that receives the form? That's a shortcode attribute so wont exist on that page. Therefore yourif
doesn't know what to look for. – vancoder Commented Sep 14, 2020 at 18:05