WordPress update_user_meta不发布单选按钮值


WordPress update_user_meta does not post radio button values

我一直在尝试在我的WordPress网站的单独页面上创建一个自定义表单。我最终制作了一个插件,生成表单html,应该在wp_usermeta中发布输入的数据。有三个单选按钮选项,其中一个是"其他",一旦点击它,就会出现一个可供选择输入的文本框。

我的问题是,单选按钮值不张贴到wp_usermeta,只有文本框输入(如果输入)。

下面是这个表单的html片段:
<form name="forming_the_team" method="POST" action="">
    <div id="about">
         A. Tell us about the team. <br/>
         <input type="radio" name="team" value="club" onclick="otherCheck()" id="club"/>Club<br/>
         <input type="radio" name="team" value="class" onclick="otherCheck()" id="class"/>Class<br/>
         <input type="radio" name="team" value="other" id="other-radio" onclick="otherCheck()"/>Other</br>
         <input type="text" name="team2" id="other-box"/><br/>
    </div>
    <br/>
    <input type="submit" value="Submit"/><br/>
</form>

otherCheck() onclick属性是一个javascript函数,当"other"单选按钮被选中时,它会使文本框可见。

下面是我尝试更新wp_usermeta:

的方法之一
 <?php function handleDB() {
        $user_id = get_current_user_id();?>
        <script type="text/javascript">
        if(document.getElementById('club').checked) {
              <?php update_user_meta( $user_id, 'team', $_POST['team']);?>
        }
        if(document.getElementById('class').checked) {
              <?php update_user_meta( $user_id, 'team', $_POST['team']);?>
        } 
        if(document.getElementById('other-radio').checked)  {
              <?php update_user_meta( $user_id, 'team', $_POST['team2']);?>
        }
        </script>
        <?php     
}?>

我通过短代码从同一页面调用handleDB():

function shortcodeDB() {
    ob_start();
    displayHTML();
    handleDB();
    return ob_get_clean();
}
add_shortcode('form_handle','shortcodeDB');

总之,文本输入被成功发布,而单选按钮的值为post NULL。我能做些什么来发布单选按钮的值。

提前感谢。

你不能以这种方式混合客户端代码(javascript)和服务器端代码(PHP)。无论你发布了什么内容,PHP都将运行。下面是一个只使用PHP的解决方案。

<?php function handleDB() {
    $user_id = get_current_user_id();
    if( 'other' == $_POST['team'] ){
        update_user_meta( $user_id, 'team', $_POST['team2']);    
    }else{
        update_user_meta( $user_id, 'team', $_POST['team']);
    }
}
?>

如果这不起作用,我将在下面的注释中进一步排除故障,但这应该可以解决问题。