Page 1 of 1

first time using sessions

Posted: Sun Mar 28, 2004 2:20 pm
by John Cartwright
This seems to work ok....

Code: Select all

<?php
<INPUT name=type_other value="<? echo $_SESSION['type_other']; ?>" size=50><br>
                <? 
				
				$type_other = $_POST['type_other']; 
				$_SESSION['type_other'] = $type_other; 
				
				 ?>
?>
But the problem is this.

You are at index.php.. and then click on order.php which loads it up fine.. you then have a form to fill out. Let's concentrate on just 1 field for now.. lets say type_other. You fill it out and click submit and since other fields are empty the form is reloaded. The field type_other is supposed to be filled from the session but it is not.. when you click on refresh it is then shown.. any ideas why? If this example is not sufficient I'll show you the whole script.

*edit* on every single page that i want to display a session I have to reload the page for it to be shown.. not just the form......

Posted: Sun Mar 28, 2004 3:32 pm
by John Cartwright
up

Posted: Sun Mar 28, 2004 3:49 pm
by markl999
Probably need to see more code, but it just sounds like the order of things that maybe wrong. (ie where you start the session, assign the session var etc).

Posted: Sun Mar 28, 2004 3:52 pm
by John Cartwright
This is my whole script.. as you can see the sessionstart is the first thing

Code: Select all

<? 
session_start();
include "inc/design1.php"; 

echo $_SESSION['type_other'];

