默认选中单选按钮,我想默认显示表单

时间:2017-04-10 05:02:04

标签: javascript php codeigniter validation radio-button

我有一个包含三个表格的注册页面。我想在同一页面注册用户和服务提供商。

1 - 包含两个单选按钮[服务提供商和用户]  2 - 用于在单选按钮1上注册服务提供商的表格。
 3 - 用于在单选按钮2上注册用户的表单。

我通过单选按钮onChange事件实现了功能,在该页面的div标签中打开相应的表单。使用的样式显示:无,并在单选按钮更改时显示。

我在PHP的CodeIgniter框架上。我想对两种形式进行验证。 因此,......

现在的情况是,

我检查了第一个单选按钮。但是我想这样做,默认情况下检查一下,我想只用事件打开第一种形式的服务提供商。除此之外,我可以删除样式显示:无。但我不想用它来做那件事。我想在基于事件的基础上做。

我的收音机按钮形式。

<form>
<input value="1" type="radio" name="formselector" onchange="displayForm(this)" checked="checked"></input>Service Provider
                                    <br><input value="2" type="radio" name="formselector" onchange="displayForm(this)"></input>User

                                </form>

以上单选按钮的JS。

<script type="text/javascript">
        function displayForm(c) {
            if (c.value == "1") {

                document.getElementById("ccform").style.display = 'inline';
                document.getElementById("paypalform").style.display = 'none';
            } else if (c.value == "2") {
                document.getElementById("ccform").style.display = 'none';

                document.getElementById("paypalform").style.display = 'inline';
            } else {
            }
        }
    </script>  

现在有两个表格Div标签。

    <div id="form">
                           <!-- <form role="form" id="form" class="login-form" action="<?php echo base_url(); ?>index.php/Home/add_service_provider" method="post" onsubmit="return fun();">                             
                            --><form style="display:none" id="ccform">
                                <div class="form-group">
                                    <div class="input-icon"> 
                                                                        <!--<i class="styled-select service-select"></i>-->

                                        <i class="fa fa-building"></i>
                                        <select class="form-control" name="city" style="padding-left:38px" >
                                            <option value="">Choose City</option>
                                            <?php
                                            $i = 0;
                                            foreach ($city as $row) {
                                                ?>
                                                <option class="subitem"  name="city" value="<?php echo $row->city_name ?>"><?php echo $row->city_name ?></option>
                                            <?php } ?>     
                                        </select>  

                                    </div>
                                </div>
                                <div class="form-group">
                                    <div class="input-icon">
                                        <i class="fa fa-tint"></i>
                                        <select class="form-control" name="area" style="padding-left:38px" >
                                            <option value="">Choose Area</option>
                                            <?php
                                            $i = 0;
                                            foreach ($areaname as $row) {
                                                ?>
                                                <option class="subitem" name="area" value="<?php echo $row->area_name ?>"><?php echo $row->area_name ?></option>
                                            <?php } ?>     
                                        </select>   
                                    </div>
                                </div>
                                <div class="form-group">
                                    <div class="input-icon">
                                        <i class="fa fa-user"></i>
                                        <select class="form-control" name="service" style="padding-left:38px" >
                                            <option value="">Choose Service</option>
                                            <?php
                                            $i = 0;
                                            foreach ($service as $row) {
                                                ?>
                                                <option class="subitem" name="service" value="<?php echo $row->category_name ?>"><?php echo $row->category_name ?></option>
                                            <?php } ?>     
                                        </select>   
                                    </div>
                                </div>
                                <div class="form-group">
                                    <div class="input-icon">
                                        <i class="icon fa fa-user"></i>
                                        <input type="text" id="username" class="form-control" name="username[]" placeholder="First Name">
                                    </div>
                                </div>
                                <div class="form-group">
                                    <div class="input-icon">
                                        <i class="icon fa fa-user"></i>
                                        <input type="text" id="lastname" class="form-control" name="username[]" placeholder="Last Name" >
                                    </div>
                                </div>                              
                                <div class="form-group">
                                    <div class="input-icon">
                                        <i class="icon fa fa-envelope"></i>
                                        <input type="email" id="email" class="form-control" name="email" placeholder="Email Address">
                                    </div>
                                </div> 
                                <div class="form-group">
                                    <div class="input-icon">
                                        <i class="icon fa fa-unlock-alt"></i>
                                        <input type="password" id="password" name="password" class="form-control" placeholder="Password" >
                                    </div>
                                </div>  
                                <div class="form-group">
                                    <div class="input-icon">
                                        <i class="icon fa fa-unlock-alt"></i>
                                        <input type="password" id="cpassword" name="cpassword" class="form-control" placeholder="Retype Password" >
                                    </div>
                                </div>
                                <div class="checkbox form-group">
                                    <input type="checkbox" id="remember" name="remember" value="forever" style="float: left;">
                                    <label for="remember">By creating account you agree to our Terms & Conditions</label>
                                </div>
                                <button type="submit" class="btn btn-common log-btn" id="submit">Register</button>
                            </form>
