noobtopro Posted May 1, 2011 Share Posted May 1, 2011 Hello again everyone, I am new to PHP. This is the second part of a question: Question: In the program you created in question 4, allow your user the option of saving the information for the next time they visit. If they choose "yes", save information in a cookie. At the bottom of question5.php I added a checkbox to save the information. I then created an if statement on the processing page (question5display.php. I would appreciate some help, thanks guys. Here is the form ( called it question5.php): <html> <head> <title> Please Enter Your Text</title> </head> <body> <form method="post" action="question5display.php"> <p>Enter the text you want formatted please: <input type="text" name="textformat" maxlength="30" size="30" /> <table> <tr> <td><label for="font">Select Font:</label></td> <td><select id="font" name="font"> <option value="Verdana">Verdana</option> <option value="Arial">Arial</option> <option value="Times New Roman">Times New Roman</option> </select> </td> </tr> <tr> <td><label for ="size">Select Size:</label></td> <td><select id="size" name="size"> <option value="10px">10px</option> <option value="12px">12px</option> <option value="14px">14px</option> <option value="20px">20px</option> </select> </td> </tr> <tr> <td><label for="color">Select Color:</label></td> <td><select id="color" name="color"> <option value="green">Green</option> <option value="blue">Blue</option> <option value="red">Red</option> </select> </td> </tr> </table> <input type="checkbox" id="save_prefs" name="save_prefs" /> <label for="save_prefs">Save these preferences for the next time you log in.</label> <input type="submit" name="submit" value="Submit" /> </form> </body> </html> Here is the page it goes to (called it question5display): <?php if (isset($_POST['save_prefs'])) { setcookie('font', $_POST['font'], time() + 60); setcookie('size', $_POST['size'], time() + 60); setcookie('color', $_POST['color'], time() + 60); $_COOKIE['font'] = $_SESSION['font']; $_COOKIE['size'] = $_SESSION['size']; $_COOKIE['color'] = $_SESSION['color']; } session_start(); $_SESSION['textformat'] = $_POST['textformat']; $_SESSION['font'] = $_POST['font']; $_SESSION['size'] = $_POST['size']; $_SESSION['color'] = $_POST['color']; ?> <html> <head> </head> <body> <p <?php echo ' style="font-family: ' . $_SESSION['font'] . '; '; echo 'font-size: ' . $_SESSION['size'] . '; '; echo 'color: ' . $_SESSION['color'] . ';" '; ?>> <?php echo $_SESSION['textformat']; ?> </p> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/235237-form-option-to-set-cookie-help/ Share on other sites More sharing options...
sunfighter Posted May 1, 2011 Share Posted May 1, 2011 You are trying to set your cookie values from the session value, BUT you have not set session at this point. Change the php section to this: <?php session_start(); $_SESSION['textformat'] = $_POST['textformat']; $_SESSION['font'] = $_POST['font']; $_SESSION['size'] = $_POST['size']; $_SESSION['color'] = $_POST['color']; if (isset($_POST['save_prefs'])) { setcookie('font', $_POST['font'], time() + 60); setcookie('size', $_POST['size'], time() + 60); setcookie('color', $_POST['color'], time() + 60); $_COOKIE['font'] = $_SESSION['font']; $_COOKIE['size'] = $_SESSION['size']; $_COOKIE['color'] = $_SESSION['color']; } ?> Besides, you shouls start sessions as your first line of code. Quote Link to comment https://forums.phpfreaks.com/topic/235237-form-option-to-set-cookie-help/#findComment-1209071 Share on other sites More sharing options...
wildteen88 Posted May 1, 2011 Share Posted May 1, 2011 When setting cookies, the value of the cookies will not become available until you refresh the page. So you will not be able to immediately retrieve the cookie once you have set it. However you will when you reload page. Quote Link to comment https://forums.phpfreaks.com/topic/235237-form-option-to-set-cookie-help/#findComment-1209078 Share on other sites More sharing options...
sunfighter Posted May 1, 2011 Share Posted May 1, 2011 The second page with is called from the form does write to the browser, so it does set the cookies. I just gave a revised php section at the top of that page. Quote Link to comment https://forums.phpfreaks.com/topic/235237-form-option-to-set-cookie-help/#findComment-1209094 Share on other sites More sharing options...
wildteen88 Posted May 1, 2011 Share Posted May 1, 2011 The second page with is called from the form does write to the browser, so it does set the cookies. I just gave a revised php section at the top of that page. It will set the cookie. But you wont be able to retrieve the cookie immediately after you call setcookie(). That is what I trying to say. Try it yourself <?php setcookie('test', 'blah...', time()+60); echo "\$_COOKIE['test'] = " . $_COOKIE['test']; ?> When first run that code it will just display the text $_COOKIE['test'] = However when you reload the page it'll show $_COOKIE['test'] = blah.. Quote Link to comment https://forums.phpfreaks.com/topic/235237-form-option-to-set-cookie-help/#findComment-1209102 Share on other sites More sharing options...
noobtopro Posted May 1, 2011 Author Share Posted May 1, 2011 Thank you guys so much for all the help, you guys are awesome. Quote Link to comment https://forums.phpfreaks.com/topic/235237-form-option-to-set-cookie-help/#findComment-1209180 Share on other sites More sharing options...
noobtopro Posted May 1, 2011 Author Share Posted May 1, 2011 Thanks guys, here is what I came up with based on your advice. I believe it works: <?php setcookie('font', $_POST['font'], time() + 60); setcookie('size', $_POST['size'], time() + 60); setcookie('color', $_POST['color'], time() + 60); //setting cookies must be the first thing before any whitespace of any html or anything at all. $_COOKIE['font'] = $_POST['font']; $_COOKIE['size'] = $_POST['size']; $_COOKIE['color'] = $_POST['color']; session_start(); $_SESSION['textformat'] = $_POST['textformat']; $_SESSION['font'] = $_POST['font']; $_SESSION['size'] = $_POST['size']; $_SESSION['color'] = $_POST['color']; // Turn off all error reporting error_reporting(0); // Report simple running errors error_reporting(E_ERROR | E_WARNING | E_PARSE); // Reporting E_NOTICE can be good too (to report uninitialized // variables or catch variable name misspellings ...) error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE); // Report all errors except E_NOTICE // This is the default value set in php.ini error_reporting(E_ALL ^ E_NOTICE); // Report all PHP errors (see changelog) error_reporting(E_ALL); // Report all PHP errors error_reporting(-1); // Same as error_reporting(E_ALL); ini_set('error_reporting', E_ALL); ?> <?php if (isset($_POST['save_prefs'])) { echo '<html>'; echo '<head>'; echo '<title>here it is</title>'; echo '</head>'; echo '<body>'; echo '<p'; echo ' style="font-family: ' . $_COOKIE['font'] . '; '; echo 'font-size: ' . $_COOKIE['size'] . '; '; echo 'color: ' . $_COOKIE['color'] . ';" '; echo '>'; echo $_POST['textformat']; echo '</p>'; //just to check to see if the comp is inside the if statement echo $_COOKIE['font']; echo $_COOKIE['size']; echo $_COOKIE['color']; } else { echo '<html>'; echo '<head>'; echo '</head>'; echo '<body>'; echo '<p'; echo ' style="font-family: ' . $_SESSION['font'] . '; '; echo 'font-size: ' . $_SESSION['size'] . '; '; echo 'color: ' . $_SESSION['color'] . ';" '; echo '>'; echo $_SESSION['textformat']; echo '</p>'; } ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/235237-form-option-to-set-cookie-help/#findComment-1209187 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.