Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

on submit form - Session is undefined

Status
Not open for further replies.

yenping

MIS
Aug 16, 2005
17
MY
I have made a session variable from page1.php, when it goes to page2.php (for example), it shows the value.

in page2.php, there is a form, submit back to page2.php to display information from tables but it shows error that the session variable is undefined.

how can this happen? In case 1, it works, and it doesn't work in the case 2.

any answer will be appreciated.
 
Can you post your code, or enough to show your problem? With out it, we will be just guessing what your problem is.

Ken
 
Code:
<?php
session_start();
header("Cache-control:private");
?>

<?php include("conn.php"); ?>
<html>
<head>
<title>PRINSIPTEK</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="css/style1.css" rel="stylesheet" type="text/css">
<style type="text/css">
<!--
.style1 {color: #FF0000}
.style3 {color: #000000}
body {
	background-color: #f3f3f3;
}
.text1 {
	font-family: Arial, Helvetica, sans-serif;
	font-size: 12px;
}
.style4 {color: #000099}
-->
</style>
<script language="javascript1.2">
<!--
function generate_ref(){
	frm_ref=document.form_ref;
	if(frm_ref.desc.value==''||frm_ref.letter_to.value==''||frm_ref.taken_by.value==''){
		alert("Please fill up all required fields.");
		frm_ref.ref_no.value='';
	}
	else{
		var remark='';
		if(frm_ref.remark.value!=''){
			remark=frm_ref.remark.value.toUpperCase();
		}
		else{ remark=frm_ref.letter_to.value.toUpperCase(); }
	
		frm_ref.ref_no.value=frm_ref.select_company.value + '/' + frm_ref.select_type.value;
		frm_ref.ref_no.value=frm_ref.ref_no.value + '/' + remark + '/' + frm_ref.select_month.value;
		frm_ref.ref_no.value=frm_ref.ref_no.value + '/' + frm_ref.yr.value + '/' + frm_ref.num_rows.value;
		frm_ref.submit_add.disabled=false;
	}
}
function show_format(){
	frm_ref=document.form_ref;
	if(frm_ref.select_tp.value=='the_date'){
		frm_ref.search.value='yyyy-mm-dd';
	}
	else{
		frm_ref.search.value='';
	}
	frm_ref.search.focus();
}
function copy_ref(){
	if(document.form_ref.ref_no.value==''){
		alert("Cannot copy empty field!");
	}
	else{
		val=document.form_ref.ref_no.createTextRange();
//		alert(document.form_ref.ref_no.value);
		val.select();
		val.execCommand('Copy');
	}
}
-->
</script>
</head>

<body onLoad="javascript:document.forms[0].select_company.focus();">
<?php
if(isset($_POST["submit_go"])){
	$select_tp=$_POST["select_tp"];
	$search=$_POST["search"];
	$sql="SELECT * FROM ref_no WHERE ".$select_tp." LIKE '%".$search."%'";
	$result=mysql_query($sql,$db);
	display_form_2($result);
	if(mysql_num_rows($result)==0){
		echo "<center><font color=red>There is no match to your search</font></center><br><br>";
	}
	echo "<center><a href=index.php>Go back to entry &gt;&gt;</a></center>";
	mysql_close($db);
}
elseif(isset($_POST["submit_add"])){
	$company=$_POST["select_company"];
	$type=$_POST["select_type"];
	$the_date=$_POST["yr"]."/".$_POST["select_month"]."/".$_POST["select_day"];
	$desc=$_POST["desc"];
	if($_POST["remark"]!=""){ $letter_to=$_POST["remark"]; }
	else{ $letter_to=$_POST["letter_to"]; }
	$taken_by=$_POST["taken_by"];
	$ref_no=$_POST["ref_no"];
	$result=mysql_query("SELECT * FROM ref_no",$db);
	$num=$_POST["num"];
	
	$sql="INSERT INTO ref_no(ref_no,the_date,description,letter_to,taken_by,id) VALUES('".$ref_no."','".$the_date;
	$sql=$sql."','".$desc."','".$letter_to;
	$sql=$sql."','".$taken_by."',".$num.")";
	mysql_query($sql,$db) or die(mysql_error());
	
	mysql_close($db);
	display_form();
}
elseif(isset($_POST["submit_save"])){
	$result=$_POST["result"];
	$total_fld=$_POST["total_fld"];
	for($i=1;$i<$total_fld;$i=$i+6){
		$txt=array(1=>"txt".$i,2=>"txt".($i+1),3=>"txt".($i+2),4=>"txt".($i+3),5=>"txt".($i+4),6=>"txt".($i+5));
		$sql="UPDATE ref_no SET ref_no='".$_POST[$txt[2]]."',the_date='".$_POST[$txt[3]]."',description='";
		$sql=$sql.$_POST[$txt[4]]."',letter_to='".$_POST[$txt[5]]."',taken_by='";
		$sql=$sql.$_POST[$txt[6]]."' WHERE id=".$_POST[$txt[1]];
		mysql_query($sql,$db) or die(mysql_error());
	}
	display_form_2($result);
	echo "<center><font color=blue>The record(s) has been saved.</font></center><br><br>";
	echo "<center><a href=index.php>Go back to entry &gt;&gt;</a></center>";
	mysql_close($db);
}
else{ display_form(); }
#mysql_close($db);

function display_form(){
$db=mysql_connect("localhost","root","654321");
mysql_select_db("reference",$db);
$result=mysql_query("SELECT * FROM ref_no",$db);
$num=mysql_num_rows($result);
mysql_close($db);

$num=$num+1;
if($num<10){	$num_rows="00".$num; }
elseif($num<100){	$num_rows="0".$num;	}
else{	$num_rows="0".$num;	}
$the_m=array(1=>"January",2=>"February",3=>"March",4=>"April",5=>"May",6=>"June",7=>"July",8=>"August",9=>"September",10=>"October",11=>"November",12=>"December");
?>
<form name="form_ref" method="post" action="index.php">
	<input name="num" type="hidden" id="num" value="<?php echo $num; ?>">
  <input name="num_rows" type="hidden" id="num_rows" value="<?php echo $num_rows; ?>">
  <table width="500" border="1" align="center" cellpadding="0" cellspacing="0" bordercolor="#CCCCCC">
    <tr>
      <td><table width="500" border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF">
        <tr>
          <td colspan="7"><div align="center" class="style1">
              <h2 class="style1">PRINSIPTEK</h2>
          </div></td>
        </tr>
        <tr>
          <td colspan="7"><div align="center">
              <h4>Reference No. </h4>
          </div></td>
        </tr>
        <tr>
          <td colspan="6"><span class="style4">Welcome, 
            <?=$_SESSION["username"]?>
            <input name="username" type="hidden" id="username" value="<?=$_SESSION["username"]?>">
</span></td>
          <td><a href="../index.php">Log out</a> </td>
        </tr>
        <tr>
          <td colspan="7">&nbsp;</td>
        </tr>
        <tr>
          <td colspan="7"><table width="450" border="1" align="center" cellpadding="0" cellspacing="0">
            <tr>
              <td><table width="450" border="0" align="center" cellpadding="5" cellspacing="0" bordercolor="#CCCCCC">
                <tr bgcolor="#F3F3F3">
                  <td>Company</td>
                  <td><strong>:</strong></td>
                  <td><select name="select_company" class="text1" id="select_company">
                      <option value="KAB1" selected>A.BESIK (P1)</option>
                      <option value="KAB2">A.BESIK (P2)</option>
                      <option value="KAB3">A.BESIK (P3)</option>
                      <option value="ERA">ERA WANGSA</option>
                      <option value="GCR">GREEN CRESCENT RESOURCE</option>
                      <option value="HD">HAPPLY DOMAIN</option>
                      <option value="PST">PRINSIPTEK</option>
                                                      </select></td>
                </tr>
                <tr bgcolor="#CCCCCC">
                  <td>Refence type </td>
                  <td><strong>:</strong></td>
                  <td><select name="select_type" class="text1" id="select_type">
                    <option value="GEN" selected>GEN</option>
                    <option value="PC">PC</option>
                    <option value="BANK">BANK</option>
                  </select></td>
                </tr>
                <tr bgcolor="#f3f3f3">
                  <td width="96"><span class="style3">Date</span></td>
                  <td width="4"><span class="style3"><strong>:</strong></span></td>
                  <td width="271"><select name="select_day" id="select_day"><?php
				  for($i=1;$i<=31;$i++){
				  	if($i<10){
						$temp_i="0".$i;
						if($temp_i==date("d")){
				  ?>
				  <option value="<?php echo "0".$i; ?>" selected><?php echo "0".$i; ?></option>
				  <?php
				  		}
						else{
				  ?>
                    <option value="<?php echo "0".$i; ?>"><?php echo "0".$i; ?></option><?php
						}
					}
					else{
						if($i==date("d")){
					?>
					<option value="<?php echo $i; ?>" selected><?php echo $i; ?></option>
					<?php 
						}
						else{
					?>
					<option value="<?php echo $i; ?>"><?php echo $i; ?></option><?php
						}
					}
				}
					?>
                  </select>
                  <select name="select_month" class="text1" id="select5"><?php
				  for($i=1;$i<=12;$i++){
				  	if($i<10){
						$temp_i="0".$i;
						if($temp_i==date("m")){
				  ?>
				  	<option value="<?php echo "0".$i; ?>" selected><?php echo $the_m[$i]; ?></option><?php
						}
						else{
					?>
                    <option value="<?php echo "0".$i; ?>"><?php echo $the_m[$i]; ?></option><?php
						}
					}
					else{
						if($i==date("m")){
					?>
					<option value="<?php echo $i; ?>" selected><?php echo $the_m[$i]; ?></option><?php
						}
						else{
					?>
					<option value="<?php echo $i; ?>"><?php echo $the_m[$i]; ?></option><?php
						}
					}
				  }
					?>
	                    </select>
                      <input name="yr" type="text" class="text1" id="yr3" value="<?php echo date("y");?>" size="5"></td>
                </tr>
                <tr bgcolor="#CCCCCC">
                  <td>Description of Letter </td>
                  <td><strong>:</strong></td>
                  <td><input name="desc" type="text" class="text1" id="desc3" size="40"></td>
                </tr>
                <tr bgcolor="#f3f3f3">
                  <td>Letter to </td>
                  <td><strong>:</strong></td>
                  <td><input name="letter_to" type="text" class="text1" id="letter_to3" size="20">
      Remarks
        <input name="remark" type="text" class="text1" id="remark" size="15"></td>
                </tr>
                <tr bgcolor="#CCCCCC">
                  <td>Taken by </td>
                  <td><strong>:</strong></td>
                  <td><input name="taken_by" type="text" class="text1" id="taken_by3"></td>
                </tr>
                <tr bgcolor="#F3F3F3">
                  <td>Reference No. </td>
                  <td><strong>:</strong></td>
                  <td><input name="ref_no" type="text" class="text1" id="ref_no3" size="40"></td>
                </tr>
              </table></td>
            </tr>
          </table>            </td>
        </tr>
        <tr>
          <td colspan="7">&nbsp;</td>
        </tr>
        <tr>
          <td>&nbsp;</td>
          <td width="55"><input name="submit_add" type="Submit" id="submit_add" value="Add" alt="Add a reference" disabled></td>
          <td width="58"><input name="submit_save2" type="submit" disabled id="submit_save" value="Save" alt="Save reference"></td>
          <td width="101"><input name="but_copy" type="button" id="but_copy" value="Copy Ref." onClick="javascript:copy_ref();"></td>
          <td width="120"><input name="but_gen" type="button" id="but_gen" value="Generate Ref. No." alt="Generate reference no." onClick="javascript:generate_ref();"></td>
          <td width="3">&nbsp;</td>
        </tr>
        <tr>
          <td>&nbsp;</td>
          <td colspan="5">&nbsp;</td>
          <td>&nbsp;</td>
        </tr>
        <tr>
          <td>&nbsp;</td>
          <td colspan="5">Search:</td>
          <td>&nbsp;</td>
        </tr>
        <tr>
          <td width="87">&nbsp;</td>
          <td colspan="5" valign="bottom"><select name="select_tp" id="select2" onChange="show_format()">
              <option value="the_date">by date</option>
              <option value="letter_to">by letter to</option>
              <option value="taken_by">by taken by</option>
              <option value="ref_no" selected>by reference no.</option>
                    </select>
            <input name="search" type="text" id="search3">
            <input name="submit_go" type="Submit" id="submit_go" value="Go" alt="Search"></td>
          <td width="76">&nbsp;</td>
        </tr>
        <tr>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
          <td colspan="4">&nbsp;</td>
        </tr>
      </table></td>
    </tr>
  </table>
</form>
<?php
}

function display_form_2($result){
?>
<form name="form_ref" method="post" action="index.php">
  <table width="500" border="1" align="center" cellpadding="0" cellspacing="0" bordercolor="#CCCCCC">
    <tr>
      <td><table width="500" border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF">
        <tr>
          <td colspan="7"><div align="center" class="style1">
              <h2 class="style1">PRINSIPTEK</h2>
          </div></td>
        </tr>
        <tr>
          <td colspan="7"><div align="center">
              <h4>Reference No. </h4>
          </div></td>
        </tr>
        <tr>
          <td colspan="7">&nbsp;</td>
        </tr>
        <tr>
          <td colspan="7"><table width="450" border="1" align="center" cellpadding="0" cellspacing="0">
            <tr>
              <td>			 
			  <table width="450" border="0" align="center" cellpadding="5" cellspacing="0" bordercolor="#CCCCCC">
			  <tr bgcolor="#CCCCCC">
			  	<td></td>
			  	<td><strong>Reference No.</strong></td>
				<td><strong>Date</strong></td>
				<td><strong>Description</strong></td>
				<td><strong>Letter To</strong></td>
				<td><strong>Taken By</strong></td>
			  </tr>
			  <?php
			  	$i=0;
				$j=1;
				$end_num=6;
				$resultid=array(0=>"id",1=>"ref_no",2=>"the_date",3=>"description",4=>"letter_to",5=>"taken_by");
				while($myresult=mysql_fetch_assoc($result)){
					++$i;		
					$k=1;	
					if($i%2){						
				?>
                <tr bgcolor="#F3F3F3">
                  <td><?php echo $i; ?>.<input id="txt<?php echo $j; ?>" type="hidden" name="txt<?php echo $j; ?>" value="<?php echo $myresult["id"]; ?>"></td>
				  <?php  		
						$j=$j+1;
						for($j;$j<=$end_num;++$j,++$k){
				  ?>
                  <td><input name="txt<?php echo $j; ?>" type="text" id="txt<?php echo $j; ?>" size="15" value="<?php echo $myresult[$resultid[$k]]; ?>"></td>
				  <?php
				  		}
				  ?>
                </tr><?php
					}else{						
				?>
                <tr bgcolor="#CCCCCC">
                  <td><p><?php echo $i; ?>.<input id="txt<?php echo $j; ?>" type="hidden" name="txt<?php echo $j; ?>" value="<?php echo $myresult["id"]; ?>">
                  </td>
                  <?php
						$j=$j+1;
				  		for($j;$j<=$end_num;++$j,++$k){
				  ?>
                  <td><input name="txt<?php echo $j; ?>" type="text" id="txt<?php echo $j; ?>" size="15" value="<?php echo $myresult[$resultid[$k]]; ?>"></td>
				  <?php
				  		}
				  ?>
                </tr><?php
					}
					$end_num=$end_num+6;
				}
				?>
              </table></td>
            </tr>
          </table>            </td>
        </tr>
        <tr>
          <td colspan="7">&nbsp;</td>
        </tr>
        <tr>
          <td>&nbsp;</td>
          <td width="55"><input name="submit_add" type="Submit" id="submit_add" value="Add" alt="Add a reference" disabled></td>
          <td width="44">&nbsp;            </td>
          <td width="107"><input name="submit_save" type="submit" id="submit_save2" value="Save" alt="Save reference"></td>
          <td width="128"><input name="but_gen" type="button" id="but_gen" value="Generate Ref. No." alt="Generate reference no." onClick="javascript:generate_ref();" disabled></td>
          <td width="3">&nbsp;</td>
        </tr>
        <tr>
          <td>&nbsp;</td>
          <td colspan="5">&nbsp;</td>
          <td>&nbsp;</td>
        </tr>
        <tr>
          <td>&nbsp;</td>
          <td colspan="5">Search:</td>
          <td>&nbsp;</td>
        </tr>
        <tr>
          <td width="87">&nbsp;</td>
          <td colspan="5" valign="bottom"><select name="select_tp" id="select2" onChange="show_format()">
              <option value="the_date">by date</option>
              <option value="letter_to">by letter to</option>
              <option value="taken_by">by taken</option>
              <option value="ref_no" selected>by reference no.</option>
                    </select>
            <input name="search" type="text" id="search3">
            <input name="submit_go" type="Submit" id="submit_go" value="Go" alt="Search" onChange="show_format()"></td>
          <td width="76">&nbsp;</td>
        </tr>
        <tr>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
          <td colspan="4">&nbsp;</td>
        </tr>
      </table></td>
    </tr>
  </table>
  <input name="total_fld" type="hidden" id="total_fld" value="<?php echo --$j; ?>">
  <input name="result" type="hidden" id="result" value="<?=$result?>">
</form>
<?php
#mysql_close($db);
}
?>
</body>
</html>
 
what i mean is that the error shows that the session variable is undefined, if a page(for example:page1.php) was submitted to itseft.(page1.php submitted to page1.php, the value of the session variable becomes nothing)
 
I think Kenrbnsn meant the part where you assign the session variables. I can't find anywhere in your code where you do something like:
Code:
$_SESSION['username']="something";

If you assign them in another page the shows where you assign it.

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
u r rite, i didn't assign anything yet to the session...

i think i have not enough experience in using session variables, i will try get in depth to it.

anyway thank for the valuable comments. Thanks!
 
It is not that difficult. The first time you create your session, you create the values yu want to refer to throughout the website, such as:
Code:
$_SESSION['username']="something";

Once that is doine, the variable should be available whenever you wanted or until you call session_unset(). which frees all session variables currently registered.

Hope this sheds a light on the subject.

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top