trouble updating cookies (letting users update their info)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
reyns
Forum Newbie
Posts: 1
Joined: Fri Jun 27, 2003 7:52 pm
Contact:

trouble updating cookies (letting users update their info)

Post by reyns »

i'm trying to make a simple php-based form system for my site, where users can input their info, and get stuff to download back in return. i'm having some trouble getting it cookies-enabled. specifically, i'm finding that while it seems easy to create or delete cookie(s), setting it up so that if a returning user changes some of their information, i.e. deleting and rewriting or "updating" their cookie(s), is where i'm bailing. there's no db involved here yet.

here's how it's set up:

2 files, one form and one form handler. user accesses thru a link that leverages the form and passes it a parameter. ex:
http://www.subterreignmedia.com/case_st ... ted_cs=one

from there, these 2 files take over:


form.php -

Code: Select all

<?php




// create the vars needed for the page header and description 
if (isset($selected_cs))
	
	if ($selected_cs == "one")
		{
			$PageHead ="one ";
			$PageDesc = "one";
		}	
	elseif ($selected_cs =="ttwo")	
		{
			$PageHead = "two";
			$PageDesc = "two";
		}	
	elseif ($selected_cs =="three")	
		{
			$PageHead = "three";
			$PageDesc = "three";
		}	
	elseif ($selected_cs =="four")	
		{
			$PageHead = "five";
			$PageDesc = "five";
		}	
	elseif ($selected_cs =="six")	
		{
			$PageHead = "six";
			$PageDesc = "six";
		}	
	elseif ($selected_cs =="seven")	
		{
			$PageHead = "seven";
			$PageDesc = "seven";
		}	
	else
		{
			echo("Error: You have not passed a valid value to this page. Please go back and try again");
			exit();
		}	
else
	{
		echo("Error: Variable selected_cs must be passed to this page.");
		exit();
	}
			
			
			
?>			
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd"> 
<html> 
<head> 

