2013-01-24 75 views
2

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

我的表单使用$ _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?页工作= n)。我错过了什么?

Regards,

Bob

回答

2

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

+0

大家好,我用过session_start();没有任何结果。对不起,我错过了我的代码,但在发布后发现了错误。 – bobfelstead

2

试试这个,我希望它会工作: -

session_start(); 
$_SESSION['county']=$_POST['county']; 
$_SESSION['language']=$_POST['language']; 
+0

嗨,我试过了,没有快乐。 – bobfelstead

0

使用$ _SESSION变量不开始会话? 在脚本中使用session_start();

0

感谢您的输入。我已经设定了一个cookie,调用页面上解决这一问题,在结果页面上,然后我用饼干:

<?php 
    $area = $_COOKIE['Carea']; 
    $speak = $_COOKIE['Cspeak']; 
?> 

我,VE然后传递这些变量的函数调用的参数。

function sql_select($area,$speak) 

它的工作原理。

Bob