0

I appreciate anyone taking a fresh look as I am stuck with a seemingly simple functionality. header.php has 2 search fields and a search button. The form action invokes pagination.php that runs the query and paginates the results. The problem is that the form fields are not stored in session variables. Only the form fields, session variables work in login script and with hard-coded values.

Here is the form from header.php (with some debugging statements):

    <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>

Here is the first part of pagination.php (before SQL and pagination):

<?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>";
}

The result is "You did not enter a searchstring" and $keywords and $zip have no value (with error ON it says indexes 'search_string' and 'zip3' undefined).

However 'test' has correct value and if "if (isset($_POST['submit']))" is commented out in header.php - 'test2' also gets a value. But not the other 2 variables where values should be assigned from the form on submit. When I change form method to GET, I can see the field value picked up.

Have read all relevant articles, tried different browsers, checked for quotes, brackets etc. but ran out of ideas...

user2097141
  • 307
  • 2
  • 4
  • 15
  • `error_reporting(E_ALL); ini_set('display_errors', '1');` then the dupe makes sense. – AbraCadaver Jan 06 '17 at 20:15
  • Make sure that you have started session using `session_start();` – Rajdeep Paul Jan 06 '17 at 20:16
  • I can't see how the "Headers already sent" posting is relevant to my question about session variables. And yes, session_start() is there as shown. – user2097141 Jan 06 '17 at 21:05
  • And here is the output to confirm that session variables work, except for those coming from the form fields: pagination:Testsearchtest2:Test2zip:0search:test You did not enter a searchstring. No more data [1] – user2097141 Jan 06 '17 at 21:18
  • `$_SESSION['search_string'] = $_POST['searchstring'];` should be in pagination.php – Rohit Jan 06 '17 at 22:40
  • @Rohit Thanks, Rohit. This is how it works now. However, as POST variable is only available on the first call, there is additional code. Also I have to unset the SESSION variable in header.php, so that it does not offer itself for the different search. $search = $_POST['searchstring']; if (isset($_POST['searchstring']) && $search != "") { $keywords = $_POST['searchstring']; $_SESSION['keywords'] =$keywords ; unset($_POST['searchstring']); } $keywords = $_SESSION['keywords']; – user2097141 Jan 07 '17 at 16:04

0 Answers0