我有一个问题,我想将表单数据从"home.php"传输到"dashboard.php",但这里我在dashboardphp上检查了登录会话的会话,如下所示:-
<?php
session_start();
?>
<?php
if(!$_SESSION["UserName"])
{
//Do not show protected data, redirect to login...
header('Location: login.php');
}
?>
在这里,当用户从"home.php"提交表单数据时,如果没有生成登录会话,他会被重定向到登录页面进行登录。在主页上,我使用POST方法形成如下:-
<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>
<strong>Success!</strong> Thank you for your message.
</div>
<div class="alert alert-danger hidden" id="contact-error">
<span class="glyphicon glyphicon-remove "></span>
<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>
登录页面上的代码:-
<?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>";
}
}
?>
显示"home.php"到"dashboard.php"数据的代码
<?php echo $_POST["name_"]; ?>
</br> </br>
<?php echo $_POST["subject_"]; ?>
</br> </br>
<?php echo $_POST["message_"]; ?>
</br> </br>
登录后,用户可以访问dashboard.php,但我的问题是,当用户登录后,我从"home.php"中获取的输入数据显示了一个错误,如下所示:附上图像链接:-
[1]: https://i.stack.imgur.com/G5Udj.png
我在完成以上表单数据处理后收到的错误-->如果没有会话则登录-->Dashboard
注意:第401行C:''examplep''htdocs''youngants''dashboard.php中的未定义索引:name_
注意:未定义的索引:subject_ in C:''examplep''htdocs''youngants''dashboard.php,位于第403行
注意:未定义的索引:message_在C:''examplep''htdocs''youngants''dashboard.php中的第405行
注意:第408行C:''examplep''htdocs''youngants''dashboard.php中的未定义索引:name
伙计们,我需要你们的帮助,我如何通过会话变量将数据从"home.php"携带到"dashboard.php",甚至可以在用户提交表单后重定向到登录,然后在登录后返回dashboard.php后保持填充状态。
任何需要的更多信息,请评论,我会提供它。
首先;在第一段代码中,您使用$_SESSION,但不调用SESSION_start();您必须在使用$_SESSION的每个文件的开头调用它。
你得到的不是错误,而是警告。这是因为POST变量与会话变量不同。在张贴(使用html表单)后,它们只会在那里停留一页。如果你想长时间使用它们,可以把它们放在会话中或作为cookie。
在登录代码中,您使用header()函数重定向用户,然后将$_SESSION[用户名]分配给某个对象,但这可能不会执行。(因为你重定向了它。)我建议你设置一个出口;重定向后立即调用以使用下面的代码将不会执行。