Page 1 of 1

Problem with Session variables in form

Posted: Sun Apr 17, 2005 12:39 am
by mor_111
Hi
I m new to php ,building a form to enter user information .this is how it is suposed to work.there are 2 forms form.php and form1.php user is told to enter his input in form.php which is then posted to form1.php for validation
then form1.php is repeated through self posting until all the information is correct. The problem is i m using session variables in second form if the variable is filled on the first form then the value is retained and can be printed anywhere,but if the variable is entered in second form then it is lost when the page post the information to itself (if the variable is valid then it is printed if it is invalid user is asked to enter it again).i hope i make my problem clear .any help will be deeply appreciated ......thanx

Code: Select all

//form.php

<!doctype html public "-//W3C//DTD HTML 4.0 //EN"> 
<html>
<head>
       <title>MY first fu%^#$* page</title>
       <!-- jobapp.html -->
<BODY>
<H1>Voter Registration Page</H1>
<P>Are you dying to cast your vote
</P>
<?php
error_reporting(1);
for($i=1;$i<32;$i++)
{$day[$i]=$i;}
$month=array("jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec");
for($i=1900;$i<2005;$i++)
{$year[$i-1900]=$i;
}
?>
<form method="post" name="frm_reg" action="form1.php">
 First Name :
<INPUT NAME="app_first" TYPE="text"><BR>
 Last Name :
<INPUT NAME="app_last" TYPE="text"><BR>
 
 Enter valid E-mail address *:
 <INPUT NAME="email" TYPE="text"><BR>
 Select your SEX :
  <SELECT NAME="sex">
<OPTION VALUE="m">Definitely Male</OPTION>
<OPTION VALUE="f">Definitely Female</OPTION>

 </SELECT><BR>
<?
echo'Date of Birth';

echo'<SELECT NAME = "frm_day">';
  for($i=1;$i<32;$i++)
{echo'<OPTION VALUE='.$i.'>'.$i;}
echo'</SELECT>';
 echo'<SELECT NAME = "frm_month">';
  for($i=0;$i<12;$i++)
{echo'<OPTION VALUE='.$i.'>'.$month[$i];}
echo'</SELECT>';
 echo'<SELECT NAME = "frm_year">';
  for($i=0;$i<105;$i++)
{echo'<OPTION VALUE='.$year[$i].'>'.$year[$i];}
echo'</SELECT><BR>';
//$frm_month=$frm_month + 1;
?>
  Please enter your telephone number:
<INPUT NAME="phone" TYPE="text"><BR>

<? $db=mysql_connect(localhost,"user here","password here");
     mysql_select_db("user",$db);
     $result=mysql_query("SELECT DISTINCT stname from testconstate");
     
?>

Enter your State;
 <?
 echo'<SELECT NAME = "frm_state">';
  while($result1=mysql_fetch_array($result))
{echo'<OPTION VALUE='.$result1["stname"].'>'.$result1["stname"];}
echo'</SELECT><BR>';


?>

<INPUT NAME="enter" TYPE="submit" VALUE="Enter">
</FORM>

</BODY>
</HTML>

       
</head>
<body>

</body>

Code: Select all

//form1.php
session_start(); // This connects to the existing session

session_register ("app_first");
session_register ("app_last");
session_register ("email");
session_register ("sex");
session_register ("phone");
session_register ("frm_day");
session_register ("frm_month");
session_register ("frm_year");
session_register ("frm_state");
session_register ("frm_const");

$HTTP_SESSION_VARS['app_first'] = $_POST['app_first'];
$HTTP_SESSION_VARS ["app_last"] = $_POST['app_last'];
$HTTP_SESSION_VARS ["email"] = $_POST['email'];
$HTTP_SESSION_VARS ["sex"] = $_POST['sex'];
$HTTP_SESSION_VARS ["phone"] = $_POST['phone'];

