Page 1 of 1

Problem with session variable?

Posted: Wed May 07, 2003 8:32 am
by demolud2
Hi,

after selecting new item from pull down menu my script should reload page with new options depending on item selected. Unfortunately it doesn't.
Instead first position is being selected. But when I choose same item for the second time, it works perfectly.
It repeats every time I first run the script (page).
This is my script (part of it):

Code: Select all

<?php

$formVars = array();

if(!session_is_registered($_SESSION['formVars'])) session_register("formVars");

foreach($_POST as $varname => $value) $formVars[$varname] = $value;

?>

<FORM NAME="DataBase" METHOD="POST" ACTION="BazaEB.php?action=raporty&sub=platnosci">
 <SELECT NAME="TypRaportu"onChange="this.form.action='BazaEB.php?action=raporty&sub=platnosci';this.form.method='POST';this.form.submit();">

<?php
my_connect("BazaEB");

$query = "SELECT DBReports.nazwa FROM DBReports ";

$query .= "WHERE DBReports.typ='Pymt' ORDER BY DBReports.nazwa;";

$wynik = sql("BazaEB",$query);

while ($rekord = mysql_fetch_array ($wynik)) {

   if($formVars['TypRaportu']){
      if($formVars['TypRaportu'] == $rekord[0]) echo "  <OPTION SELECTED>$rekord[0]\n"
      else echo "  <OPTION>$rekord[0]\n";
    }

   else {
      if(ereg("Select",$rekord[0])) echo "<OPTION SELECTED>$rekord[0]\n"
      else echo " <OPTION>$rekord[0]\n";
   }

}

?>
</SELECT>

Rest of the script behaves correctly, it means it's being extended according to the option that should
be chosen from pull-down menu.
It is strange but I had not such problems on Windows. I have them on newly set-up Linux. Perhaps
some PHP configuration missing?
System: RedHat 9.0, PHP 4.2.2, Apache 2.0.40, mysql 3.23.54
Thanks in advance for any hint,
regards,
Maciek


Posted: Wed May 07, 2003 9:13 am
by volka
have only read until
if(!session_is_registered($_SESSION['formVars'])) session_register("formVars");
so this might not solve your problem but...
you should not mix session_register()/...is_registered/... functions with $_SESSION. If $_SESSION['formVars'] is defined it's registered

Code: Select all

if(!isset($_SESSION['formVars']))

Posted: Wed May 07, 2003 9:17 am
by twigletmac
Yeah, you may just need to change those first few lines to something like:

Code: Select all

if (!isset($_SESSION['formVars'])) {
	$_SESSION['formVars'] = array();
}

$formVars = $_SESSION['formVars'];
instead of:

Code: Select all

$formVars = array(); 

if(!session_is_registered($_SESSION['formVars'])) session_register("formVars");
Mac

Posted: Wed May 07, 2003 9:34 am
by demolud2
Thank's for your tip, didn't help :-(

Regards,

Maciek

BTW. I know that Windows is probably not a reference system but it really worked on that system...

Posted: Wed May 07, 2003 9:49 am
by volka
note that you have to change all session variables,e.g.
$formVars['TypRaportu']
becomes

Code: Select all

$_SESSION['formVars']['TypRaportu']
<FORM NAME="DataBase" METHOD="POST" ACTION="BazaEB.php?action=raporty&sub=platnosci">
although the form's method is POST you have to fetch the parameters action and sub from the superglobal $_GET.
The METHOD-property only applies to child-elements of the form that are passed during a submit.
If you want to ship all parameters via POST you might add hidden input fields to your form.

Code: Select all

&lt;input type=hidden" name="action" value="raporty" /&gt;
&lt;input type=hidden" name="sub" value="platnosci" /&gt;
your onChange-code actually does not change the form's properties (setting them to the same values). So you might reduce it to

Code: Select all

onChange="submit();"&gt;

Posted: Wed May 07, 2003 9:53 am
by demolud2
Just to add:

I have checked contents of $formVars['TypRaportu'] when first selecting an item:

(I have selected following one: By product:db-collect - by name)

By product:db-collect - by nameTypRaportu=By product:db-collect - by name

This is the reason for the strange behaviour. But why variable $formVars['TypRaportu'] has this TypRaportu=By product:db-collect - by name stuck at the end of the real value?

Regards,

Maciek