suttercain Posted April 23, 2007 Share Posted April 23, 2007 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. Quote Link to comment Share on other sites More sharing options...
mpharo Posted April 23, 2007 Share Posted April 23, 2007 What is the error your getting? Just looking at your code, you do not set $value up as anything...I dont know if your passing it from another page or what, but $value is not set as variable, so essentially your cookie has no data.. Quote Link to comment Share on other sites More sharing options...
suttercain Posted April 23, 2007 Author Share Posted April 23, 2007 I thought I had set it here: $value = array('$year', '$miles', '$colour', '$model'); Or do I need to assign a variable to $year, etc.. like so $year= $_POST['year']; ? Quote Link to comment Share on other sites More sharing options...
mpharo Posted April 23, 2007 Share Posted April 23, 2007 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... Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.