WordPress update_user_meta不发布单选按钮值

时间:2016-08-16 20:36:16

标签: javascript php wordpress radio-button

我一直在尝试将自定义表单放在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函数,如果&#34;其他&#34;选中单选按钮。

以下是我尝试更新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');

总而言之,文本输入成功发布,而单选按钮值发布为NULL。我该怎么做才能发布单选按钮值。

提前谢谢。

1 个答案:

答案 0 :(得分:0)

您不能以这种方式混合客户端代码(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']);
    }
}
?>

如果以下情况不起作用,我会进一步排查,但这应该可以解决问题。