如何从操作页面单击后退按钮后转到带有值的页面

时间:2015-11-24 10:00:36

标签: php jquery html ajax html5

我有一个名为 reg_apply_form.php 的表单页面以及名为 preview.php 的操作页面 单击预览按钮完成表单后,我的操作页面显示所有值。如果我认为,我的信息有误,那么我会点击后退按钮。但问题是,如果我点击后退按钮,我的所有值都将删除。如果点击返回按钮,我想支持所有值的表单页面。 我的两页代码如下:

表单页码:

<!DOCTYPE html>
    <head>
    </head>
    <body id="body">
        <form action="preview.php" method="post" class="form-inline vh_pre_reg_form">
            <div class="form-group">
                <label>First Name</label>
                <input name="fname" id="one" type="text" class="form-control" placeholder="Your First Name">
            </div>
            <div class="form-group">
                <label>Last Name</label>
                <input name="lname" id="two" type="text" class="form-control" placeholder="Your Last Name" />
            </div>
            <div class="form-group">
                <label>Email</label>
                <input name="email" id="txtEmail" type="email" class="form-control" placeholder="Your Email" />
            </div>
            <div class="form-group">
                <label>Address</label>
                <input name="address" type="text" id="four" class="form-control" placeholder="Your Address" />
            </div>
            <div class="form-group">
                <label>Phone Number</label>
                <input name="vh_rg_phone" type="text" pattern="[0-9]{6,}" id="vh_rg_phone" class="form-control" placeholder="Your Phone Number" />
            </div>
            <div class="form-group">
                <label>Mobile Number</label>
                <input name="vh_rg_mob" type="number" pattern="\d{11}" id="vh_rg_mob" class="form-control" placeholder="Your Mobile Number" />
            </div>
            <div class="form-group">
                <label>Father's Name</label>
                <input name="vh_reg_father_name" type="text" id="vh_reg_fname" class="form-control" placeholder="Your Father's Name" />
            </div>
            <div class="form-group">
                <label>Father's Contact</label>
                <input name="vh_reg_father_con" type="number" pattern="\d{11}" id="vh_rg_fcontact" class="form-control" placeholder="Your Father's Contact" />
            </div>
            <div class="form-group">
                <label>Mother's Name</label>
                <input name="vh_reg_mother_name" type="text" id="vh_reg_mname" class="form-control" placeholder="Your Mother's Name" />
            </div>
            <div class="form-group">
                <label>Mother's Contact</label>
                <input name="vh_reg_mother_con" type="number" pattern="\d{11}" id="vh_reg_mcontact" class="form-control" placeholder="Your Mother's Contact" />
            </div>
            <div class="form-group">
                <label>Guardian Name</label>
                <input name="vh_reg_gaurdi_name" type="text" id="vh_reg_guardi_name" class="form-control" placeholder="Your Guardian Name" />
            </div>
            <div class="form-group">
                <label>Guardian Contact</label>
                <input name="vh_reg_gaurdi_con" type="number" pattern="\d{11}" id="vh_rg_guardi_contact" class="form-control" placeholder="Your Guardian Contact" />
            </div>
            <div class="form-group table">              
                <label>Date of Birth <strong>*</strong></label>
                    <input type="date" id="birthday" name="birth_date" />           
            </div>
            <div class="form-group">
                <label>Password Number</label>
                <input name="mainpass" type="password" id="mainpass" class="form-control" placeholder="Make Your Password" />
            </div>  
            <div class="form-group">
                <label>Confirm Password</label>
                <input name="conpass" type="password" id="conpass" class="form-control" placeholder="Type Your Password Again" />
            </div>
            <input name="submit" id="btnValidate" type="submit" value="Preview"/>
            <input type="reset" value="Clear All"/>
        </form>
    </body>
</html>

我的操作页面代码:

<?php
    if($_POST && isset($_POST['mainpass'], $_POST['fname'], $_POST['lname'], $_POST['conpass'], $_POST['email'])){
        if($_POST['mainpass'] == $_POST['conpass']){            
            $fname = "$_POST[fname]";
            $lname = "$_POST[lname]";
            $email = "$_POST[email]";
            $address = "$_POST[address]";
            $student_phone = "$_POST[vh_rg_phone]";
            $student_mobile = "$_POST[vh_rg_mob]";
            $religion = "$_POST[religion]";
            $father = "$_POST[vh_reg_father_name]";
            $father_contact = "$_POST[vh_reg_father_con]";
            $mother = "$_POST[vh_reg_mother_name]";
            $mother_contact = "$_POST[vh_reg_mother_con]";
            $guardian = "$_POST[vh_reg_gaurdi_name]";
            $date_of_birth = "$_POST[birth_date]";
            $guardian_contact = "$_POST[vh_reg_gaurdi_con]";
            $password = "$_POST[mainpass]"; 
        } else{header("Location: go.php");}
    }
    else{
        header("Location: no.php");
    }
