Ray44
Ray44

Reputation: 23

Losing first 2 session variables after calling a 3rd session variable (php 5)

Here's my case (relatively new on php): I got a page "zoek_form.php" where you can enter 2 search values in a form (naam and categorie). When submitted, page "zoek.php" is loaded and a search will be performed (mysql 5.6). To perform the search the 2 values are obtained from the 2 session variables. So far so good, the search works and the rows are retrieved. But now I want the user to be able to make a sequence (via ORDER BY) in zoek.php, based on a dropdown list. The selected value will be stored in a 3rd session variable. But now the problem: when selecting a sequence and submit the form, the first 2 session values are lost. I'm puzzled why. The essence of session variables is just storing the values and to be able to use them over and over again? (till they are overwritten or killed). Of course I use session_start(); at the beginning of the php-script (otherwise it would not have worked at all ;-). Any ideas?

Here's zoek_form.php:

<html>
<head>
<title>Zoeken</title>
</head>
<body>

<?php session_start(); ?>
<form name="form1" method="POST" action="zoek.php">
<table border="0">
<tr><td>Naam product:</td>
<td><input type="text" size="50" name="form_naam"></td></tr>
<tr><td>Categorie:</td>
<td><input type="text" size="50" name="form_cat"></td></tr>
<tr><td></td>
<td align = "right"><input type="submit" name="B1" value="Zoeken">
</td></tr>
</table>
</form>

</body>
</html>

Here's zoek.php:

<html>
<head>
<title>Zoeken</title>
</head>
<body>

<form name="form1" method="POST" action="">
<table border="0">
<tr><td>Sorteer op:</td>
<td><select name="form_sort">
  <option value="Naam">Naam</option>
  <option value="Categorie">Categorie</option>  
</select></td>
<td><input type="submit" name="B1" value="Sorteer"></td></tr>
</table>
</form>

<?php

session_start();
require_once 'test_connect.php';
$_SESSION['form_naam'] = $_POST['form_naam'];
$_SESSION['form_cat'] = $_POST['form_cat'];
$_SESSION['form_sort'] = $_POST['form_sort'];
// The 3 lines below were used to check whether session vars were set
// echo  $_SESSION['form_naam'];
// echo  $_SESSION['form_cat'];
// echo  $_SESSION['form_sort'];

function sorteren() {
global $sorteer;
$sorteer = $_SESSION['form_sort'];
  if ($sorteer == "Naam") {
  $sorteer = "ORDER BY naam";
  }
  else {
  $sorteer = "ORDER BY categorie";
  }
 }

// Put values from zoek_form.php in vars.
$naam = $_SESSION['form_naam'];
$cat = $_SESSION['form_cat'];

// Check if user has set a sequence. If yes: call function sorteren(),
// if no: leave var $sorteer empty.
if (isset($_SESSION['form_sort'])) {
sorteren();
}
else {
$sorteer = "";
}

// Get rows from table product
$sql = "SELECT * FROM product WHERE naam LIKE '$naam%' OR categorie
LIKE '$cat%' $sorteer";
$result = $conn -> query($sql);

if ($result->num_rows > 0) {
// here code to retrieve rows etc.    
}

// Give result free
$result -> free();

// Close connection
$conn -> close();

?>

</body>
</html>

Upvotes: 2

Views: 73

Answers (1)

FuzzyTree
FuzzyTree

Reputation: 32402

Your form in zoek.php doesn't contain form_naam and form_cat so when you run

$_SESSION['form_naam'] = $_POST['form_naam'];
$_SESSION['form_cat'] = $_POST['form_cat'];

It sets those values to null. If you want to retain those values you could try passing them back again in the form with hidden input fields

<input type="hidden" name="form_naam" value="<?= $_SESSION['form_naam'] ?>">
<input type="hidden" name="form_cat" value="<?= $_SESSION['form_cat'] ?>">

Another way to prevent overwriting the session values is to only change them if the $_POST values are set

if(isset($_POST['form_naam']) && isset($_POST['form_cat'])) {
  $_SESSION['form_naam'] = $_POST['form_naam'];
  $_SESSION['form_cat'] = $_POST['form_cat'];
}

Upvotes: 2

Related Questions