会话变量不是从HTML表单设置的

时间:2017-01-06 20:12:09

标签: php html session

我很欣赏任何有新鲜面貌的人,因为我看似简单的功能。 header.php有2个搜索字段和一个搜索按钮。 表单操作调用pagination.php来运行查询并对结果进行分页。 问题是表单字段不存储在会话变量中。只有表单字段,会话变量在登录脚本中工作,并且具有硬编码值。

这是来自header.php的表单(带有一些调试语句):

    <form action="http://my1shop.uk/phpmarketplace/customer/pagination.php" method="post" class="searchform">

                    <input name="searchstring" class="textbox" type="text" placeholder="search string"/>
                    <input name="postcode" class="textbox" type="text" placeholder="postcode"/>
                    <input name="submit" class="submit" value="Search" type="submit" />
<?php
session_start();
//unset($_SESSION['test']);
$_SESSION['test']= 'Testsearch';

if (isset($_POST['submit'])) { 
$_SESSION['search_string'] = $_POST['searchstring'];
$_SESSION['zip3'] = substr($_POST['postcode'],0,3);
$_SESSION['test2'] = 'Test2';
}

?>
</form>

这是pagination.php的第一部分(在SQL和分页之前):

<?php
//error_reporting(E_ALL); ini_set('display_errors', 'On');
include_once '../config.php'; // has session_start() in it


// find out how many rows are in the table 
$sql = "SELECT COUNT(*) FROM forsale_content";
$result = dbQuery($sql) ;
$r = dbFetchRow($result);
$numrows = $r[0];

// number of rows to show per page
$rowsperpage = 5;
// find out total pages
$totalpages = ceil($numrows / $rowsperpage);

// get the current page or set a default
if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {
   // cast var as int
   $currentpage = (int) $_GET['currentpage'];
} else {
   // default page num
   $currentpage = 1;
} // end if

// if current page is greater than total pages...
if ($currentpage > $totalpages) {
   // set current page to last page
   $currentpage = $totalpages;
} // end if
// if current page is less than first page...
if ($currentpage < 1) {
   // set current page to first page
   $currentpage = 1;
} // end if

// the offset of the list, based on current page 
$offset = ($currentpage - 1) * $rowsperpage;

$keywords = $_SESSION['search_string'];
$zip = $_SESSION['zip3'];
$search = $_POST['searchstring'];
//$keywords = $_POST['searchstring'];
echo "pagination:" . $_SESSION['test'];
echo "test2:" . $_SESSION['test2'];
echo "zip:" . $_SESSION['zip3'];
echo strlen($keywords) ;
echo "search:" . $search;

if( $keywords == "")
{
echo "<div align=\"center\"><b>You did not enter a searchstring.</b></div>";
}

结果是&#34;你没有输入搜索字符串&#34;并且$ keywords和$ zip没有值(错误ON表示索引&#39; search_string&#39;&#39; zip3&#39; undefined)。

然而,&#39;测试&#39;具有正确的价值,如果&#34; if(isset($ _ POST [&#39; submit&#39;]))&#34;在header.php中被注释掉了 - &#39; test2&#39;也获得了价值。但不是其他2个变量,其中值应从提交表单中分配。 当我将form方法更改为GET时,我可以看到拾取的字段值。

已阅读所有相关文章,尝试过不同的浏览器,检查引号,方括号等,但用完了想法......

0 个答案:

没有答案