单击链接时将会话变量传递到新页面 [英] Pass session variable to new page when link is clicked

查看:82
本文介绍了单击链接时将会话变量传递到新页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对此我有点麻烦.我有一个包含各种链接的书籍的搜索结果页面,所有链接都链接到同一页面(book.php),并且我想设置一个会话变量以传递到新页面,该页面应显示有关特定结果链接的更多信息,即已选择.当在while循环中选择链接时,我试图将变量$ _SESSION ['booktitle']传递给book.php.搜索结果页面的代码为:

I'm having a bit of trouble with this. I have a search results page of books with various links, all linking to the same page (book.php) and I want to set a session variable to pass through to the new page which should display more info about the specific result link that is selected. I am trying to pass the variable $_SESSION['booktitle'] through to book.php when the link in the while loop is selected. The code of the search results page is:

<form id="rform" name="formsort" action=""  method="post">
<select name="sort" id="sort" onChange="document.forms['formsort'].submit()">
<option value="default">Sort Results</option>
<option value="lowhigh">Price: Low to High</option>
<option value="highlow">Price: High to Low</option>
<option value="az">Alphabetical Order: A-Z</option>
<option value="za">Alphabetical Order: Z-A</option>
</select>
<?php
if(isset($_POST['title']))
    {
        echo '<input type="hidden" name="title" value="'.$_POST['title'].'" />';
    }
if(isset($_POST['author']))
    {
        echo '<input type="hidden" name="author" value="'.$_POST['author'].'" />';
    }
if(isset($_POST['isbn']))
    {
        echo '<input type="hidden" name="isbn" value="'.$_POST['isbn'].'" />';
    }
if(isset($_POST['keyword']))
    {
        echo '<input type="hidden" name="keyword" value="'.$_POST['keyword'].'" />';
    }
?>
</form>
<?php
include "include.php";
session_start();
// Defining variables from home.php if set
$title = mysql_real_escape_string($_POST['title']);
$author = mysql_real_escape_string($_POST['author']);
$isbn = mysql_real_escape_string($_POST['isbn']);
$keyword = mysql_real_escape_string($_POST['keyword']);

if(isset($_POST['sort'])){
$sort=@$_POST['sort'];}
// Determine the WHEREs to use
$where = array();
if ( ! empty($title))
$where[] = "booktitle LIKE '%" . $title . "%'";
if ( ! empty($author))
$where[] = "author LIKE '%" . $author . "%'";
if ( ! empty($isbn))
$where[] = "isbn LIKE '%" . $isbn . "%'";
if ( ! empty($keyword))
$where[] = "description LIKE '%" . $keyword . "%'";

//Determine the ORDERs to use
$order = array();
if ((isset($sort)) && ($sort=='lowhigh'))
$order[] = "price ASC";
if ((isset($sort)) && ($sort=='highlow'))
$order[] = "price DESC";
if ((isset($sort)) && ($sort=='az'))
$order[] = "booktitle ASC";
if ((isset($sort)) && ($sort=='za'))
$order[] = "booktitle DESC";
// Build the query
$query = 'SELECT * FROM book';
if ( ! empty($where))
$query .= ' WHERE ' . implode(' AND ', $where);
if(! empty($sort))
$query .= ' ORDER BY ' . implode($order);
//Display results
$result = mysqli_query($con, $query) or die("Error in query $query: " . mysqli_error());
while ($row = mysqli_fetch_array($result)) {
  ?><a id="1" href="book.php" border="0"><img src="<?php echo $row[12]; ?>" width="112px" height="150px" /></a>
<a href="book.php?booktitle='<?php echo $_SESSION['booktitle']=$row[1];?>'"><b><?php echo  $row[1] . ", (" . $row[5] . ")";?></b></a><br><?php
echo $row[2];
echo "<br><div style='text-align:right'> <b>&pound;" . $row[9] . "</b></div>";
echo "<b>Book description:</b> " . substr($row[3],0,300) . "...<br /><hr>";
}
mysqli_close($con);             //closes the connection
?>

我使用以下方法在book.php中称呼它:

and I call it in book.php using:

<?php
include "include.php";
session_start();
$book=$_SESSION['booktitle'];

$query = "SELECT * FROM book WHERE booktitle='$book'";
$result = mysqli_query($con, $query) or die("Error in query $query: " . mysqli_error());
while ($row = mysqli_fetch_array($result)) {
echo $row[1];
} ?>

但是,无论我选择什么书链接,都将回显while循环中第一个搜索结果的书名.知道为什么吗?

But no matter what book link I select, the booktitle of the 1st search result in the while loop is being echoed out. Any idea why?

任何帮助将不胜感激.

推荐答案

我希望我能为您提供最大的帮助.

I hope that i can offer you my maximum help .

首先,我想了解如果第二个块是book.php,则第一个块所在的文件名是什么?

First of all i want to understand what is the file name where the first block exists if the second block is book.php ?

然后我建议您使用代码顶部的session_start()消除任何标题警告".

Then i want to suggest that you use the session_start() in the top of your code to eliminate any "header warnings" .

然后,我想请您向我解释为什么您将此指令放入代码中

Then i want from you to explain to me why you put in your code this instruction

<?php echo $_SESSION['booktitle']=$row[1]?>

为什么不这样写:

<?php
$_SESSION['booktitle'] = $row[1];
echo $_SESSION['booktitle'];
?>

然后总是回显一次只需要一次的$_SESSION['variable'] 之后,在直接回显它的情况下,对它进行unset()看起来就像您的情况unset($_SESSION['booktitle']);

Then always after echoing a $_SESSION['variable'] that is needed just for one time do an unset() for it after echoing it directly look like this in your case unset($_SESSION['booktitle']);

在上述代码中,我认为您的新书名搜索没有存储在新的会话中,而旧的搜索结果只是存储在其中,请尝试应用这些修改并告诉我是否解决了这个问题:)

What i think that your new booktitle search is not stored in a new session during the above code and the old search result is just stored try to apply these modifications and tell me if it is solved :)

这篇关于单击链接时将会话变量传递到新页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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