PHP从会话数组显示单个值 [英] PHP Display single value from session array

查看:44
本文介绍了PHP从会话数组显示单个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有一个PHP表格供合作伙伴注册报价.用户登录后,他们被带到报价单的开头.我希望用户的名字自动显示在第一个表单字段中.

I currently have a PHP form for our partners to register quotes. After the user log in they are brought to the beginning of the quote form. I would like the user's first name to display in the first form field automatically.

用户的会话信息包括一个值数组;其中一个是firstName值.这是我要在用户登录时在第一个字段中显示的值.

The user's session information includes an array of values; one of which is the firstName value. This is the value that I would like to display in the first field when the user logs in.

当我尝试加载页面时,出现错误,而不是用户的名字.

When I try to load the page I am getting an error instead of the user's first name.

这是我得到的错误:
通知:未定义的索引: 77
C:\ wamp64 \ www \ quote_generator \ quote_tool.php 中的用户>

This is the error I am getting:
Notice: Undefined index: user in C:\wamp64\www\quote_generator\quote_tool.php on line 77

这是更新的页面代码:

<?php
    require("config.php");
    if(empty($_SESSION['user'])) 
    {
        header("Location: index.php");
        die("Redirecting to index.php"); 
    }
?>

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Quoting Tool </title>
    <meta name="description" content="Teo partner quote generator tool">
    <meta name="Kenneth Carskadon" content="www.kencarskadon.com">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- CSS -->
    <link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Roboto:400,100,300,500">
    <link rel="stylesheet" href="assets/css/bootstrap.min.css" media="screen">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/3.1.3/css/bootstrap-datetimepicker.min.css">
    <link rel="stylesheet" href="assets/css/font-awesome/css/font-awesome.css">
    <link rel="stylesheet" href="assets/css/form-elements.css">
    <link rel="stylesheet" href="assets/css/datepicker.css">
    <link rel="stylesheet" href="assets/css/style.css">
    <style>
        body {
            background: url("assets/images/backgrounds/background.jpg") no-repeat fixed center;
            background-size: cover;
        }
    </style>
</head>
<body>

