douyao1856 2016-01-09 03:52
浏览 77

在PHP MYSQL中将表单数据从1页传输到许多其他页面

I have a issue where I want to transfer form data from "home.php" to "dashboard.php", but here i have a check of session on dashboard.php for login session as below:-

    <?php  
session_start();
?>
<?php
if(!$_SESSION["UserName"])
{
    //Do not show protected data, redirect to login...
    header('Location: login.php');  
}
?>

here when a user submits a form data from "home.php" he gets redirected to login page for login if session of login is not generated. where as on homepage i have use POST method to form as below:-

 <form class="form ajax-contact-form" method="POST" action="dashboard.php">
                            <div class="alert alert-success hidden" id="contact-success">
                                <span class="glyphicon glyphicon-ok "></span> &nbsp;
                                <strong>Success!</strong> Thank you for your message.
                            </div>
                            <div class="alert alert-danger hidden" id="contact-error">
                                <span class="glyphicon glyphicon-remove "></span> &nbsp;
                                <strong>Error!</strong> Oops, something went wrong.
                            </div>
                            <div class="row col-p10">
                                <div class="col-sm-6">
                                    <label class="mb10">
                                        <input type="text" name="name_" id="name_" required class="form-control" placeholder=" Full Name * ">


                                    </label>
                                </div>
                                <div class="col-sm-6">
                                    <label class="mb10">
                                        <input type="text" name="subject_" id="subject_" required class="form-control" placeholder=" Subject *">
                                    </label>
                                </div>
                            </div>
                            <div class="row col-p10">
                                <div class="col-sm-6">
                                    <label class="mb10">
                                        <input type="text" name="phone_" id="phone_" class="form-control" placeholder=" Phone">
                                    </label>
                                </div>
                                <div class="col-sm-6">
                                    <label class="mb10">
                                        <input type="email" name="email_" id="email_" required class="form-control" placeholder=" Email Address *">
                                    </label>
                                </div>
                            </div>
                            <label>
                                <textarea name="message_" id="message_" cols="30" rows="10" required class="form-control" placeholder=" Message *"></textarea>
                            </label>
                            <div class="mb40"></div>
                            <div class="clearfix">
                            <!-- Enter your google site key here for captcha -->
                               <div class="pull-right xs-pull-left xs-box">



                                </div>  
                                <div class="pull-left">
                                    <button type="submit" class="btn btn-icon btn-e" value="submit" name="submit"><i class="icon icon_mail_alt"></i> Sumbit</button>

                                    <?php 
                                             if (isset($_POST['submit'])) { 
                                             $_SESSION['name_'] = $_POST['name_'];
                                             $_SESSION['subject_'] = $_POST['subject_'];
                                             } 
                                    ?> 

                                </div>
                            </div>
                        </form>

Code on Login page :-

  <?php  
include("connection.php");
session_start();//session starts here  
?>  

<?php
if(isset($_SESSION["UserName"]))
{
    //Do not show protected data, redirect to login...

    header('Location: dashboard.php');
}
?>

<?php   

                                    if(isset($_POST['login']))  
                                    {  
                                        $username=$_POST['Username'];  
                                        $user_pass=$_POST['password'];  
                                        $encrypt_pass = md5($user_pass);


                                        $check_user="select * from tbl_logindetails WHERE UserName='".$username."' AND Password='".$encrypt_pass."'";


                                        $run=mysqli_query($connection,$check_user); 

                                        if(mysqli_num_rows($run))  
                                        {  
                                            //echo "<script>window.open('www.google.com','_self')</script>"; 
                                            header("Location: dashboard.php");                                  

                                            $_SESSION['UserName']= $username; //here session is used and value of $user_email store in $_SESSION.  


                                        }  
                                        else  
                                        {  

                                          echo "<script>alert( 'Error in Registering Useer, Please try again later' )</script>";  
                                        }  
                                    }  
                                    ?> 

Code to Display data of "home.php" to "dashboard.php"

                   <?php echo $_POST["name_"]; ?>
                    </br> </br>
                    <?php echo $_POST["subject_"]; ?> 
                    </br> </br>
                    <?php echo $_POST["message_"]; ?> 
                    </br> </br>

After Login the user gets access to dashboard.php, but my issue is when the user comes after doing the login the input data that i took from "home.php" shows an error as below attached image link:-

[1]: http://i.stack.imgur.com/G5Udj.png

Error i am getting after doing the above process of form data --> login if no session --> Dashboard

Notice: Undefined index: name_ in C:\xampp\htdocs\youngants\dashboard.php on line 401

Notice: Undefined index: subject_ in C:\xampp\htdocs\youngants\dashboard.php on line 403

Notice: Undefined index: message_ in C:\xampp\htdocs\youngants\dashboard.php on line 405

Notice: Undefined index: name in C:\xampp\htdocs\youngants\dashboard.php on line 408

Guys I need your help where how can i carry the data from "home.php" to "dashboard.php" by sessions variable which can even stay filled after the user is redirected to login after form submit and then returns to dashboard.php after login.

Any more information needed please comment i will provide it.

  • 写回答

1条回答 默认 最新

  • dongyu2300 2016-01-09 04:06
    关注

    First of all; in the first code you use $_SESSION but you don't call session_start(); you have to call it at the beginning of the each file you use $_SESSION.

    The things you are getting are not errors but warnings. It's because POST variables are not like session variables. They will stay there for only one page after they are POSTed (with an html form). Put them inside of the sessions or as cookies if you want to use them for a long period of time.

    On your login code, you use header() function to redirect user, and then you assign $_SESSION['Username'] to something, but this will may not get executed. (Because you redirect it.) I suggest you to put an exit; call right after the redirection to make use the code below will not be executed.

    评论

报告相同问题?