?>
<!DOCTYPE HTML>
<html lang="en-US">
    <head>
        <title></title>
    </head>
    <body>
        <form action="password.php" method="post" class="form-inline vh_pre_reg_form">
            <div class="form-group">
                <label>First Name</label>
                <span><?php echo $fname; ?></span>
            </div>
            <div class="form-group">
                <label>Last Name</label>
                <span><?php echo $lname; ?></span>
            </div>
            <div class="form-group">
                <label>Email</label>
                <span><?php echo $email; ?></span>
            </div>
            <div class="form-group">
                <label>Address</label>
                <span><?php echo $address; ?></span>
            </div>
            <div class="form-group">
                <label>Phone Number</label>
                <span><?php echo $student_phone; ?></span>
            </div>
            <div class="form-group">
                <label>Mobile Number</label>
                <span><?php echo $student_mobile; ?></span>
            </div>
            <div class="form-group">
                <label>Father's Name</label>
                <span><?php echo $father; ?></span>
            </div>
            <div class="form-group">
                <label>Father's Contact</label>
                <span><?php echo $father_contact; ?></span>
            </div>
            <div class="form-group">
                <label>Mother's Name</label>
                <span><?php echo $mother; ?></span>
            </div>
            <div class="form-group">
                <label>Mother's Contact</label>
                <span><?php echo $mother_contact; ?></span>
            </div>
            <div class="form-group">
                <label>Guardian Name</label>
                <span><?php echo $guardian; ?></span>
            </div>
            <div class="form-group">
                <label>Guardian Contact</label>
                <span><?php echo "here will be date" . $guardian_contact; ?></span>
            </div>
            <div class="form-group table">              
                <label>Date of Birth <strong>*</strong></label>
                    <span><?php echo $date_of_birth;?></span>           
            </div>
            <input name="submit" id="btnValidate" type="submit" value="Submit"/>
            <a href="reg_apply_form.php"><input type="button" value="Back"/></a>
        </form>
    </body>
</html>

我不知道怎么能这样做。请帮我。如果可能的话,请给我完整的代码。 谢谢

1 个答案:

答案 0 :(得分:1)

实现此目的的一种方法是Ajax请求,并在您的实际页面上直接插入响应。

例如:我在主表单周围添加了div,为预览内容添加了div。当表单提交预览时,我隐藏主div并显示带结果的预览div。

现在,当用户点击后退按钮时,我隐藏预览div并显示主div。

<强>更新

主要HTML

<!DOCTYPE html>
    <head>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
        <script type="text/javascript">
            $(document).ready(function() {
                $('input#btnValidate').click( function() {
                    $.ajax({
                        url: 'preview.php',
                        type: 'post',
                        dataType: 'html',
                        data: $('form#vh_pre_reg_form').serialize(),
                        success: function(data) {
                                    $("#containerMain").hide();
                                    $("#containerPreview").show();
                                    $("#containerPreview").html(data);
                                }
                    });
                });
                $(document).on("click","#back", function() {
                    $("#containerPreview").html("").hide();
                    $("#containerMain").show();
                })
            })
        </script>
    </head>
    <body id="body">
        <div id="containerMain">
            <form action="preview.php" method="post" id="vh_pre_reg_form" class="form-inline vh_pre_reg_form">
                <div class="form-group">
                    <label>First Name</label>
                    <input name="fname" id="one" type="text" class="form-control" placeholder="Your First Name">
                </div>
                <div class="form-group">
                    <label>Last Name</label>
                    <input name="lname" id="two" type="text" class="form-control" placeholder="Your Last Name" />
                </div>
                <div class="form-group">
                    <label>Email</label>
                    <input name="email" id="txtEmail" type="email" class="form-control" placeholder="Your Email" />
                </div>
                <div class="form-group">
                    <label>Address</label>
                    <input name="address" type="text" id="four" class="form-control" placeholder="Your Address" />
                </div>
                <div class="form-group">
                    <label>Phone Number</label>
                    <input name="vh_rg_phone" type="text" pattern="[0-9]{6,}" id="vh_rg_phone" class="form-control" placeholder="Your Phone Number" />
                </div>
                <div class="form-group">
                    <label>Mobile Number</label>
                    <input name="vh_rg_mob" type="number" pattern="\d{11}" id="vh_rg_mob" class="form-control" placeholder="Your Mobile Number" />
                </div>
                <div class="form-group">
                    <label>Father's Name</label>
                    <input name="vh_reg_father_name" type="text" id="vh_reg_fname" class="form-control" placeholder="Your Father's Name" />
                </div>
                <div class="form-group">
                    <label>Father's Contact</label>
                    <input name="vh_reg_father_con" type="number" pattern="\d{11}" id="vh_rg_fcontact" class="form-control" placeholder="Your Father's Contact" />
                </div>
                <div class="form-group">
                    <label>Mother's Name</label>
                    <input name="vh_reg_mother_name" type="text" id="vh_reg_mname" class="form-control" placeholder="Your Mother's Name" />
                </div>
                <div class="form-group">
                    <label>Mother's Contact</label>
                    <input name="vh_reg_mother_con" type="number" pattern="\d{11}" id="vh_reg_mcontact" class="form-control" placeholder="Your Mother's Contact" />
                </div>
                <div class="form-group">
                    <label>Guardian Name</label>
                    <input name="vh_reg_gaurdi_name" type="text" id="vh_reg_guardi_name" class="form-control" placeholder="Your Guardian Name" />
                </div>
                <div class="form-group">
                    <label>Guardian Contact</label>
                    <input name="vh_reg_gaurdi_con" type="number" pattern="\d{11}" id="vh_rg_guardi_contact" class="form-control" placeholder="Your Guardian Contact" />
                </div>
                <div class="form-group table">              
                    <label>Date of Birth <strong>*</strong></label>
                        <input type="date" id="birthday" name="birth_date" />           
                </div>
                <div class="form-group">
                    <label>Password Number</label>
                    <input name="mainpass" type="password" id="mainpass" class="form-control" placeholder="Make Your Password" />
                </div>  
                <div class="form-group">
                    <label>Confirm Password</label>
                    <input name="conpass" type="password" id="conpass" class="form-control" placeholder="Type Your Password Again" />
                </div>
                <input name="submit" id="btnValidate" type="button" value="Preview"/>
                <input type="reset" value="Clear All"/>
            </form>
        </div>
        <div id="containerPreview" style="display:none"></div>
    </body>