<!-- Top menu -->
    <nav class="navbar navbar-inverse navbar-no-bg" role="navigation">
        <div class="container">
            <div>
                <a class="navbar-brand" href="#">
                    <img src="assets/images/logo/Teo%20Logo%20White.png" class="img-responsive" id="teo-logo">
                </a>
            </div>
        </div>
    </nav>

    <!-- Top content -->
    <div class="top-content">
        <div class="inner-bg">
            <div class="container form-container">
                <div class="row">
                    <div class="col-sm-8 col-sm-offset-2 text">
                        <h1><strong>Teo</strong> Partner Quote Generation Form</h1>
                    </div>
                </div>
                <div class="row">
                    <div class="col-sm-8 col-sm-offset-2 form-box">
                        <form role="form" action="" method="post" class="registration-form">
                            <!-- Fieldset 1: Partner Information -->
                            <fieldset>
                                <div class="form-top">
                                    <div class="form-top-left">
                                        <h2>Partner Information</h2>
                                        <h3>Tell us who you are</h3>
                                    </div>
                                    <div class="form-top-right">
                                        <img src="assets/images/icons/partner_info.png" class="form-icon">
                                    </div>
                                </div>
                                <div class="form-bottom">
                                    <div class="form-group">
                                        <input type="text" name="form-partner-name" placeholder="Partner name" class="form-partner-name form-control" id="form-partner-name" 
                                               value="<?php echo $_POST['user']['firstName']?>">
                                    </div>
                                    <div class="form-group">
                                        <select name="partner-level" type="text" title="Select partner level" class="form-control" id="form-partner-level" >
                                          <option>Select partner level</option>  
                                          <option data-price="34">Platinum</option>
                                          <option data-price="32">Gold</option>
                                          <option data-price="29">Silver</option>
                                        </select>
                                    </div>
                                    <div class="form-group">
                                            <input type="text" readonly="readonly" name="discount-perc" placeholder="Discount(%)" class="form-control" id="form-discount-perc">
                                        </div><br />
                                        <div class="form-group">
                                            <input type="text" name="form-margin-perc" placeholder="Margin(%)" class="form-control" id="form-margin-perc">
                                        </div>
                                    <br /><br />
                                    <button type="button" class="btn btn-next">Next</button>
                                </div>
                            </fieldset>

                            <!-- Fieldset 2: Quote Information -->
                            <fieldset>
                                <div class="form-top">
                                    <div class="form-top-left">
                                        <h2>Quote Information</h2>
                                        <h3>Tell us about your customer</h3>
                                    </div>
                                    <div class="form-top-right">
                                        <img src="assets/images/icons/partner_info.png" class="form-icon">
                                    </div>
                                </div>
                                <div class="form-bottom">
                                    <div class="form-group">
                                        <input type="text" name="form-customer-name" placeholder="Customer name" class="form-customer-name form-control" id="form-customer-name">
                                    </div>
                                    <div class="form-group">
                                        <input type="text" name="form-quote-number" placeholder="Quote Number" class="form-quote-number form-control" id="form-quote-number">
                                    </div>
                                    <div class="form-group">
                                        <input  type="text" name="form-incentive-exp" placeholder="Incentive Expiration Date"  class="teo-datepicker">
                                    </div>
                                    <div class="form-group">
                                        <input  type="text" name="form-proposal-exp" placeholder="Proposal Expiration Date"  class="teo-datepicker">
                                    </div>


                                    <br /><br />
                                    <button type="button" class="btn btn-previous">Previous</button>
                                    <button type="button" class="btn btn-next">Next</button>
                                </div>
                            </fieldset>

                            <!-- Fieldset 3: Select Servers -->
                            <fieldset>
                                <div class="form-top">
                                    <div class="form-top-left">
                                        <h2>Select Servers</h2>
                                        <h3>Tell us about the servers you need</h3>
                                    </div>
                                    <div class="form-top-right">
                                        <img src="assets/images/icons/select_servers.png" class="form-icon">
                                    </div>
                                </div>
                                <div class="form-bottom">
                                    <div class="form-group">
                                        <label for="server-select">What kind of server do you need?</label>
                                        <select class="form-control" id="server-type-select">
                                            <option>Please select and option below</option>
                                            <option>Pro Server</option>
                                            <option>Mini Server</option>
                                        </select>
                                    </div>

                                    <div class="form-group">
                                        <label for="server-select">How many servers do you need?</label>
                                        <input type="text" name="form-server-quantity" placeholder="Number of servers" class="form-control" id="form-server-quantity">
                                    </div>
                                    <br /><br />
                                    <button type="button" class="btn btn-previous">Previous</button>
                                    <button type="button" class="btn btn-next">Next</button>
                                    <button type="button" class="btn btn-skip">skip</button>
                                </div>
                            </fieldset>

                            <!-- Fieldset 4: Configure Servers -->
                            <fieldset>
                                <div class="form-top">
                                    <div class="form-top-left">
                                        <h2>Select Servers</h2>
                                        <h3>Tell us about the servers you need</h3>
                                    </div>
                                    <div class="form-top-right">
                                        <img src="assets/images/icons/select_servers.png" class="form-icon">
                                    </div>
                                </div>
                                <div class="form-bottom">
                                    <div class="form-group">
                                        <label for="server-select">What kind of server do you need?</label>
                                        <select class="form-control" id="server-type-select">
                                            <option>Please select and option below</option>
                                            <option>Pro Server</option>
                                            <option>Mini Server</option>
                                        </select>
                                    </div>

                                    <div class="form-group">
                                        <label for="server-select">How many servers do you need?</label>
                                        <input type="text" name="form-server-quantity" placeholder="Number of servers" class="form-control" id="form-server-quantity">
                                    </div>
                                    <br /><br />
                                    <button type="button" class="btn btn-previous">Previous</button>
                                    <button type="button" class="btn btn-next">Next</button>
                                    <button type="button" class="btn btn-skip">Skip</button>
                                </div>
                            </fieldset>
                            <div class="btn-logout">
                                <a href="logout.php">Logout</a>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>


    <!-- Javascript -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script src="assets/js/bootstrap.min.js"></script>
    <script src="assets/js/jquery.backstretch.min.js"></script>
    <script type='text/javascript' src="https://rawgit.com/RobinHerbots/jquery.inputmask/3.x/dist/jquery.inputmask.bundle.js"></script>
    <script src="assets/js/bootstrap-datepicker.js"></script>
    <script src="assets/js/scripts.js"></script>

    <!--[if lt IE 10]>
            <script src="assets/js/placeholder.js"></script>
    <![endif]-->
</body>
</html>

更新:我有一个会话页面,向我显示当前会话的信息,这些是我正在讨论的表单的结果:

Update: I have a session page that shows me the information on the current session, and these are the results for the form I am talking about:

查看图像

推荐答案

屏幕截图已对其进行了澄清.

The screenshot clarified it.

您应该可以使用

$ session ['user'] ['firstName']

但是,如上面的评论所述,您将希望摆脱使用 eval 的习惯,这会带来安全隐患.

As noted in the comments above though, you will want to migrate away from using eval, which has security implications.

这篇关于PHP从会话数组显示单个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