Sergiu 0 Posted June 21, 2011 Report Share Posted June 21, 2011 Hi everyone, i wonder if anyone has managed solving the exercise from chapter 9, page 256, "make the form in customize.php sticky, so that reflects the user's current choices". It's about making a sticky form for a pull-down menu. I've thought about this version: "<select name="font_size"> <option value="">Font Size</option> <option value="xx-small" <?php if ($_POST['font_size']=="xx-small") {print "selected=selected";} ?> >XX-Small</option> <option value="x-small" <?php if ($_POST['font_size']=="x-small") {print "selected=selected";} ?> >X-Small</option>...etc." but it's not correct. Any suggestion would be greatly appreciated, thanks Quote Link to post Share on other sites
Paul Swanson 104 Posted June 21, 2011 Report Share Posted June 21, 2011 Is the problem that you receive error messages when the page loads, or is the field just not sticky? It looks like what you have would correctly make the field sticky, but you should be seeing 'undefined variable' messages when the page loads because the $_POST variables won't exist until the form is submitted. Try this: <select name="font_size"> <option value="">Font Size</option> <option value="xx-small" <?php if (isset ($_POST['font_size']) && $_POST['font_size']=="xx-small") {print "selected=selected";} ?> >XX-Small</option> <option value="x-small" <?php if (isset ($_POST['font_size']) && $_POST['font_size']=="x-small") {print "selected=selected";} ?> >X-Small</option> </select> The code now looks to see if the $_POST['font_size'] variable exists before trying the comparison, so you shouldn't see any "undefined variable 'font_size'" messages. 1 Quote Link to post Share on other sites
Sergiu 0 Posted June 22, 2011 Author Report Share Posted June 22, 2011 Thanks Paul for your suggestion! The thing is, that exercise being at the end of chapter about cookies (chapter 9), i was thinking that the author had in mind a solution involving cookies, but that's not necessarily being the case, i guess. Quote Link to post Share on other sites
Larry 433 Posted June 22, 2011 Report Share Posted June 22, 2011 Paul may not have been looking at the book (I suspect he doesn't have it). Just take the code he provided and replace uses of $_POST with $_COOKIE and you should be fine. Quote Link to post Share on other sites
ChrisElliott 0 Posted October 30, 2011 Report Share Posted October 30, 2011 I've been struggleing with this problem for a little while now. I tried Paul's code suggestion, but it isn't working for me still. Can anyone tell me what I'm doing wrong? <form action = "customize.php" method="post"> <select name="font_size"> <option value="">Font Size</option> <option value="xx-small" <?php if (isset($_POST['font_size']) && $_POST['font_size']=="xx-small"){print "selected=selected";} ?> >xx-Small</option> <option value="x-small" <?php if (isset($_POST['font-size']) && $_POST['font-size']=="x-small"){print "selected=selected";} ?>>x-small</option> <option value="small" <?php if (isset($_POST['font-size']) && $_POST['font-size']=="small"){print "selected=selected";} ?>>small</option> <option value="medium" <?php if (isset($_POST['font-size']) && $_POST['font-size']=="medium"){print "selected=selected";} ?>>medium</option> <option value="large" <?php if (isset($_POST['font-size']) && $_POST['font-size']=="large"){print "selected=selected";} ?>>large</option> <option value="x-large" <?php if (isset($_POST['font-size']) && $_POST['font-size']=="x-large"){print "selected=selected";} ?>>x-large</option> <option value="xx-large" <?php if (isset($_POST['font-size']) && $_POST['font-size']=="xx-large"){print "selected=selected";} ?>>xx-large</option> </select> <select name="font_color"> <option value="" selected = "selected">Font Color</option> <option value="999" <?php if (isset($_POST['font-color']) && $_POST['font-color']=="999") {print "selected=selected"; } ?> >Gray</option> <option value="0c0" <?php if (isset($_POST['font-color']) && $_POST['font-color']=="0c0") {print "selected=selected"; } ?> >Green</option> <option value="00f" <?php if (isset($_POST['font-color']) && $_POST['font-color']=="00f") {print "selected=selected"; } ?> >Blue</option> <option value="c00" <?php if (isset($_POST['font-color']) && $_POST['font-color']=="c00") {print "selected=selected"; } ?> >Red</option> <option value="000" <?php if (isset($_POST['font-color']) && $_POST['font-color']=="000") {print "selected=selected"; } ?> >Black</option> </select> <input type="submit" name="submit" value="Set My Preferences" /> </form> When I run this code, it doesn't seem to do anything different...it doesn't appear to be sticky. Quote Link to post Share on other sites
Larry 433 Posted October 31, 2011 Report Share Posted October 31, 2011 If you look at my comment just before yours, you'll see that I say you need to use $_COOKIE instead of $_POST. Quote Link to post Share on other sites
ChrisElliott 0 Posted November 1, 2011 Report Share Posted November 1, 2011 I've tried this with $_COOKIE and $_POST and got the same result. Quote Link to post Share on other sites
ChrisElliott 0 Posted November 1, 2011 Report Share Posted November 1, 2011 I figured out that I was trying to use font-size and font-color instead of font_size and font_color. Thanks for your help, Larry. Quote Link to post Share on other sites
Larry 433 Posted November 4, 2011 Report Share Posted November 4, 2011 Ah, excellent. Kudos for catching that and thanks for letting us know. Quote Link to post Share on other sites
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.
Note: Your post will require moderator approval before it will be visible.