Sergiu Posted June 21, 2011 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 Link to comment Share on other sites More sharing options...
Paul Swanson Posted June 21, 2011 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 Link to comment Share on other sites More sharing options...
Sergiu Posted June 22, 2011 Author 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. Link to comment Share on other sites More sharing options...
Larry Posted June 22, 2011 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. Link to comment Share on other sites More sharing options...
ChrisElliott Posted October 30, 2011 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. Link to comment Share on other sites More sharing options...
Larry Posted October 31, 2011 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. Link to comment Share on other sites More sharing options...
ChrisElliott Posted November 1, 2011 Share Posted November 1, 2011 I've tried this with $_COOKIE and $_POST and got the same result. Link to comment Share on other sites More sharing options...
ChrisElliott Posted November 1, 2011 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. Link to comment Share on other sites More sharing options...
Larry Posted November 4, 2011 Share Posted November 4, 2011 Ah, excellent. Kudos for catching that and thanks for letting us know. Link to comment Share on other sites More sharing options...
Recommended Posts