从表单传递的php SESSION变量 - php

尝试将表单变量传递给生成多个页面的脚本时遇到问题。

我的表格使用$ _POST在以下脚本中提供了一个县名和一个语言作为2个单独的字段。当通过$ _POST传递变量时,脚本对查询进行了硬编码,并且对page = 1 results.php页面有效。但是,后续页面松散了$ _POST变量,并且我知道此问题的解决方案涉及$ _SESSION命令,但我无法使其正常工作:(。事实上,尽管参数已传递给这些功能。

这是我的代码

<?php
$_SESSION['county']=$_POST['county'];
$_SESSION['language']=$_POST['language'];

$speak=$_POST['language'];
$area=$_POST['county']
?>
<table class="bda" width="100%" align = "center"><tr><td class="hrak">Open test db file on lcn</td></tr></table>

<?php
$conn = connect();
/*******************************************************************************************
The variable '$showrecs' refers to the number of rows on each page - this can be changed.
'$pagerange refers to blocks of records, again can be changed.
*******************************************************************************************/
 $area=$_POST['county'];
 $speak=$_POST['language'];
 $showrecs = 20;
 $pagerange = 10;
 $page = @$_GET["page"];
 if (!isset($page)) $page = 1;
 select();
 mysql_close($conn);
 ?>
 <table class="bda" width="100%"><tr><td class="hrak">Powered by MySQL</td></tr></table>
 </body>
 </html>
 <?php
 /******************************************** - START OF FUNCTIONS  *************************************/
 ?>
 <?php function select()
  {
  global $a;
  global $showrecs;
  global $page;

  $res = sql_select();
$count = sql_getrecordcount();
  if ($count % $showrecs != 0) {
      $pagecount = intval($count / $showrecs) + 1;
  }
  else {
      $pagecount = intval($count / $showrecs);
  }
      $startrec = $showrecs * ($page - 1);
  if ($startrec < $count) {mysql_data_seek($res, $startrec);}
  $reccount = min($showrecs * $page, $count);
  ?>
     <table class="bd" border="0" cellspacing="1" cellpadding="4" width="100%" align = "center">

     <tr><td class="hrak" align="center" width="100%">Records shown <?php echo $startrec + 1 ?> -        <?php echo $reccount ?> of <?php echo $count ?></td></tr>
    </table>

 <?php showpagenav($page, $pagecount); ?>
 <br>
 <table class="tbl" border="0" cellspacing="1" cellpadding="5"width="100%">
 <tr>

 <!- ***************************************
    HEADER row of the table - HTML
  **************************************** ->

  <td>
  </td>

  </tr>
<?php
   for ($i = $startrec; $i < $reccount; $i++)
   {
     $row = mysql_fetch_assoc($res);
     $mem13 = ($row["title"]);
     if ($mem13 == 'Mr')
         {
         $style = "dr";
         $styler = "drr";

      if ($i % 2 != 0) 
         {
         $style = "sr";
         $styler = "srr";
         }
    }
   else
    {
         $style = "ndr";
         $styler = "ndrr";

      if ($i % 2 != 0) 
    {
        $style = "nsr";
        $styler = "nsrr";
    }

   }
?>

<tr>

 <!-- *********************************************
This is where I populate the DATA rows of the table  - HTML
 ********************************************** -->


 <td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["id"]) ?></td>
 </tr>

<?php
    }
   /*** mysql_free_result($res); ***/
 ?>
</table>
<br>
<?php showpagenav($page, $pagecount); ?>
<?php } ?>

