【问题标题】:Appended input form unable to be submitted via POST无法通过 POST 提交附加的输入表单
【发布时间】:2018-09-17 03:56:02
【问题描述】:

我的 HTML 端有这样的表单:

<form method="post">
    <div class="radio">
        <label><input type="radio" class="optradio" name="optMsg" value="user is no longer exist">user is no longer exist.</label>
    </div>
    <div class="radio radioOthersHolder">
        <label><input type="radio" class="radioOthers optradio" name="optMsg" value="0">others</label>
    </div>
    <button type="submit" name="delete" class="btn btn-danger margin-top">
        Submit
    </button>
</form>

这就是我根据单选选择附加输入控件的方式:

$(document).ready(function(){
    $('.optradio').change(function(){
        if($('.radioOthers').is(':checked')) { 
            $('.radioOthersHolder').append('<input type="text" name="txtOthers" class="form-control txtOthers" max="160" placeholder="State your message here." required />');
        } else {
            $('.txtOthers').remove();
        }
    });
});

然后在我的 PHP 端,我尝试获取所有 POST 数据并检查值,但如果我选择其他单选并提交,我的附加输入似乎无法识别。我不确定它会导致什么。

if(isset($_POST['delete'])){
    if(isset($_POST['optMsg']) && !empty($_POST['optMsg'])){
        if($_POST['optMsg'] == 0){
            if(isset($_POST['txtOthers']) && !empty($_POST['txtOthers'])){
                $reasonMsg = $_POST['txtOthers'];
            }
        } else {
            $reasonMsg = $_POST['optMsg'];
        }

        print_r($_POST); exit();
    }
}

【问题讨论】:

    标签: php post input append codeigniter-3


    【解决方案1】:

    一切都很好。但你必须在这一行设置value="1"

    <input type="radio" class="radioOthers optradio" name="optMsg" value="0">others</label>
    

    当您设置value="0" 时,以下条件返回 false,您不会得到结果。

    if(isset($_POST['optMsg']) && !empty($_POST['optMsg'])
    

    注意 isset() 检查变量是否设置。 empty() 检查值是否不为空或 0。 所以value="0" 导致了这个问题。

    【讨论】:

    • 是的,你没看错。我刚刚想通了,所以我将值更改为 others 的字符串。现在一切正常
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-18
    • 2014-05-31
    • 1970-01-01
    • 2013-05-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多