Jump to content

Setting the cookie using form values...


suttercain

Recommended Posts

Good day to everyone,

 

I am taking a php class and have a homework assignment. The teacher wants us to

 

1. Have a form.

2. Validate the form.

3. If validated set a cookie using the form inputs as values.

 

I am stuck on step three.

 

Here is the code I am using, all within one page (I like PHP_SELF for some reason).

 

Code:

<?php
$value = array('$year', '$miles', '$colour', '$model');
setcookie("automobile", $value, time()+3600);

if(isset($_POST['submit'])){ // If the form was submitted
     validate(); // Check for empty fields
     if(count($errors) != 0){ // If there are errors, let the user know.
      	show_form();// redisplay the form
     } else { 

 move_on(); } // After the user submits the form, if validated, will check the cookie status
   }
   else{show_form();}

function move_on() {
if (isset($_COOKIE['automobile'])) {
       print_r ($_COOKIE);
echo "Cookie is set!";
	} else {
echo "Cookie is not set!";
	}
}

function validate() {
global $errors;
	if ($_POST['year'] == "" || !preg_match('/\d{4}/',$_POST['year'])) {
	$errors['year']="<font color='red'>* Please enter a four digit year.</font>";
	}
	if ($_POST['miles'] == "") {
	$errors['miles']="<font color='red'>*</font>";
	}
	if ($_POST['colour'] == "") {
	$errors['colour']="<font color='red'>*</font>";
	}
	if ($_POST['model'] == "") {
	$errors['model']="<font color='red'>*</font>";
			} 
	}

//If the first time view the page or if there are errors
function show_form() {
global $errors;
?>

<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
Year:<?php echo $errors['year']; ?><br />
<input name="year" type="text" value="<?php echo $_POST['$year']; ?>" size="4" maxlength="4" />
<br /><br />
Miles:<?php echo $errors['miles']; ?><br />
<input name="miles" type="text" value="<?php echo $_POST['miles']; ?>" size="6" maxlength="6" />
<br /><br />
Colour:<?php echo $errors['colour']; ?><br />
<input name="colour" type="text" value="<?php echo $_POST['colour']; ?>" size="12" maxlength="12" />
<br /><br />
Model:<?php echo $errors['model']; ?><br />
<input name="model" type="text" value="<?php echo $_POST['model']; ?>" size="20" maxlength="20" />
<br /><br />
<input type="submit" name="submit" />
</form>
<?php
}
?>

 

When I check if the cookie is set, I get that it isn't. I am not sure how or why it is not set.

 

Any suggestions? Thanks.

Link to comment
Share on other sites

In that code those values arnt specified as anything, if your submitting the form to itself you need to do like $year=$_POST['year'];......I am not that familiar with cookies, but can you set it as an array? I think you need a single cookie for each value or insert each value seperatly in the same cookie...try inserting each value seperatly in the cookie with multiple setcookie() commands...

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.