dongmeirang4679 2016-08-16 20:36
浏览 71
已采纳

WordPress update_user_meta不发布单选按钮值

I have been trying to make a custom form located on a separate page in my WordPress website. I ended up making a plugin that generates the form html and should post entered data in wp_usermeta. There are three radio button options, one of which is "other", that once clicked makes a textbox visible for alternative input.

The issue I have is that radio button values are not posted to wp_usermeta, only textbox input is (if entered).

Here is the html snippet for the form:

<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 attribute is a javascript function that makes a text box visible if "other" radio button is checked.

Here is one of the things I tried (among others) to update 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     
}?>

I call handleDB() from the same page via shortcode:

function shortcodeDB() {
    ob_start();
    displayHTML();
    handleDB();
    return ob_get_clean();
}

add_shortcode('form_handle','shortcodeDB');

To summarize, text input gets posted successfully, while radio button values post NULL. What can I do to post radio button values as well.

Thank you in advance.

  • 写回答

1条回答 默认 最新

  • dt56449492 2016-08-16 20:55
    关注

    You can't mix client side code (javascript) and server side code (PHP) in the that way. The PHP is going to run no matter what in what you have posted. Here is a solution that should work using only 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']);
        }
    }
    ?>
    

    If this doesn't work comment below and I will troubleshoot further but this should do the trick.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。