将SESSION数据存储在一个表单中

时间:2017-01-12 21:56:27

标签: php session

我正在尝试将数据存储在$_SESSION变量中,以便我可以在表单的其他部分使用它。

我有一个团队名称输入,我需要在表格下拉几步后使用,团队名称仅来自用户输入。

我努力在整个表单中传递这些数据,然后在最后发布。

会话正在header.php

中设置
<?php
if(!empty($_POST)) {
    $_SESSION['dashboardName'] = $_POST['dashboardName'];
    $_SESSION['teamName'] = $_POST['teamName'];
    $_SESSION['firstName'] = $_POST['firstName']; 
    $_SESSION['lastName'] = $_POST['lastName'];      
    $_SESSION['memberTeam'] = $_POST['memberTeam']; 
    $_SESSION['stenData'] = $_POST['stenData'];
    $_SESSION['score_1'] = $_POST['score_1'];
    $_SESSION['score_2'] = $_POST['score_2']; 
    $_SESSION['score_3'] = $_POST['score_3']; 
    $_SESSION['score_4'] = $_POST['score_4'];  
    $_SESSION['score_5'] = $_POST['score_5']; 
    $_SESSION['score_6'] = $_POST['score_6']; 
    $_SESSION['score_7'] = $_POST['score_7']; 
    $_SESSION['score_8'] = $_POST['score_8'];                                           
}
?>

<form action="" method="POST">

<!-- STEP :: 1 -->
    <div class="step-one">
    <h1>STEP 1</h1>
        <input type="text" name="dashboardName" required placeholder="Dashboard Name" value="<?php echo $_SESSION['dashboardName']; ?>">
        <button class="step-one-next">Next</button>
    </div>

<!-- STEP :: 2 -->
    <div class="step-two">
    <h1>STEP 2</h1>
        <input type="text" name="teamName" placeholder="Team name" required value="<?php echo $_SESSION['teamName']; ?>">
        <button class="step-two-previous">Previous</button>   
        <button class="step-two-next">Next</button>        
    </div>

<!-- STEP :: 3 -->
    <div class="step-three">
    <h1>STEP 3</h1>
        <input type="text" name="firstName" placeholder="First Name" required value="<?php echo $_SESSION['firstName']; ?>">
        <input type="text" name="lastName" placeholder="Last Name" required value="<?php echo $_SESSION['lastName']; ?>">
        <button class="step-three-previous">Previous</button>
        <button class="step-three-next">Next</button>
    </div>

<!-- STEP :: 4 -->    
    <div class="step-four">
    <h1>STEP 4</h1>
        <select name="memberTeam">
            <option><?php echo $_SESSION['teamName']; ?></option>
        </select>
        <button class="step-four-previous">Previous</button>
        <button class="step-four-next">Next</button>        
    </div>

<!-- STEP :: 5 -->     
    <div class="step-five">
    <h1>STEP 5</h1>
        <textarea name="stenData" placeholder="Paste Sten Data..." required><?php echo $_SESSION['stenData']; ?></textarea>
        <button class="step-five-previous">Previous</button>
        <button class="step-five-next">Next</button>        
    </div>

<!-- STEP :: 6 -->   
    <div class="step-six">  
    <h1>STEP 6</h1>
        <select name="score_1">
            <option selected="selected"><?php echo $_SESSION['score_1']; ?></option>
            <option value="1">1</option>
            <option value="2">2</option>
            <option value="3">3</option>
            <option value="4">4</option>
            <option value="5">5</option>
        </select>  

        <select name="score_2">
            <option selected="selected"><?php echo $_SESSION['score_2']; ?></option>
            <option value="1">1</option>
            <option value="2">2</option>
            <option value="3">3</option>
            <option value="4">4</option>
            <option value="5">5</option>
        </select>  

        <select name="score_3">
            <option selected="selected"><?php echo $_SESSION['score_3']; ?></option>
            <option value="1">1</option>
            <option value="2">2</option>
            <option value="3">3</option>
            <option value="4">4</option>
            <option value="5">5</option>
        </select> 

        <select name="score_4">
            <option selected="selected"><?php echo $_SESSION['score_4']; ?></option>
            <option value="1">1</option>
            <option value="2">2</option>
            <option value="3">3</option>
            <option value="4">4</option>
            <option value="5">5</option>
        </select> 

         <select name="score_5">
            <option selected="selected"><?php echo $_SESSION['score_5']; ?></option>
            <option value="1">1</option>
            <option value="2">2</option>
            <option value="3">3</option>
            <option value="4">4</option>
            <option value="5">5</option>
        </select> 

        <select name="score_6">
            <option selected="selected"><?php echo $_SESSION['score_6']; ?></option>
            <option value="1">1</option>
            <option value="2">2</option>
            <option value="3">3</option>
            <option value="4">4</option>
            <option value="5">5</option>
        </select> 

         <select name="score_7">
            <option selected="selected"><?php echo $_SESSION['score_7']; ?></option>
            <option value="1">1</option>
            <option value="2">2</option>
            <option value="3">3</option>
            <option value="4">4</option>
            <option value="5">5</option>
        </select> 

        <select name="score_8">
            <option selected="selected"><?php echo $_SESSION['score_8']; ?></option>
            <option value="1">1</option>
            <option value="2">2</option>
            <option value="3">3</option>
            <option value="4">4</option>
            <option value="5">5</option>
        </select> 
        <button class="step-six-previous">Previous</button>
        <button class="step-six-next">Next</button>                           
    </div>




<!-- STEP :: 7 -->
    <div class="step-seven">   
    <h1>STEP 7</h1> 
    <p><?php echo $_SESSION['dashboardName']; ?></p>
        <button class="step-seven-previous">Previous</button>
        <input type="submit" name="test" value="Submit">
    </div>

</form>

<script>
$(".step-one-next").click(function(){
    $(".step-one").hide();
    $(".step-two").show();
});
$(".step-two-previous").click(function(){
    $(".step-two").hide();
    $(".step-one").show();
});
$(".step-two-next").click(function(){
    $(".step-two").hide();
    $(".step-three").show();
});
$(".step-three-previous").click(function(){
    $(".step-three").hide();
    $(".step-two").show();
});
$(".step-three-next").click(function(){
    $(".step-three").hide();
    $(".step-four").show();
});
$(".step-four-previous").click(function(){
    $(".step-four").hide();
    $(".step-three").show();
});
$(".step-four-next").click(function(){
    $(".step-four").hide();
    $(".step-five").show();
});
$(".step-five-previous").click(function(){
    $(".step-five").hide();
    $(".step-four").show();
});
$(".step-five-next").click(function(){
    $(".step-five").hide();
    $(".step-six").show();
});
$(".step-six-previous").click(function(){
    $(".step-six").hide();
    $(".step-five").show();
});
$(".step-six-next").click(function(){
    $(".step-six").hide();
    $(".step-seven").show();
});
$(".step-seven-previous").click(function(){
    $(".step-seven").hide();
    $(".step-six").show();
});
</script>

因此,最终目标是在每个阶段输入输入,为团队命名,然后在memberTeam <select>下拉列表中提供团队名称

1 个答案:

答案 0 :(得分:1)

每次点击“下一步”按钮,表单数据都不会发布到服务器。您可以更改为<button type="submit" class="step-one-next">Next</button>(将类型添加为提交),但是您需要处理您所在的步骤以及点击下一步时的下一步。否则,使用JavaScript会更容易。

使用jQuery更新: https://jsfiddle.net/79nj8e7p/1/

抱歉,我没有足够的声誉作为评论。