$HTTP_SESSION_VARS ["frm_day"] = $_POST['frm_day'];
$HTTP_SESSION_VARS ["frm_month"] = $_POST['frm_month'];
$HTTP_SESSION_VARS ["frm_year"] = $_POST['frm_year'];
$HTTP_SESSION_VARS ["frm_state"] = $_POST['frm_state'];
$HTTP_SESSION_VARS["frm_const"] = $_POST['frm_const'];

function check_email( $email )
{
  $email = trim( $email ); # removes whitespace
  if( !empty($email) ):
    //  validate email address syntax
    if( preg_match('/^[a-z0-9\\_\\.]+@[a-z0-9\\-]+\\.[a-z]+\\.?[a-z]{2,4}$/i', $email, $match) ):
      return strtolower($match[0]); # valid!
    endif;
  endif;
  return 0; # NOT valid!
}

  function age_cur($iMonth, $iDay, $iYear) {
    $iTimeStamp = (mktime() - 86400) - mktime(0, 0, 0, $iMonth, $iDay, $iYear);
    $iDays = $iTimeStamp / 86400;
    $iYears = floor($iDays / 365 );
    return $iYears;

}
$valid_date=checkdate($frm_month+1,$frm_day,$frm_year);

 $age=age_cur($frm_month+1,$frm_day,$frm_year);

 if($submit)
 {if($app_first==0||$app_last==0||$app_father==0||$email==0||$valid_date==0||$age<18||$address==0)
 {$submit=0;}
 else
 {$submit=1;
 echo'Success';
exit(1);
  if(!$submit)
 {
echo'Please enter the following information again<BR>';?>
<form method="post" name="frm_reg1" action="form1.php">
<?
if($app_first)
{
echo"First Name:; $app_first";
}
else
if(!$app_first)
   {echo'First Name : ';
  // $submit=0;
echo'<INPUT NAME="app_first" TYPE="text">';}
echo'<BR>';
 if($app_last)
{echo"Last Name : $app_last<BR>";}
else
if(!$app_last)
   {echo'Last Name : ';
  // $submit=0;
echo'<INPUT NAME="app_last" TYPE="text"><BR>';}
  
 if (!check_email ($email)) {
//$submit = 0;
echo'<B>Invalid E-mail Address</B>';

echo'<INPUT NAME="email" TYPE="text"><BR>';}
else
echo"Email Address : $email<BR>";
if($sex=="m")
echo'Sex : male<BR>';
else
if($sex=="f")
echo'Sex : female';

if($phone)
echo"Phone number : $phone<BR>";
else
echo'Phone number : None Entered<BR>';


$valid_date=checkdate($frm_month+1,$frm_day,$frm_year);

 $age=age_cur($frm_month+1,$frm_day,$frm_year);

 if($valid_date)
 echo"Date of Birth = $frm_day  $month[$frm_month] $frm_year <BR>";
 else
 {echo'Date of Birth';
  // $submit=0;
echo'<SELECT NAME = "frm_day">';
  for($i=1;$i<32;$i++)
{echo'<OPTION VALUE='.$i.'>'.$i;}
echo'</SELECT>';
 echo'<SELECT NAME = "frm_month">';
  for($i=0;$i<12;$i++)
{echo'<OPTION VALUE='.$i.'>'.$month[$i];}
echo'</SELECT>';
 echo'<SELECT NAME = "frm_year">';
  for($i=0;$i<105;$i++)
{echo'<OPTION VALUE='.$year[$i].'>'.$year[$i];}
echo'</SELECT><BR>';
}

if($age>=18)
echo"AGE =$age<BR>";
else
echo'Input error :Age below 18<BR>';
    if($address)
echo"Address : $address<BR>";
else
echo'<INPUT NAME="address" TYPE="text"><BR>';
if($frm_const)
{echo"Constituency:$frm_const";}
else
if(!$frm_const)
{echo'Enter constituency';
   $db=mysql_connect(localhost,"user","password");
     mysql_select_db("user",$db);
  $result2=mysql_query("SELECT * from testconstate where stname ='$frm_state'");
 echo'<SELECT NAME = "frm_const">';
  while($result3=mysql_fetch_array($result2))
{echo'<OPTION VALUE='.$result3["constname"].'>'.$result3["constname"];}
echo'</SELECT><BR>';}



echo'<INPUT NAME="Enter" TYPE="submit" VALUE="Enter">';


echo'</FORM>';

 }
?>

Posted: Sun Apr 17, 2005 1:54 am
by infolock
Do you have global variables on or off? also, you should check out Sessions on php's online manual...

if you have globals off (which is the default since php4.1), then this :

Code: Select all

<?
session_start(); // This connects to the existing session

session_register ("app_first");
session_register ("app_last");
session_register ("email");
session_register ("sex");
session_register ("phone");
session_register ("frm_day");
session_register ("frm_month");
session_register ("frm_year");
session_register ("frm_state");
session_register ("frm_const");

$HTTP_SESSION_VARS['app_first'] = $_POST['app_first'];
$HTTP_SESSION_VARS ["app_last"] = $_POST['app_last'];
$HTTP_SESSION_VARS ["email"] = $_POST['email'];
$HTTP_SESSION_VARS ["sex"] = $_POST['sex'];
$HTTP_SESSION_VARS ["phone"] = $_POST['phone'];

$HTTP_SESSION_VARS ["frm_day"] = $_POST['frm_day'];
$HTTP_SESSION_VARS ["frm_month"] = $_POST['frm_month'];
$HTTP_SESSION_VARS ["frm_year"] = $_POST['frm_year'];
$HTTP_SESSION_VARS ["frm_state"] = $_POST['frm_state'];
$HTTP_SESSION_VARS["frm_const"] = $_POST['frm_const'];
?>
should actually be :

Code: Select all

session_start(); // This connects to the existing session

$_SESSION['app_first'] = $_POST['app_first'];
$_SESSION['app_last'] = $_POST['app_last'];
$_SESSION ['email'] = $_POST['email'];
$_SESSION ['sex'] = $_POST['sex'];
$_SESSION ['phone'] = $_POST['phone'];

$_SESSION ['frm_day'] = $_POST['frm_day'];
$_SESSION ['frm_month'] = $_POST['frm_month'];
$_SESSION ['frm_year'] = $_POST['frm_year'];
$_SESSION ['frm_state'] = $_POST['frm_state'];
$_SESSION['frm_const'] = $_POST['frm_const'];


also, you are peforming an echo of passed variables like :

Code: Select all

if($app_first)
{
   echo"First Name:; $app_first";
}
if you have globals turned off, then this should be $_POST['app_first'] instead of just $app_first.

lastly, your if statements are kinda off

look into ISSET.

hope this helps.


feyd | fixed a parse error.

Posted: Sun Apr 17, 2005 9:29 am
by mor_111
hi again
as it turns out global variables on my server are turned off and i cant enable them as i dont have the priviledge so i did as u said but the problem seems to remain .The variables which were filled in form.php are posted as expected to form1.php,but once form1.php is self posted all these variables are lost whereas the variables which were filled in form1.php retain their value once on self posting but lose it on the second posting i m displaying the vital changes for one variable

Code: Select all

<?php
session_start();
$_SESSION['app_first'] = $_POST['app_first'];
$_SESSION["app_last"] = $_POST['app_last'];

<form method="post" name="frm_reg1" action="<?php echo$_SERVER['PHP_SELF']>">
<?
if($_POST['app_first'])
{
echo"First Name:";echo $_POST['app_first'];
}
else
if(!$_POST['app_first'])
   {echo'First Name : ';
  // $submit=0;
echo'<INPUT NAME="app_first" TYPE="text">';}
echo'<BR>';


echo'<INPUT NAME="Enter" TYPE="submit" VALUE="Enter">';
anyway thanx for ur reply

Posted: Sun Apr 17, 2005 9:32 am
by feyd
you're always setting the session variables. If $_POST['app_first'] is set, the next submission will write a blank string (null actually) to the session variable.