</html>

<强> PHP

<?php

    $fname = "$_POST[fname]";
    $lname = "$_POST[lname]";
    $email = "$_POST[email]";
    $address = "$_POST[address]";
    $student_phone = "$_POST[vh_rg_phone]";
    $student_mobile = "$_POST[vh_rg_mob]";
    $religion = "$_POST[religion]";
    $father = "$_POST[vh_reg_father_name]";
    $father_contact = "$_POST[vh_reg_father_con]";
    $mother = "$_POST[vh_reg_mother_name]";
    $mother_contact = "$_POST[vh_reg_mother_con]";
    $guardian = "$_POST[vh_reg_gaurdi_name]";
    $date_of_birth = "$_POST[birth_date]";
    $guardian_contact = "$_POST[vh_reg_gaurdi_con]";
    $password = "$_POST[mainpass]"; 

?>
<!DOCTYPE HTML>
<html lang="en-US">
    <head>
        <title></title>
    </head>
    <body>
        <form action="password.php" method="post" class="form-inline vh_pre_reg_form">
            <div class="form-group">
                <label>First Name</label>
                <span><?php echo $fname; ?></span>
            </div>
            <div class="form-group">
                <label>Last Name</label>
                <span><?php echo $lname; ?></span>
            </div>
            <div class="form-group">
                <label>Email</label>
                <span><?php echo $email; ?></span>
            </div>
            <div class="form-group">
                <label>Address</label>
                <span><?php echo $address; ?></span>
            </div>
            <div class="form-group">
                <label>Phone Number</label>
                <span><?php echo $student_phone; ?></span>
            </div>
            <div class="form-group">
                <label>Mobile Number</label>
                <span><?php echo $student_mobile; ?></span>
            </div>
            <div class="form-group">
                <label>Father's Name</label>
                <span><?php echo $father; ?></span>
            </div>
            <div class="form-group">
                <label>Father's Contact</label>
                <span><?php echo $father_contact; ?></span>
            </div>
            <div class="form-group">
                <label>Mother's Name</label>
                <span><?php echo $mother; ?></span>
            </div>
            <div class="form-group">
                <label>Mother's Contact</label>
                <span><?php echo $mother_contact; ?></span>
            </div>
            <div class="form-group">
                <label>Guardian Name</label>
                <span><?php echo $guardian; ?></span>
            </div>
            <div class="form-group">
                <label>Guardian Contact</label>
                <span><?php echo "here will be date" . $guardian_contact; ?></span>
            </div>
            <div class="form-group table">              
                <label>Date of Birth <strong>*</strong></label>
                    <span><?php echo $date_of_birth;?></span>           
            </div>
            <input name="submit" id="btnValidate" type="submit" value="Submit"/>
            <input id="back" type="button" value="Back"/>
        </form>
    </body>
</html>

工作正常。