提交表格后如何显示模态?

时间:2018-03-18 16:01:27

标签: javascript php modal-dialog

我正在尝试在提交表单后立即显示模态。 这是我在php标签内的代码(检查用户是否选中了一个框,如果是这样,它应该显示在模态中检查的行的值 - 但是我的模态不起作用..只有在我在外面制作另一个按钮时才有效显示它的按钮..):

if(isset($_REQUEST["submit"])) {
    if(isset( $_REQUEST["userSelection"])) {
        $check= $_REQUEST["userSelection"];
    }
    if($check==0) {
        echo "walay sulod";
        // var_dump($check);
    } else
        $a = implode(",",$check);

    $_SESSION["userSelection"] = $a;
    // var_dump($_SESSION["userSelection"]);
?>
<script type="text/javascript"> 
    $(document).ready(function(){
        $('#exampleModalCenter').modal('show');
    });
</script>
<?php
}

?>

这是我的表格,每行包含一个复选框:

<form method="REQUEST" action="" id="formID" name="userCheck">
    <div class="margin_table">
        <table class="table table-bordered table-hover">
            <thead>
                <th>School name</th>
                <th>Location</th>
                <th>Population</th>
                <th>Cost</th>
                <th>TNotchers</th>
                <th>Rating</th>
                <th><input type="submit" name="submit" value="submit" class="btn btn-primary render" data-toggle="modal" data-target="#exampleModalCenter"></th>
                <!-- <th><input type="submit" name="submit" class="btn btn-primary" data-toggle="modal" data-target="#exampleModalCenter"></input></th> -->
            </thead>
            <tbody>
                <?php foreach ($school as $s) {
                    echo '<tr>';
                    echo '<td>'.$s['school_name'].'</td>'; 
                    echo '<td>'.$s['location'].'</td>';
                    echo '<td>'.$s['population'].'</td>';
                    echo '<td>'.$s['cost'].'</td>';
                    echo '<td>'.$s['topnotchers'].'</td>';
                    echo '<td>'.$s['rating'].'</td>';
                    echo '<td><input type="checkbox" name="userSelection[]" id="mymod" onclick="userSelection()" value="'.$s['school_id'].'"</td>';
                    echo '</tr>';
                }
                ?>
            </tbody>
        </table>
    </div>
</form> 

这是我的模态:

<div class="modal fade" id="exampleModalCenter" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="exampleModalLongTitle">Modal title</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                <?php var_dump($_SESSION["userSelection"]); ?> 
                    ...
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                <button type="button" class="btn btn-primary">Save changes</button>
            </div>
        </div>
    </div>
</div>

我希望你能帮助我。谢谢。

1 个答案:

答案 0 :(得分:0)

如果您只想在选中复选框后显示模态,请使用:

echo '<td><input type="checkbox" name="userSelection[]" id="mymod" onclick="$(\'#exampleModalCenter .modal-body\').html(\''. $s['school_id'] .'\');userSelection();" data-toggle="modal" data-target="#exampleModalCenter" value="'.$s['school_id'].'"</td>';

甚至更合适的是在$('#exampleModalCenter').modal('show');

函数中添加userSelection()

如果您想要通过复选框提交表单,请点击$("[name=submit]").click()方法中添加此onclick()

这将显示来自后端的检查值,这更安全。

您还可以向onsubmit()添加的自定义函数添加参数,以告知服务器表单是否已被汇总。