<!-- <form role="form" class="login-form" method="post" action="<?php echo base_url(); ?>index.php/Home/add_user" onsubmit="return fun();"> -->
                            <form style="display:none" id="paypalform">    
                                <div class="form-group">
                                    <div class="input-icon">
                                        <i class="icon fa fa-user"></i>
                                        <input type="text" id="username" class="form-control" name="username[]" placeholder="First Name" required>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <div class="input-icon">
                                        <i class="icon fa fa-user"></i>
                                        <input type="text" id="lastname" class="form-control" name="username[]" placeholder="Last Name" required>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <div class="input-icon">
                                        <i class="icon fa fa-envelope"></i>
                                        <input type="email" id="email" class="form-control" name="email" placeholder="Email Address" required>
                                    </div>
                                </div> 
                                <div class="form-group">
                                    <div class="input-icon">
                                        <i class="icon fa fa-unlock-alt"></i>
                                        <input type="password" class="form-control" id="Password" name="Password" placeholder="Password" required>
                                    </div>
                                </div>  
                                <div class="form-group">
                                    <div class="input-icon">
                                        <i class="icon fa fa-unlock-alt"></i>
                                        <input type="password" name="cPassword" id="cPassword" class="form-control" placeholder="Retype Password" required>
                                    </div>
                                </div>                 
                                <div class="checkbox">
                                    <input type="checkbox" id="remember" name="rememberme" value="forever" style="float: left;" required>
                                    <label for="remember">By creating account you agree to our Terms & Conditions</label>
                                </div>
                                <button type="submit" class="btn btn-common log-btn" id="register">Register</button>
                            </form>


                        </div>

最重要的是..我想用CodeIgniter Standard进行验证。 我的问题是,

假设一个场景......目前我正在填写用户注册表。现在点击提交按钮。它将用于验证检查。假设验证失败。它将重定向到此注册页面,但由于未选中单选按钮,因此不会显示用户表单并显示验证错误。

希望你理解我的问题。

请承担css和所有这些事情。

感谢。

2 个答案:

答案 0 :(得分:1)

单选按钮代码:

 <form>
        <input value="1" type="radio" id="chk1" name="formselector" onchange="displayForm(this)" checked="checked"></input>Service Provider
        <br><input value="2" id="chk2" type="radio" name="formselector" onchange="displayForm(this)"></input>User

</form>

Html表格

