Home     Articles & Projects     Products & Web Services     Forum

Forms and PHP

Hi David,
Is it possible to "remember" the last selection in a dropdown form?

  print "<form method='get' action=''>";
  print "<select name='cur' onChange='this.form.submit()'>";

  function parseCurrency($record)
  {
print "<option value='".$record["ID"]."'>".$record["NAME"]."</option>";
  }
  print '</select>';
  print "</form>";

The dropdown form works with the cookie enabled as per the last post but would like it to keep the last selection.
Thanks,
Simon

Hi Simon, To pre-select a

Hi Simon,

To pre-select a choice in a drop-down box you need to add the attribute selected='selected' (XHTML compliant). So to do this, all you need to do is bring the current value of $cur in as a global variable and compare it to $record["ID"] (which I assume is the currency code value). Where it matches, you add the above attribute. For example:

  print "<form method='get' action=''>";
  print "<select name='cur' onChange='this.form.submit()'>";

  function parseCurrency($record)
  {
    global $cur;
    if ($cur == $record["ID"])
    {
      $selected = "selected='selected'";
    }
    else
    {
      $selected = "";
    }
    print "<option value='".$record["ID"]."' ".$selected.">".$record["NAME"]."</option>";
  }
  print '</select>';
  print "</form>";

Something like that should do the trick!
Cheers,
David.

Hi David, Thanks for that it

Hi David,
Thanks for that it worked a charm!
On the site I chose not to rewrite one section which was "Pricing and Availability". I am able to change the currency on any page except this page I chose not to rewrite. It throws up an error which is due to the variable not being passed by the form but I can't figure out why they aren't being sent!

On this page

http://www.scottishhotels.net/test/hotelDetailPrices.php?hotel_id=96704&hotel_name=Tigh-Dearg-Hotel-Leisure-Club&sdate=2007-12-18&nights=4

It gives the error why trying to change the currency. Could you once again point me in the right direction please?
Thanks,
Simon

Hello Simon, It looks like

Hello Simon,

It looks like the error here is simply a function of the usual parameters required by that page not being present after submitting the form. You can see this by going to the main URL without any parameters...

http://www.scottishhotels.net/test/hotelDetailPrices.php

The easiest way to deal with this would be to move the currency form to the POST method, which will make sure that the target script has the same query string as the original page. If you try this, you would need to change the currency pickup code from from the previous thread as follows:

<?php
// check for currency value on query string or cookie (in that order)
// or set default value
if ($_POST["cur"])
{
 
$cur = $_GET["cur"];
}
elseif(
$_COOKIE["cur"])
{
 
$cur = $_COOKIE["cur"];
}
else
{
 
$cur = "GBP"; // set the default value here
}
// now set a cookie for next time
setcookie("cur",$cur,strtotime("+1 Year"),"/");
?>

See what you think of that method - there is an alternative which involves making the query string a hidden field in the form, and then on the next page view you can use PHP's parse_str() function to extract the same variables from that as would have been brought in from the URL....

Cheers,
David.