<title>case studies </title>
<script language="JavaScript">
function validate(form)
{
	if (form.firstlastname.value.length == 0){alert("You must enter in a name.");return false;}	
	if (form.title.value.length == 0){alert("You must enter in a title.");return false;}	
	if (form.company.value.length == 0){alert("You must enter in a company name.");return false;}	
	if (form.address1.value.length == 0){alert("You must select in an address.");return false;}
	if (form.city.value.length == 0){alert("You must enter in a city.");return false;}
	if (form.state.value.length == 0){alert("You must enter in a state.");return false;}			
	if (form.country.value.length == 0){alert("You must enter in a country.");return false;}	
	if (form.leademail.value.length == 0){alert("You must enter provide an email address");return false;}	
}
</script>
</head>
<body bgcolor="#ffffff" marginheight="8" marginwidth="10" topmargin="8" leftmargin="10">

			<table border="0" cellpadding="0" cellspacing="0" width="450">
				<tr>
					<td valign="top"><?php echo $PageHead; ?>
						<br><br>
						<?php echo $PageDesc; ?> 
						<br><br>To download the Case Study in Adobe Acrobat 4 (.PDF) format, please provide us with the following information:
						<form action="case_formhandler.php" method="POST" name="form" ONSUBMIT="return validate(this);">
				
						<table border="0" border="0" cellpadding="2" cellspacing="2" width="400">

							<tr>
								<td  align="right">Name: <span class="reqfieldtxt">*</span> </td>
								<td  valign="top">
								<input type="text" size="30" name="firstlastname" value="<? if(isset($firstlastname)) echo $firstlastname; ?>">
								</td>
							</tr>
							<tr>
								<td  align="right">Title: <span class="reqfieldtxt">*</span> </td>
								<td  valign="top"><input type="text" size="30" name="title" value="<? if(isset($title)) echo $title; ?>"></td>
							</tr>
							<tr>
								<td  align="right">Company: <span class="reqfieldtxt">*</span> </td>
								<td  valign="top"><input type="text" size="30" name="company" value="<? if(isset($company)) echo $company; ?>"></td>
							</tr>
							<tr>
								<td  align="right">Address: <span class="reqfieldtxt">*</span> </td>
								<td  valign="top"><input type="text" size="35" name="address1" value="<? if(isset($address1)) echo $address1; ?>"></td>
							</tr>
							<tr>
								<td  align="right"><img src="/images/1pixel.gif" alt="" width="1" height="1"></td>
								<td  valign="top"><input type="text" size="35" name="address2" value="<? if(isset($address2)) echo $address2; ?>"></td>
							</tr>
							<tr>
								<td  align="right">City: <span class="reqfieldtxt">*</span> </td>
								<td  valign="top"><input type="text" size="30" maxlength="30" name="city" value="<? if(isset($city)) echo $city; ?>"></td>
							</tr>
							<tr>
								<td  align="right">State: <span class="reqfieldtxt">*</span> </td>
								<td  valign="top"><input type="text" size="20" maxlength="20" name="state" value="<? if(isset($state)) echo $state; ?>"></td>
							</tr>
							<tr>
								<td  align="right">ZIP Code: <span class="reqfieldtxt">*</span> </td>
								<td  valign="top"><input type="text" size="30" maxlength="30" name="postal" value="<? if(isset($postal)) echo $postal; ?>"></td>
							</tr>
							<tr>
								<td  align="right">Country: <span class="reqfieldtxt">*</span> </td>
								<td  valign="top"><input type="text" size="30" maxlength="30" name="country" value="<? if(isset($country)) echo $country; ?>"></td>
							</tr>
							<tr>
								<td  align="right">Phone #: <span class="reqfieldtxt">*</span> </td>
								<td  valign="top"><input type="text" size="30" maxlength="30" name="phone" value="<? if(isset($phone)) echo $phone; ?>"></td>
							</tr>
							<tr>
								<td  align="right">FAX #: </td>
								<td  valign="top"><input type="text" size="30" maxlength="30" name="fax" value="<? if(isset($fax)) echo $fax; ?>"></td>
							</tr>
							<tr>
								<td align="right">E-mail:  <span class="reqfieldtxt">*</span> </td>
								<td  valign="top"><input type="text" size="30" name="leademail" value="<? if(isset($leademail)) echo $leademail; ?>"></td>
							</tr>
							<tr>
								<td  align="right">
								Download Selected Case Studies: <span class="reqfieldtxt">*</span></td>
								<td  valign="top">
								<INPUT TYPE="checkbox" NAME="selected_cs[]" VALUE="one" <? if ($selected_cs=="one") {echo "checked";} ?> >one<br>
								<INPUT TYPE="checkbox" NAME="selected_cs[]" VALUE="two" <? if ($selected_cs=="two") {echo "checked";} ?>>two<br>
								<INPUT TYPE="checkbox" NAME="selected_cs[]" VALUE="three" <? if ($selected_cs=="three") {echo "checked";} ?>>three<br>
								<INPUT TYPE="checkbox" NAME="selected_cs[]" VALUE="four" <? if ($selected_cs=="four") {echo "checked";} ?>>four<br>
								<INPUT TYPE="checkbox" NAME="selected_cs[]" VALUE="five" <? if ($selected_cs=="five") {echo "checked";} ?>>five<br>
								<INPUT TYPE="checkbox" NAME="selected_cs[]" VALUE="six" <? if ($selected_cs=="six") {echo "checked";} ?>>six<br>
								
								</td>
							</tr>
							<tr>
								
								<td  valign="top"><input type="submit" value="Submit" name="submit">&nbsp; or &nbsp;<input type="reset" value="Clear" name="reset"></td>
							</tr>
						</table>
						<!-- End Table Holding Case Studies Request Form --->
						</form>
						<br><span >NOTE: Items with a <span class="reqfieldtxt">*</span> are required fields.
						<br><br>See our <a href="/privacy_policy.html" class="btmnavlink" target="new">Privacy Policy</a>.
						
				
					</td>	
				</tr>
			</table>


<!-- End Main Table -->

</body>
</html>
and formhandler.php -

Code: Select all

<?php

//timestamp
$today = getdate(); 
$mon = $today['mon']; 
$mday = $today['mday']; 
$year = $today['year']; 
$hours = $today['hours'];
$minutes = $today['minutes'];
$seconds = $today['seconds'];

//set cookies
if(isset($title)) setcookie("title", $title, time()+ 86400 * 356);  
if(isset($title)) setcookie("title", $title, time()+ 86400 * 356);  
if(isset($company)) setcookie("company", $company, time()+ 86400 * 356);  
if(isset($address1)) setcookie("address1", $address1, time()+ 86400 * 356);  
if(isset($address2)) setcookie("address2", $address2, time()+ 86400 * 356); 
if(isset($city)) setcookie("city", $city, time()+ 86400 * 356);  
if(isset($state)) setcookie("state", $state, time()+ 86400 * 356);  
if(isset($postal)) setcookie("postal", $postal, time()+ 86400 * 356);  
if(isset($country)) setcookie("country", $country, time()+ 86400 * 356);  
if(isset($phone)) setcookie("phone", $phone, time()+ 86400 * 356);  
if(isset($address1)) setcookie("address1", $address1, time()+ 86400 * 356);  
if(isset($fax)) setcookie("fax", $fax, time()+ 86400 * 356);  
if(isset($leademail)) setcookie("leademail", $leademail, time()+ 86400 * 356);  