<div id="form">
    <form style="display:none" id="ccform">
        <div class="form-group">
           <div class="input-icon"> 
               <i class="fa fa-building"></i>
                <select class="form-control" name="city" style="padding-left:38px" >
                 <option value="">Choose City</option>
                 <?php
                 $i = 0;
                 foreach ($city as $row) {
                 ?>
                 <option class="subitem"  name="city" value="<?php echo $row->city_name ?>"><?php echo $row->city_name ?></option>
                 <?php } ?>     
                 </select>  
                          </div>
                    </div>
                    <div class="form-group">
                        <div class="input-icon">
                            <i class="fa fa-tint"></i>
                            <select class="form-control" name="area" style="padding-left:38px" >
                                <option value="">Choose Area</option>
                                <?php
                                $i = 0;
                                foreach ($areaname as $row) {
                                ?>
                                <option class="subitem" name="area" value="<?php echo $row->area_name ?>"><?php echo $row->area_name ?></option>
                                <?php } ?>     
                            </select>   
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="input-icon">
                            <i class="fa fa-user"></i>
                            <select class="form-control" name="service" style="padding-left:38px" >
                                <option value="">Choose Service</option>
                                <?php
                                $i = 0;
                                foreach ($service as $row) {
                                ?>
                                <option class="subitem" name="service" value="<?php echo $row->category_name ?>"><?php echo $row->category_name ?></option>
                                <?php } ?>     
                            </select>   
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="input-icon">
                            <i class="icon fa fa-user"></i>
                            <input type="text" id="username" class="form-control" name="username[]" placeholder="First Name">
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="input-icon">
                            <i class="icon fa fa-user"></i>
                            <input type="text" id="lastname" class="form-control" name="username[]" placeholder="Last Name" >
                        </div>
                    </div>                              
                    <div class="form-group">
                        <div class="input-icon">
                            <i class="icon fa fa-envelope"></i>
                            <input type="email" id="email" class="form-control" name="email" placeholder="Email Address">
                        </div>
                    </div> 
                    <div class="form-group">
                        <div class="input-icon">
                            <i class="icon fa fa-unlock-alt"></i>
                            <input type="password" id="password" name="password" class="form-control" placeholder="Password" >
                        </div>
                    </div>  
                    <div class="form-group">
                        <div class="input-icon">
                            <i class="icon fa fa-unlock-alt"></i>
                            <input type="password" id="cpassword" name="cpassword" class="form-control" placeholder="Retype Password" >
                        </div>
                    </div>
                    <div class="checkbox form-group">
                        <input type="checkbox" id="remember" name="remember" value="forever" style="float: left;">
                        <label for="remember">By creating account you agree to our Terms & Conditions</label>
                    </div>
                    <button type="submit" class="btn btn-common log-btn" id="submit">Register</button>
                </form>
                <form style="display:none" id="paypalform">    
                    <div class="form-group">
                        <div class="input-icon">
                            <i class="icon fa fa-user"></i>
                            <input type="text" id="username" class="form-control" name="username[]" placeholder="First Name" required>
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="input-icon">
                            <i class="icon fa fa-user"></i>
                            <input type="text" id="lastname" class="form-control" name="username[]" placeholder="Last Name" required>
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="input-icon">
                            <i class="icon fa fa-envelope"></i>
                            <input type="email" id="email" class="form-control" name="email" placeholder="Email Address" required>
                        </div>
                    </div> 
                    <div class="form-group">
                        <div class="input-icon">
                            <i class="icon fa fa-unlock-alt"></i>
                            <input type="password" class="form-control" id="Password" name="Password" placeholder="Password" required>
                        </div>
                    </div>  
                    <div class="form-group">
                        <div class="input-icon">
                            <i class="icon fa fa-unlock-alt"></i>
                            <input type="password" name="cPassword" id="cPassword" class="form-control" placeholder="Retype Password" required>
                        </div>
                    </div>                 
                    <div class="checkbox">
                        <input type="checkbox" id="remember" name="rememberme" value="forever" style="float: left;" required>
                        <label for="remember">By creating account you agree to our Terms & Conditions</label>
                    </div>
                    <button type="submit" class="btn btn-common log-btn" id="register">Register</button>
                </form>
 </div>

Javascript:

<script type="text/javascript">
                var i= document.getElementById('chk1').checked;
                var j= document.getElementById('chk2').checked;
                if(i==true){
                    document.getElementById("ccform").style.display = 'inline';
                }
                if(j==true){
                    document.getElementById("paypalform").style.display = 'inline';
                }
                function displayForm(c) {
                    if (c.value == "1") {

                        document.getElementById("ccform").style.display = 'inline';
                        document.getElementById("paypalform").style.display = 'none';
                    } else if (c.value == "2") {
                        document.getElementById("ccform").style.display = 'none';

                        document.getElementById("paypalform").style.display = 'inline';
                    } else {
                    }
                }
            </script> 

您可以在这里查看示例

https://codepen.io/sunny_khatri/pen/VpoXoQ

答案 1 :(得分:0)

您将在https://www.formget.com/form-validation-using-codeigniter/

找到有关验证的更多详细信息