if (isset($confirm)){

$to="cartizzle@hotmail.com";
$subject="A web design Order";

mail ($to, $subject, "Name: $name\nE-mail: $email\nMessage: $message\n\nIP: $REMOTE_ADDR", "From: $email") or print "An unknown error occured.<br>";

}else{

$error=0;
$missing = array();
$view=1;
if(isset($submit)){ 

# # # # # # # # # # ERROR CHECKING # # # # # # # # # #  
					   
if(!empty($_POST)){  
  $required =      array('type','description','firstname','lastname','organization','city','state','country','email');
  foreach($required as $req){ 
    if(empty($_POST[$req])){ 
      $missing[] = $req; 
      $error="1"; 
  
  } } } 

# # # # # # # # # # # # # # # # # # # # # # # # # # # # 

if ($error==0){



$formvariables = array($type,$type_other,$logodesign,$bannerdesign,$graphicdesign,$shoppingcart,
					   $interactivefeatures,$flashanimationandpresentation,$webhosting,$domainnameregistration,$sitemaintenance,
					   $editingandwritting,$merchantaccount,$customscripts,$creditcardprocessing,$searchengineoptimization,
					   $searchcapabilitiesonthewebsite,$databaseprogramming,$budget,$time_start,$time_frame,$description,
					   $firstname,$lastname,$organization,$address,$city,$state,$country,$phone,$fax,$email,$icq,
					   $contact_email,$contact_phone,$contact_icq,$tog);
					   
$feature =       array($logodesign,$bannerdesign,$graphicdesign,$shoppingcart,
					   $interactivefeatures,$flashanimationandpresentation,$webhosting,$domainnameregistration,$sitemaintenance,
					   $editingandwritting,$merchantaccount,$customscripts,$creditcardprocessing,$searchengineoptimization,
					   $searchcapabilitiesonthewebsite,$databaseprogramming);



# # # # # # # # # # # # # # # # # # # # # # # # # # # # 
########################################################
# # # # # # # # # # MAKING THE EMAIL # # # # # # # # # # 

$message = '<b><font size=3><u>Information about your project</u></font></b><br><br>';
$message .= '<b>Type of site: </b>'.$_SESSION['budget'].'<br>';		   
if ($type_other != ""){ 
$message .= '<b>Other: </b>'.$_SESSION['type_other'].'<br>'; 
} 
$message .= '<b>Features for the site: </b><br>';
foreach($feature as $feat){
if(isset($feat)){
$message .= '- '.$feat.'<br>';
}
} 
if (isset($features_other1)){
$message .= $features_other1.'<br>';
} 
$message .= '<b>Estimated Budget:</b> '.$budget.'<br>';
$message .= '<b>Time until we can start project: </b>'.$time_start.'<br>';
$message .= '<b>Time Frame: </b>'.$time_frame.'<br>';
$message .= '<b>Overview: </b>'.$description.'<br>'; 

echo "<br>";

echo "<table border="1" width="452" border="1" cellpadding="0" cellspacing="0" bordercolor="9A9A9A" align="center">\n".
     		"<tr class="porttext" bordercolor="EFEFEF" bgcolor="EFEFEF"><td width="5%"><td width="90%">\n";	 
echo "<br>".$message."<br>";
echo "<td width="5%">\n";
echo "</td></td></td></tr>\n";
echo "<tr bordercolor="EFEFEF" bgcolor="EFEFEF"><td><td><a class="conflink" href="?confirm=">Click here to confirm</a><br><td>&nbsp;</td>&nbsp;</td>&nbsp;</td></tr>\n";
echo "</table>\n";

# # # # # # # # # # # # # # # # # # # # # # # # # # # # 

} 
}if ($error=="1" || $view=="1"){ 

?>

<FORM action=order.php method=post>
  
  <div align="center"><br>
    
	<table class="porttext" width="95%" border="0">
      <tr>
        <td <? if(!empty($missing)){ echo ' class="error"'; } ?>> 

		<? 
		
		   if(!empty($missing)){ echo 'You have incomplete or incorrect fields. The required fields that need corrections are 
		                               highlighted in red.'; 
		   }else{ echo 'Please fill out the following form that will give us a good idea about your project. We will get back to 
		                you within 24 hours with an estimate or additional questions about your site. Thank you!'; } 
						
		?>
		
		</td>
      </tr>
    </table>
    
	<br></div>
  
  <TABLE class="porttext" cellSpacing=1 cellPadding=2 width=95% align=center bgColor=#9A9A9A border=0>
    <TBODY>
      <TR bgcolor="#CCCCCC" > 
        <TD height="23" colSpan=2 bgcolor="#CCCCCC"><B>Information about your project:</B></TD>
      </TR>
      <TR> 
        <TD colSpan=2 bgcolor="#ffffff"> <TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
            
		  <TBODY class="porttext">
              <TR> 
                <TD <? if(in_array('type', $missing)){ echo 'class="error"'; }else{ echo 'class="error2"'; } ?> height="19" colSpan=2>
				Are you looking for: *
				</TD>	  
			  			  </TR>
              <TR> 
                <TD width="6%"><INPUT type=radio value="New Web site design" name=type></TD>
                <TD width="94%">New Web site design </TD>
              <? $_SESSION['type'] = $type; ?>
			  </TR>
              <TR> 
                <TD><INPUT type=radio value="Re-design of the current Web site" name=type></TD>
                <TD>Re-design of the current Web site</TD>
                <? $_SESSION['type'] = $type; ?>
			  </TR>
              <TR> 
                <TD><INPUT type=radio value="Other" name=type></TD>
                <TD>Other (please specify below):</TD>
                <? $_SESSION['type'] = $type; ?>
			  </TR>
              <TR> 
                <TD></TD>
                <TD>
				
				<INPUT class="texta" name=type_other value="<? echo $_SESSION['type_other']; ?>" size=50><br>
                <? 
				$type_other = $_POST['type_other']; 
				$_SESSION['type_other'] = $type_other; 
				?>
				
				<br>
                </TD>
              </TR>
            </TBODY>
          </TABLE>
          
		  <TABLE class="porttext" cellSpacing=0 cellPadding=0 width="100%" border=0>
            <TBODY>
              <TR> 
                <TD colSpan=2><B>Please select features and services you are interested in:</B></TD>
              </TR>
              <TR> 
                <TD><INPUT name=logodesign type=checkbox id="logodesign" value="Logo Design"></TD>
                <TD>Logo Design</TD>
              <? $_SESSION['logodesign'] = $logodesign; ?>
			  </TR>
              <TR> 
                <TD><INPUT name=bannerdesign type=checkbox id="bannerdesign" value="Banner Design"></TD>
                <TD>Banner Design</TD>
              <? $_SESSION['organization'] = $organization; ?>
			  </TR>
              <TR> 
                <TD><INPUT name=graphicdesign type=checkbox id="graphicdesign" value="Graphic Design"></TD>
                <TD>Graphic Design</TD>
              <? $_SESSION['graphicdesign'] = $graphicdesign; ?>
			  </TR>
              <TR> 
                <TD><INPUT name=shoppingcart type=checkbox id="shoppingcart" value="Shopping Cart"></TD>
                <TD>Shopping Cart</TD>
              <? $_SESSION['shoppingcart'] = $shoppingcart; ?>
			  </TR>
              <TR> 
			  
			  
                <TD><INPUT name=interactivefeatures type=checkbox id="interactivefeatures" value="Interactive Features"></TD>
                <TD>Interactive Features (Chat, Forum, Forms)</TD>
              </TR>
              <? $_SESSION['interactivefeatures'] = $interactivefeatures; ?>
			  <TR> 
                <TD><INPUT name=flashanimationandpresentation type=checkbox id="flashanimationandpresentation" value="Flash Animation & Presentation"></TD>
                <TD>Flash Animation & Presentation</TD>
              </TR>
              <? $_SESSION['flashanimationandpresentation'] = $flashanimationandpresentation; ?>
			  <TR> 
                <TD><INPUT name=webhosting type=checkbox id="webhosting" value="Web Hosting"></TD>
                <TD>Web Hosting</TD>
              <? $_SESSION['webhosting'] = $webhosting; ?>
			  </TR>
              <TR> 
                <TD><INPUT name="domainnameregistration" type=checkbox id="domainnameregistration" value="Domain Name Registration"></TD>
                <TD>Domain Name Registration</TD>
              <? $_SESSION['domainnameregistration'] = $domainnameregistration; ?>
			  </TR>
              <TR> 
                <TD><INPUT name=sitemaintenance type=checkbox id="sitemaintenance" value="Site Maintenance"></TD>
                <TD>Site Maintenance</TD>
              <? $_SESSION['sitemaintenance'] = $sitemaintenance; ?>
			  </TR>
              <TR> 
                <TD><INPUT name=editingandwritting type=checkbox id="editingandwritting" value="Editing and Writing"></TD>
                <TD>Editing and Writing</TD>
              <? $_SESSION['editingandwritting'] = $editingandwritting; ?>
			  </TR>
              <TR> 
                <TD><INPUT name=merchantaccount type=checkbox id="merchantaccount" value="Merchant Account"></TD>
                <TD>Merchant Account</TD>
              <? $_SESSION['merchantaccount'] = $merchantaccount; ?>
			  </TR>
              <TR> 
                <TD><INPUT name=customscripts type=checkbox id="customscripts" value="Custom Scripts"></TD>
                <TD>Custom Scripts</TD>
              <? $_SESSION['customscripts'] = $customscripts; ?>
			  </TR>
              <TR> 
                <TD><INPUT name=creditcardprocessing type=checkbox id="creditcardprocessing" value="Credit Card Processing"></TD>
                <TD>Credit Card Processing</TD>
              <? $_SESSION['creditcardprocessing'] = $creditcardprocessing; ?>
			  </TR>
              <TR> 
                <TD><INPUT name=searchengineoptimization type=checkbox id="searchengineoptimization" value="Search Engine Optimization"></TD>
                <TD>Search Engine Optimization</TD>
              <? $_SESSION['searchengineoptimization'] = $searchengineoptimization; ?>
			  </TR>
              <TR> 
                <TD><INPUT name=searchcapabilitiesonthewebsite type=checkbox id="searchcapabilitiesonthewebsite" value="Search Capabilities on the Web Site"></TD>
                <TD>Search Capabilities on the Web Site</TD>
              <? $_SESSION['searchcapabilitiesonthewebsite'] = $searchcapabilitiesonthewebsite; ?>
			  </TR>
              <TR> 
                <TD><INPUT name=databaseprogramming type=checkbox id="databaseprogramming" value="Database Programming"></TD>
                <TD>Database Programming</TD>
              <? $_SESSION['databaseprogramming'] = $databaseprogramming; ?>
			  </TR>
              <TR> 
                <TD height="21"><INPUT type=checkbox value=Other name="Other"></TD>
                <TD>Other (please specify below):</TD>
              <? $_SESSION['Other'] = $Other; ?>
			  </TR>
              <TR> 
                <TD></TD>
                <TD><TEXTAREA class="textb" name="features_other" cols=42 rows=3 id="features_other"><? echo "$features_other" ?></TEXTAREA></TD>
              <? $_SESSION['features_other'] = $features_other; ?>
			  </TR>
            </TBODY>
          </TABLE>
          <br>
          <table class="porttext" width="95%" border="0" cellpadding="0" cellspacing="0">
            <tr> 
              <td width="80%" height="20"> Estimate Budget:</td>
              <td width="44%"> 
			      <select class="texta" name="budget">
                  <option value="Under $750" selected>Under $750 
                  <option value=$750-1,500>$750-1,500 
                  <option value=$1500-2500>$1,500-2,500 
                  <option value=$2500-4000>$2,500-4,000 
                  <option value=$4000-7000>$4,000-7,000 
                  <option value=$7000-10000$>$6,000-10,000 
                  <option value="$10,000 or higher">$10,000 or higher</option>
                  </select>
			  <? $_SESSION['budget'] = $budget; ?>
			  </td>
            </tr>
            <tr> 
              <td height="20"><div align="left">How soon do you need to start the project?:</div></td>
              <td>
			  	  <select class="texta" name="time_start">
                  <option value=A.S.A.P. selected>A.S.A.P. 
                  <option value="Within 2 weeks">Within 2 weeks 
                  <option value="Within 2-4 weeks">Within 2-4 weeks 
                  <option value="Within 4-8 weeks">Within 4-8 weeks 
                  <option value="Within 8-12 weeks">Within 8-12 weeks 
                  <option value="12+ weeks">12+ weeks</option>
                </select>
			  <? $_SESSION['time_start'] = $time_start; ?>
			  </td>
            </tr>
            <tr> 
              <td height="20"><div align="left">Time Frame:</div></td>
              <td>
			      <select class="texta" name="time_frame">
                  <option value="Less than a week" selected>Less than a week 
                  <option value="1-2 weeks">1-2 weeks 
                  <option value="2-3 weeks">2-3 weeks 
                  <option value="3-5 weeks">3-5 weeks 
                  <option value="5-8 weeks">5-8 weeks 
                  <option value="8-12 weeks">8-12 weeks 
                  <option value="12+ weeks">12+ weeks</option>
                  </select>
			  <? $_SESSION['time_frame'] = $time_frame; ?>
			  </td>
            </tr>
          </table>
          
		  <br>
 
           <TABLE class="porttext" cellSpacing=0 cellPadding=0 width="100%" border=0>
            <TBODY>
              <TR> 
                <TD height="19" colspan="2" <? if(in_array('description', $missing)){ echo 'class="error"'; }else{ echo 'class="error2"'; } ?>>
				Please provide a brief overview of the project: *
				</TD>
              </TR>
              <TR> 
                <TD width="6%"></TD>
                <TD width="94%"e><textarea class="textb" name="description" rows=5 cols=45><? echo "$description" ?></textarea>
				<? $_SESSION['description'] = $description; ?>
				</TD>
              </TR>
            </TBODY>
          </TABLE>
          <br>
        </TD>
      </TR>
    </TBODY>
  </TABLE>
  <BR>
  <TABLE class="porttext" cellSpacing=1 cellPadding=2 width=95% align=center bgColor=#9A9A9A 
      border=0>
    <TBODY>
      <TR bgcolor="#CCCCCC"> 
        <TD colSpan=2><B>Your contact information</B></TD>
      </TR>
      <TR bgColor=#ffffff> 
        <TD <? if(in_array('firstname', $missing)){ echo 'class="error"'; }else{ echo 'class="error3"'; } ?>>Your first name *</TD>
        <TD>&nbsp; <INPUT class="texta" name="firstname" value="<? echo "$firstname" ?>" size=25> <? $_SESSION['firstname'] = $firstname; ?> 
        </TD>
      </TR>
      <TR bgColor=#ffffff> 
        <TD <? if(in_array('lastname', $missing)){ echo 'class="error"'; }else{ echo 'class="error3"'; } ?>>Last name *</TD>
        <TD>&nbsp; <INPUT class="texta" name="lastname" value="<? echo "$lastname" ?>" size=25> <? $_SESSION['lastname'] = $lastname; ?>
		</TD>
      </TR>
      <TR bgColor=#ffffff> 
        <TD <? if(in_array('organization', $missing)){ echo 'class="error"'; }else{ echo 'class="error3"'; } ?>>Organization (Company Name) *</TD>
        <TD>&nbsp; <INPUT class="texta" name="organization" value="<? echo "$organization" ?>" size=25> <? $_SESSION['organization'] = $organization; ?>
		</TD>
      </TR>
      <TR bgColor=#ffffff> 
        <TD>Address </TD>
        <TD>&nbsp; <INPUT class="texta" name="address" value="<? echo "$address" ?>" size=25 maxLength=255> <? $_SESSION['address'] = $address; ?>
		</TD>
      </TR>
      <TR bgColor=#ffffff> 
        <TD <? if(in_array('city', $missing)){ echo 'class="error"'; }else{ echo 'class="error3"'; } ?>>City *</TD>
        <TD>&nbsp; <INPUT class="texta" name="city" value="<? echo "$city" ?>" size=25> <? $_SESSION['city'] = $city; ?>
		</TD>
      </TR>
      <TR bgColor=#ffffff> 
        <TD <? if(in_array('state', $missing)){ echo 'class="error"'; }else{ echo 'class="error3"'; } ?>>Area (province, state) *</TD>
        <TD>&nbsp; <INPUT class="texta" name="state" value="<? echo "$state" ?>" size=25> <? $_SESSION['state'] = $state; ?>
		</TD>
      </TR>
      <TR bgColor=#ffffff> 
        <TD <? if(in_array('country', $missing)){ echo 'class="error"'; }else{ echo 'class="error3"'; } ?>>Country *</TD>
        <TD>&nbsp; <INPUT class="texta" name="country" value="<? echo "$country" ?>" size=25> <? $_SESSION['country'] = $country; ?>
		</TD>
      </TR>
      <TR bgColor=#ffffff> 
        <TD>Phone number including area code</TD>
        <TD>&nbsp; <INPUT class="texta" name="phone" value="<? echo "$phone" ?>" size=25 maxLength=25> <? $_SESSION['phone'] = $phone; ?>
		</TD>
      </TR>
      <TR bgColor=#ffffff> 
        <TD>Fax number (optional)</TD>
        <TD>&nbsp; <INPUT class="texta" name="fax" value="<? echo "$fax" ?>" size=25 maxLength=25> <? $_SESSION['fax'] = $fax; ?>
		</TD>
      </TR>
      <TR bgColor=#ffffff> 
        <TD <? if(in_array('email', $missing)){ echo 'class="error"'; }else{ echo 'class="error3"'; } ?>>Email *</TD>
        <TD>&nbsp; <INPUT class="texta" name="email" value="<? echo "$email" ?>" size=25 maxLength=255> <? $_SESSION['email'] = $email; ?>
		</TD>
      </TR>
      <TR bgColor=#ffffff> 
        <TD>ICQ</TD>
        <TD>&nbsp; <INPUT class="texta" name="icq" value="<? echo "$icq" ?>" size=25 maxLength=255> <? $_SESSION['icq'] = $icq; ?>
		</TD>
      </TR>
      <TR align=left bgColor=#ffffff> 
        <TD>Prefered way to contact you:</TD>
        <TD> <TABLE class="porttext" cellSpacing=0 cellPadding=0 align=left border=0>
            <TBODY>
              <TR> 
                <TD><INPUT name="contact_email" type=checkbox value="<? echo "$contact_email" ?>"></TD>
                <TD>E-Mail</TD>
              </TR>
              <TR> 
                <TD><INPUT type=checkbox value=YES name="contact_phone"></TD>
                <TD>Phone</TD>
              </TR>
              <TR> 
                <TD><INPUT type=checkbox value=YES name="contact_icq"></TD>
                <TD>ICQ</TD>
              </TR>
            </TBODY>
          </TABLE></TD>
      </TR>
    </TBODY>
  </TABLE>
  <br>
  <CENTER>
    <BR>
    <INPUT name="submit" type=submit value="Send request">
    <INPUT name="reset"  type=reset value="Clear form">
  </CENTER>
</FORM>

<? 
}
}

include "inc/design2.php";
 
 ?>

Posted: Sun Mar 28, 2004 3:58 pm
by markl999
Yeah, it's the order of things ;)
Take for example...

<INPUT class="texta" name=type_other value="<? echo $_SESSION['type_other']; ?>" size=50><br>
<?
$type_other = $_POST['type_other'];
$_SESSION['type_other'] = $type_other;
?>

If you think about the flow of the code, here's what happens.
1. You show a form
2. You set a session var to the value of $_POST['type_other'], which hasn't been set yet as the form hasn't been posted.
3. You post the form
4. You set the form element type_other to the value of the session var, which is empty at the moment (see number 2.)
5. The session variable is now assigned the value of the posted type_other (which now has a value from the form you've just posted).
6. You click refresh, or repost, and the form element now shows the correct value as the session var is now set.

So you basically need to move all the code that is similar to:
<?
$type_other = $_POST['type_other'];
$_SESSION['type_other'] = $type_other;
?>

out of the form and move it just under the session_start(), possibly in a code block like,
if(!empty($_POST['submit'])){
//assign all the session vars here
}

Hope that makes some kina sense ;)

Posted: Sun Mar 28, 2004 4:04 pm
by John Cartwright
Thank you :) let me give her a try and I'll post again if I have any problems... until then thank you once again :)

Edit: It is working great.. Thanks a bunch