// set static vars for page
$mail_to ="reyns@subterreignmedia.com";
$mail_from = $email2;


//check to see if at least one case study was selected
if (isset($selected_cs))
	{
	}
	else
	{
	echo("Error: You must select at least one case study to download. Please hit your browser's back button and select a case study");
	exit();
	}



//create list of selected newsletters
$count=count($selected_cs);
	for ($i=0; $i<$count; $i++) { 
		$selected_casestudies .= $selected_cs[$i];
		$selected_casestudies .= "\n";
		}
//construct email lead message
$mail_subject="Case Study from the  Website";
$mail_body="$mon/$mday/$year $hours:$minutes:$seconds\nCase Study Download from the  Website:\n\n";
$mail_body .="Name:    $firstlastname\n";
$mail_body .="Title:   $title\n";
$mail_body .="Company: $company\n";
$mail_body .="Phone:   $phone\n";
$mail_body .="Fax:     $fax\n";
$mail_body .="Email:   $leademail\n";
$mail_body .="Address: $address1 $address2\n";
$mail_body .="City:    $city\n";
$mail_body .="State:   $state\n";
$mail_body .="Postal:  $postal\n";
$mail_body .="Country: $country\n\n";
$mail_body .="Case Studies Selected:\n\n$selected_casestudies\n";
$mail_body = stripslashes($mail_body);




//send email message
mail($mail_to, $mail_subject, $mail_body, "From: webteam@subterreignmedia.com\nReply-To: webteam@subterreignmedia.com"); 

//write to log file
$writestring = "$mon/$mday/$year | $hours:$minutes:$seconds | $firstlastname | $title | $company | $phone | $fax | $leademail | $address1 $address2 | $city | $state | $postal | $country | $selected_casestudies \n"; 
$filepointer = fopen("/home/webmaster/subterreignmedia.com/formhandler-logs/casestudies/log.txt","a"); 
fwrite($filepointer,$writestring); 
fclose($filepointer);  
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd"> 
<html> 
<head> 

<title> Case Studies</title> 

</head>
<body bgcolor="#ffffff" marginheight="8" marginwidth="10" topmargin="8" leftmargin="10">




					<span class="head2txt">Download &#174; Case Studies</span>
					<br><br>
					Thank you for your interest in our products. To download case studies , click on the link(s) below:
					<br>
					<br>
						<table width="400" border="0" class="bodytxt">
							<?
							//loop the selected stuff to download
							$count=count($selected_cs);
							for ($i=0; $i<$count; $i++) {
							
								if ($selected_cs[$i]=="one") {
									echo "<tr><td><a href='/download/case_studies/one.pdf' target='_blank'>one</a></td></tr>";
								}
								if ($selected_cs[$i]=="two") {
									echo "<tr><td><a href='/download/case_studies/two.pdf' target='_blank'>two</a></td></tr>";
								}
								if ($selected_cs[$i]=="three") {
									echo "<tr><td><a href='/download/case_studies/three.pdf' target='_blank'>three</a></td></tr>";
								}
								if ($selected_cs[$i]=="four") {
									echo "<tr><td><a href='/download/case_studies/four.pdf' target='_blank'>four</a></td></tr>";
								}
								if ($selected_cs[$i]=="fiver") {
									echo "<tr><td><a href='/download/case_studies/five.pdf' target='_blank'>five</a></td></tr>";
								}
								if ($selected_cs[$i]=="six") {
									echo "<tr><td><a href='/download/case_studies/six.pdf' target='_blank'>six</a></td></tr>";
								}
							}
							?> 
						</table>
				


	
<!-- End Main Table -->

</body>
</html>
i've found a couple different things on the web today that supposedly fix this prob but so far none have worked reliably (even in browsers other than IE 6.0)... any ideas re. how i can help my visitors experience "editable" cookies here would be way cool.

cheers,
[sr]

[Admin Edit: Added

Code: Select all

tags - please use them it makes people more likely to look at your code]
Last edited by reyns on Sat Jun 28, 2003 11:27 am, edited 1 time in total.
qartis
Forum Contributor
Posts: 271
Joined: Sat Dec 14, 2002 4:43 pm
Location: BC, Canada
Contact:

Post by qartis »

I don't have time to parse that whole shebang right now, but looking it over just now, I saw

Code: Select all

if ($selected_cs&#1111;$i]=="onei") &#123;
I think you mean "one".

I'll look at the code a bit more tomorrow.
Post Reply