提交表单而不使用PHP更改选项卡窗格

时间:2018-01-23 18:58:12

标签: php html ajax forms

我正在使用Bootstrap 4,我创建了一个双窗格表单;注册和登录表格。

我知道使用AJAX更适合这种情况,它不会使页面刷新,但是,我问是否有任何方法可以实现页面刷新并仅使用PHP预览同一个窗格。 / p>

Form

在代码中使用PHP。有什么办法,当我在注册窗格(右侧的那个)中提交表单时,页面会刷新并保持在同一个窗格中。

<!-- Tabs -->
        <nav class="register-nav">
            <div class="nav nav-tabs nav-justified" id="nav-tab" role="tablist">
                <a class="nav-item nav-link active" id="nav-signin-tab" data-toggle="tab" href="#nav-signin" role="tab">Sign in</a>
                <a class="nav-item nav-link" id="nav-signup-tab" data-toggle="tab" href="#nav-signup" role="tab">Create New Account</a>
            </div>
        </nav>
        <!-- Content of Tabs -->
        <div class="tab-content" id="nav-tabContent">
            <!-- Sign in Tab Pane -->
            <div class="tab-pane fade show active" id="nav-signin" role="tabpanel">
                <form action="" method="POST">
                    <div class="form-group">
                        <div class="row">
                            <div class="col-sm-6">
                                <a class="btn btn-block btn-social btn-facebook">
                                    <span class="fa fa-facebook"></span> Sign in with Facebook
                                </a>
                            </div>
                            <div class="col-sm-6">
                                <a class="btn btn-block btn-social btn-google">
                                    <span class="fa fa-google"></span> Sign in with Google
                                </a>
                            </div>
                        </div>
                    </div>
                    <div class="form-group">
                        <input type="text" class="form-control" placeholder="Username / Email Address">
                    </div>
                    <div class="form-group">
                        <input type="password" class="form-control" placeholder="Password">
                    </div>
                    <div class="form-group text-center mt-5">
                        <input type="submit" class="btn btn-outline-info" value="Sign in">
                    </div>
                </form>
            </div>
            <!-- Sign Up Tab Pane -->
            <div class="tab-pane fade" id="nav-signup" role="tabpanel">
                <form action="" method="POST">
                    <div class="form-group">
                        <input type="text" class="form-control" placeholder="First name" required>
                    </div>
                    <div class="form-group">
                        <input type="text" class="form-control" placeholder="Last name" required>
                    </div>
                    <div class="form-group">
                        <input type="text" class="form-control" placeholder="Username" required>
                    </div>
                    <div class="form-group">
                        <input type="email" class="form-control" placeholder="Email Address" required>
                    </div>
                    <div class="form-group">
                        <input type="password" class="form-control" placeholder="Password" required>
                    </div>
                    <div class="form-group">
                        <input type="password" class="form-control" placeholder="Confirm Password" required>
                    </div>
                    <div class="form-check">
                        <input type="checkbox" class="form-check-input" required>
                        <label class="form-check-label">By registering to
                            <a href="index.html">velocity</a>, you agree to its
                            <a href="terms.html">terms and conditions</a>
                        </label>
                    </div>
                    <div class="form-group text-center mt-5">
                        <input id="register-btn" type="submit" class="btn btn-outline-success" value="Register" disabled>
                    </div>
                </form>
            </div>
        </div>

2 个答案:

答案 0 :(得分:1)

它实际上不是PHP的东西,但您可以使用PHP来操作HTML以将活动类移动到另一个选项卡。

<?php
$signinTabIsActive = "active";
$signupTabIsActive = "";

if(isset($_POST['firstname'])){
   $signinTabIsActive = "";
   $signupTabIsActive = "active";
?>

<!-- Tabs -->
    <nav class="register-nav">
        <div class="nav nav-tabs nav-justified" id="nav-tab" role="tablist">
            <a class="nav-item nav-link <?= $signinTabIsActive; ?>" id="nav-signin-tab" data-toggle="tab" href="#nav-signin" role="tab">Sign in</a>
            <a class="nav-item nav-link <?= $signupTabIsActive; ?>" id="nav-signup-tab" data-toggle="tab" href="#nav-signup" role="tab">Create New Account</a>
        </div>
    </nav>

答案 1 :(得分:0)

添加一个隐藏输入,其值指示应打开哪个窗格。生成表单(使用Php)时,根据$ _POST数组中的此值确定哪个窗格获取活动类