Jump to content


Photo

Ghost $_POST in Firefox


  • Please log in to reply
3 replies to this topic

#1 heller

heller
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 15 September 2006 - 07:08 AM

Hi there! I've got a weird problem with a trivial piece of script... Googled and faqqed my brains off, but still nothing...

There's a table displaying some data from DB. I'm using a drop-down list to set filters for certain columns, e.g.:
<form name="frmDate" method="post" action="/index.php?section=rep&subsection=ind&dbtFilter=date">
<select name="ddlDate" onchange="document.frmDate.submit()">
<option value="choice=all">all</option>
<option value="min=2006-9-8">previous seven days</option>
<option value="min=2006-9-01">this month</option>
<option value="min=2006-8-01&max=2006-8-31">last month</option>
</select>
</form>
As you can see, the name of the field to be filtered is sent by GET (dbtFilter) and the options (choice, min and/or max) are sent by POST.

On receiving the page, I retrieve and save the whole lot in $_SESSION, so that multiple filters can be applied to the table, one by one. Here's the code chunk:
if (isset($_GET['dbtFilter'])) {

	// compose drop-down list name from field name
	// and extract filter option pairs (e.g. 'choice=123' or 'max=2006-09-10')
	$pairs = explode("&", $_POST['ddl'.ucfirst($_GET['dbtFilter'])]);
	
	// separate option keys and values
	foreach ($pairs as $pair) {
		$pair = explode("=", $pair);
		$options[$pair[0]] = $pair[1];
	}

	// reset filter for this particular column - new data is here
	unset($_SESSION['filters'][$_GET['dbtFilter']]);

	// leave the filter empty if 'all' (no filtering) ist set, otherwise save new values
	if ($options['choice'] != "all") {
		$_SESSION['filters'][$_GET['dbtFilter']] = array(
			"choice" => $options['choice'],
			"min" => $options['min'],
			"max" => $options['max']
		);
	}
}
Thus, if I choose e.g. 'previous seven days' option for the 'Date' column, $_SESSION['filters']['date'] gets filled with data. Next, I choose e.g. 'John Smith' in 'Name', $_SESSION['filters']['name'] gets filled, too. I should have two active filters by now, right?

Now, the problem: in the example above, after I choose 'Smith' (or whatever), previously set $_SESSION['filters']['date']'s three values ('choice', 'min', 'max') get NULLed, all three of them! Moreover, if I click any link on the page (e.g. sort by some column), any filter's options set with a previous click get NULLed, too. After clicking around I end up with a $_SESSION['filters'][ALL_FILTERABLE_COLUMNS] each filled with three NULL's.

I debugged the whole thing: it works big time until the very end of the script! The $pairs array is initialized, $options too, session dump shows that the data has been saved to a proper place and remained there until the end of the script. It just wouldn't persist until the page has been updated!

After several experiments I discovered there was something wrong with the POST data. If I hard-code any string instead of $_POST['ddl'.ucfirst($_GET['dbtFilter'])], it works, the data doesn't get lost! But I can actually see the value of $_POST['ddl'.ucfirst($_GET['dbtFilter'])]! It does go to $_SESSION and activate its very filter on the page.

I tried it on three machines with three different configurations - same result, but what a waste of time: today I found out that in IE everything works perfectly, the multiple filters are there!

Anyone seen this before? Any ideas where I am being had by my favorite FF are very much appreciated.

#2 markbett

markbett
  • Members
  • PipPipPip
  • Advanced Member
  • 133 posts

Posted 15 September 2006 - 07:28 AM

read the values from the array and set them to a variable THEN write them back into the array , otherwise ensure you are writing to the correct key value in the array or else you will overwrite the information.

#3 btherl

btherl
  • Staff Alumni
  • Advanced Member
  • 3,893 posts
  • LocationAustralia

Posted 15 September 2006 - 07:30 AM

I'm interested to see some var_dump()s of your $_GET, $_POST and $_SESSION .. surely there must be something different?

When you say "filled with three NULLs", do you mean that the array $_SESSION['filters'][filter] exists, and each element has value null?  Or does has the array disappeared completely, resulting in any access to its elements returning null?

#4 heller

heller
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 15 September 2006 - 10:17 AM

read the values from the array and set them to a variable THEN write them back into the array

I tried everything: assigning and reassigning to variables, playing with different arrays extracting values with ereg_replace() instead of explode(), splitting strings in different ways etc. While hard-coded strings in any part of the script (e.g. 'blahblah' instead of $pair[1]) remain in $_SESSION forever unless unset or overwritten, but as soon as there anything derived from $_POST, it vanishes in the thin air.

otherwise ensure you are writing to the correct key value in the array or else you will overwrite the information

Even if I messed up with the keys (which I didn't), the information should be overwritten with another information, not just NULL's.

When you say "filled with three NULLs", do you mean that the array $_SESSION['filters'][filter] exists, and each element has value null?

Exactly.

I'm interested to see some var_dump()s of your $_GET, $_POST and $_SESSION .. surely there must be something different?

There we go. I'll show only the ['filters'] part of the $_SESSION, and I'll show it twice: before filter adjustment (the script in my original message) and after it. So I load the page with the table:

$_SESSION['filters']: NULL
$_GET: array(2) { ["section"]=> string(3) "rep" ["subsection"]=> string(3) "ind" }
$_POST: array(0) { }
$_SESSION['filters'] after adjusting: NULL


Then I choose 'last month' for the 'Date' column:

$_SESSION['filters']: NULL
$_GET: array(3) { ["section"]=> string(3) "rep" ["subsection"]=> string(3) "ind" ["dbtFilter"]=> string(4) "date" }
$_POST: array(1) { ["ddlDate"]=> string(27) "min=2006-8-01&max=2006-8-31" }
$_GET['dbtFilter']: string(4) "date"
$_POST['ddl'.ucfirst($_GET['dbtFilter'])]: string(27) "min=2006-8-01&max=2006-8-31"
$pairs: Array ( [0] => min=2006-8-01 [1] => max=2006-8-31 )
$options: Array ( [min] => 2006-8-01 [max] => 2006-8-31 )
$_SESSION['filters'] after adjusting: array(1) { ["date"]=> array(3) { ["choice"]=> NULL ["min"]=> string(9) "2006-8-01" ["max"]=> string(9) "2006-8-31" } }


Perfect, isn't it? Everything is seen, found, assigned etc. Okay. Now I go for 'Category' with the ID '1':

$_SESSION['filters']: array(1) { ["date"]=>  array(3) { ["choice"]=>  NULL ["min"]=>  NULL ["max"]=>  NULL } }
$_GET: array(3) { ["section"]=> string(3) "rep" ["subsection"]=> string(3) "ind" ["dbtFilter"]=> string(8) "category" }
$_POST: array(1) { ["ddlCategory"]=> string(8) "choice=1" }
$_GET['dbtFilter']: string(8) "category"
$_POST['ddl'.ucfirst($_GET['dbtFilter'])]: string(8) "choice=1"
$pairs: array(1) { [0]=> string(8) "choice=1" }
$options: array(1) { ["choice"]=> string(1) "1" }
$_SESSION['filters'] after adjusting: array(2) { ["date"]=> array(3) { ["choice"]=> NULL ["min"]=> NULL ["max"]=> NULL } ["category"]=> array(3) { ["choice"]=> string(1) "1" ["min"]=> NULL ["max"]=> NULL } }


Bang! Again, everything is seen and assigned, but the $_SESSION['filters']['date']'s three values (well, two)... Where the shining star are they?

And, again: why are they in place in IE?




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users