<?php function showpagenav($page, $pagecount)
{
?>
  <table class="bd" border="0" cellspacing="1" cellpadding="4" >
  <tr>
<?php if ($page > 1) { ?>
 <!- /*************************************************************************
 This generates 'results.php' and the sub pages results results.php?page=2...
 *****************************************************************************/
 -->

 <td class="hrak1" align="center"><a href="results.php?page=<?php echo $page - 1 ? >">&lt;&lt;&nbsp;Prev</a>&nbsp;</td>
<?php } ?>
<?php
   global $pagerange;

   if ($pagecount > 1) {

   if ($pagecount % $pagerange != 0) {
     $rangecount = intval($pagecount / $pagerange) + 1;
   }
   else {
      $rangecount = intval($pagecount / $pagerange);
        }
  for ($i = 1; $i < $rangecount + 1; $i++) {
     $startpage = (($i - 1) * $pagerange) + 1;
     $count = min($i * $pagerange, $pagecount);

     if ((($page >= $startpage) && ($page <= ($i * $pagerange)))) {
       for ($j = $startpage; $j < $count + 1; $j++) {
        if ($j == $page) {
 ?>
  <td><b><?php echo $j ?></b></td>
 <?php } else { ?>
 <!--
 /*****************************************************************************************************
 results.php?pagen  so that the required page number is generated.
 *****************************************************************************************************/
-->
<td class="hrak1" align="center"><a href="results.php?page=<?php echo $j ?>"><?php echo $j ?></a></td>
<?php } } } else { ?>
<td class="hrak1" align="center"><a href="results.php?page=<?php echo $startpage ?>"><?php echo  $startpage ."..." .$count ?></a></td>
<?php } } } ?>
<?php if ($page < $pagecount) { ?>
<td class="hrak1" align="center">&nbsp;<a href="results.php?page=<?php echo $page + 1 ?>">Next&nbsp;&gt;&gt;</a>&nbsp;</td>
<?php } ?>
</tr>
</table>
<?php } ?>

<?php 

/******************************************** - FUNCTION - SQL_SELECT *************************************/
function sql_select()
{
/******************************************************************************
SELECT statement for results to be displayed
******************************************************************************/
   $area=$_POST['county'];
   $speak=$_POST['language'];
   global $conn;
   $sql = "SELECT * FROM test2 WHERE language='$speak' AND county='$area' ORDER BY id";
   $res = mysql_query($sql, $conn) or die(mysql_error());
   return $res;
}
/******************************************** - FUNCTION - SQL_GETRECORDCOUNT *************************************/
function sql_getrecordcount()
{
  $area=$_POST['county'];
  $speak=$_POST['language'];
  $sql = "SELECT COUNT(*) FROM test2 WHERE language='$speak' AND county='$area'";
    $res = mysql_query($sql, $conn) or die(mysql_error());
    $row = mysql_fetch_assoc($res);
    reset($row);
    return current($row);
}
/******************************************** - FUNCTION - CONNECT *************************************/

我知道代码可以在不使用$ _POST传递参数的情况下工作,但是我无法使其与多个结果页一起使用(results.php?page = n)。我想念什么?

问候,

鲍勃

参考方案

必须将session_start()放在每个使用$_SESSION变量的脚本的开头。

jQuery不起作用 - php

我正在使用带有ajax的jquery。有时,给出错误$未定义。这是我的代码:<script language="javascript" type="text/javascript"> var base_path="<? echo $this->baseUrl().'/…

Div单击与单选按钮相同吗? - php

有没有一种方法可以使div上的click事件与表单环境中的单选按钮相同?我只希望下面的div提交值,单选按钮很丑代码输出如下:<input id="radio-2011-06-08" value="2011-06-08" type="radio" name="radio_date&#…

PHP-复选框组 - php

我有一个需要发布的表单复选框组。<input type="checkbox" value="true" checked name="chk0[]"> <input type="checkbox" value="false" name=…

PHP:正则表达式匹配短语不包含特定单词 - php

我已经厌倦了寻找足够接近的例子,花时间寻求一些快速帮助!这是我的代码:preg_match_all( '#<li.*?>.*?</li>#s', $card_html, $activity ); 我想对其进行修改,以使<li.*?>排除单词Unplayed。 (单词出现在.*?之后和>之前。编辑:…

侧面导航菜单问题 - php

单击时如何使所有嵌套列表折叠并展开。这是我的xHTML。<ul> <li><a href="some-link.php">First Nested List</a></li> <li><a href="some-link.php">Fi…