Jump to content
Larry Ullman's Book Forums

Question Re Exercise From Chapter 9


Recommended Posts

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

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.

  • Upvote 1
Link to comment
Share on other sites

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

  • 4 months later...

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

 Share

×
×
  • Create New...