Unless a form was just submitted, no default option is set in a select
box, so the first option in alphabetical order is used. That can be
annoying, for instance, for users who mainly process data in one city
and find another city selected by default in all dropdowns.
On form submission we now track the selected city or area in a session
variable, and retrieve it when showing a form with no default option.
<?php
if ($_POST['area_name']) {
<?php
if ($_POST['area_name']) {
+ set_last_selected("city_id", $_POST['city_id']);
$id = add_area($_POST['area_name'], $_POST['city_id']);
if ($id !== false) {
echo "<p>Added area.</p>\n";
$id = add_area($_POST['area_name'], $_POST['city_id']);
if ($id !== false) {
echo "<p>Added area.</p>\n";
}
}
else if ($_POST['city_id']) {
}
}
else if ($_POST['city_id']) {
- /* XXX: city_id is actually a string $city_name/$city_id */
- header(sprintf("Location: http%s://%s/%s/in/city/%s", ($_SERVER['HTTPS']) ? "s" : "", $_SERVER['HTTP_HOST'], $module, $_POST['city_id']));
+ set_last_selected("city_id", $_POST['city_id']);
+ $city_id = $_POST['city_id'];
+ $city = get_city_by_id($city_id);
+ header(sprintf("Location: http%s://%s/%s/in/city/%s/%d", ($_SERVER['HTTPS']) ? "s" : "", $_SERVER['HTTP_HOST'], $module, $city->getName(), $city_id));
exit;
}
else if ($_POST['update_area']) {
exit;
}
else if ($_POST['update_area']) {
echo "<p>Show areas in\n";
echo "<select name=\"city_id\">\n";
foreach ($candidates as $city) {
echo "<p>Show areas in\n";
echo "<select name=\"city_id\">\n";
foreach ($candidates as $city) {
- option("city_id", sprintf("%s/%s", $city->getName(), $city->getId()), get_city_displayname($city));
+ option("city_id", $city->getId(), get_city_displayname($city));
}
echo "</select>\n";
echo "<input type=\"submit\" value=\"Show\">\n";
}
echo "</select>\n";
echo "<input type=\"submit\" value=\"Show\">\n";
<?php
if (isset($_POST['show_add_contact'])) {
<?php
if (isset($_POST['show_add_contact'])) {
+ set_last_selected("city_id", $_POST['city_id']);
$city_id = $_POST['city_id'];
show_new_contact_form($city_id);
}
else if (isset($_POST['add_contact'])) {
$city_id = $_POST['city_id'];
show_new_contact_form($city_id);
}
else if (isset($_POST['add_contact'])) {
+ set_last_selected("area_id", $area_id);
$id = add_contact($displayname);
if ($id !== false) {
echo "<p>Added contact.</p>\n";
$id = add_contact($displayname);
if ($id !== false) {
echo "<p>Added contact.</p>\n";
exit;
}
else if ($_POST['show_in_area']) {
exit;
}
else if ($_POST['show_in_area']) {
+ set_last_selected("area_id", $_POST['area_id']);
$q = new AreaQuery;
$area = $q->findOneById($_POST['area_id']);
header(sprintf("Location: http%s://%s/%s/in/area/%s/%d", ($_SERVER['HTTPS']) ? "s" : "", $_SERVER['HTTP_HOST'], $module, urlencode($area->getName()), $_POST['area_id']));
exit;
}
else if ($_POST['show_in_city']) {
$q = new AreaQuery;
$area = $q->findOneById($_POST['area_id']);
header(sprintf("Location: http%s://%s/%s/in/area/%s/%d", ($_SERVER['HTTPS']) ? "s" : "", $_SERVER['HTTP_HOST'], $module, urlencode($area->getName()), $_POST['area_id']));
exit;
}
else if ($_POST['show_in_city']) {
+ set_last_selected("city_id", $_POST['city_id']);
$q = new CityQuery;
$city = $q->findOneById($_POST['city_id']);
header(sprintf("Location: http%s://%s/%s/in/city/%s/%d", ($_SERVER['HTTPS']) ? "s" : "", $_SERVER['HTTP_HOST'], $module, urlencode($city->getName()), $_POST['city_id']));
$q = new CityQuery;
$city = $q->findOneById($_POST['city_id']);
header(sprintf("Location: http%s://%s/%s/in/city/%s/%d", ($_SERVER['HTTPS']) ? "s" : "", $_SERVER['HTTP_HOST'], $module, urlencode($city->getName()), $_POST['city_id']));
<?php
if (isset($_POST['show_add_donation'])) {
<?php
if (isset($_POST['show_add_donation'])) {
+ set_last_selected("area_id", $_POST['area_id']);
$area_id = $_POST['area_id'];
show_new_donation_form($area_id);
}
else if (isset($_POST['add_donation'])) {
$area_id = $_POST['area_id'];
show_new_donation_form($area_id);
}
else if (isset($_POST['add_donation'])) {
+ set_last_selected("area_id", $_POST['area_id']);
$id = add_donation();
if ($id !== false) {
echo "<p>Donation recorded.</p>\n";
$id = add_donation();
if ($id !== false) {
echo "<p>Donation recorded.</p>\n";
}
}
else if ($_POST['show_in_area']) {
}
}
else if ($_POST['show_in_area']) {
+ set_last_selected("area_id", $_POST['area_id']);
$q = new AreaQuery;
$area = $q->findOneById($_POST['area_id']);
/* XXX: Function to build URL because we need to set a class in links. */
$q = new AreaQuery;
$area = $q->findOneById($_POST['area_id']);
/* XXX: Function to build URL because we need to set a class in links. */
exit;
}
else if ($_POST['show_in_city']) {
exit;
}
else if ($_POST['show_in_city']) {
+ set_last_selected("city_id", $_POST['city_id']);
$q = new CityQuery;
$city = $q->findOneById($_POST['city_id']);
header(sprintf("Location: http%s://%s/%s/in/city/%s/%d", ($_SERVER['HTTPS']) ? "s" : "", $_SERVER['HTTP_HOST'], $module, urlencode($city->getName()), $_POST['city_id']));
$q = new CityQuery;
$city = $q->findOneById($_POST['city_id']);
header(sprintf("Location: http%s://%s/%s/in/city/%s/%d", ($_SERVER['HTTPS']) ? "s" : "", $_SERVER['HTTP_HOST'], $module, urlencode($city->getName()), $_POST['city_id']));
function option($select, $value, $text, $selected = null) {
echo " <option value=\"$value\"";
if (! isset($selected)) $selected = $_POST[$select];
function option($select, $value, $text, $selected = null) {
echo " <option value=\"$value\"";
if (! isset($selected)) $selected = $_POST[$select];
+ if (! isset($selected)) $selected = get_last_selected($select);
if ($value == $selected) echo " selected";
echo ">$text\n";
}
if ($value == $selected) echo " selected";
echo ">$text\n";
}
<?php
if (isset($_POST['show_add_hub'])) {
<?php
if (isset($_POST['show_add_hub'])) {
+ set_last_selected("city_id", $_POST['city_id']);
$city_id = $_POST['city_id'];
show_new_hub_form($city_id);
}
else if (isset($_POST['add_hub'])) {
$city_id = $_POST['city_id'];
show_new_hub_form($city_id);
}
else if (isset($_POST['add_hub'])) {
+ set_last_selected("area_id", $_POST['area_id']);
$id = add_hub($displayname);
if ($id !== false) {
echo "<p>Added hub.</p>\n";
$id = add_hub($displayname);
if ($id !== false) {
echo "<p>Added hub.</p>\n";
}
}
else if ($_POST['show_in_area']) {
}
}
else if ($_POST['show_in_area']) {
+ set_last_selected("area_id", $_POST['area_id']);
$q = new AreaQuery;
$area = $q->findOneById($_POST['area_id']);
header(sprintf("Location: http%s://%s/%s/in/area/%s/%d", ($_SERVER['HTTPS']) ? "s" : "", $_SERVER['HTTP_HOST'], $module, urlencode($area->getName()), $_POST['area_id']));
exit;
}
else if ($_POST['show_in_city']) {
$q = new AreaQuery;
$area = $q->findOneById($_POST['area_id']);
header(sprintf("Location: http%s://%s/%s/in/area/%s/%d", ($_SERVER['HTTPS']) ? "s" : "", $_SERVER['HTTP_HOST'], $module, urlencode($area->getName()), $_POST['area_id']));
exit;
}
else if ($_POST['show_in_city']) {
+ set_last_selected("city_id", $_POST['city_id']);
$q = new CityQuery;
$city = $q->findOneById($_POST['city_id']);
header(sprintf("Location: http%s://%s/%s/in/city/%s/%d", ($_SERVER['HTTPS']) ? "s" : "", $_SERVER['HTTP_HOST'], $module, urlencode($city->getName()), $_POST['city_id']));
$q = new CityQuery;
$city = $q->findOneById($_POST['city_id']);
header(sprintf("Location: http%s://%s/%s/in/city/%s/%d", ($_SERVER['HTTPS']) ? "s" : "", $_SERVER['HTTP_HOST'], $module, urlencode($city->getName()), $_POST['city_id']));
<?php
if (isset($_POST['show_add_order'])) {
<?php
if (isset($_POST['show_add_order'])) {
+ set_last_selected("area_id", $_POST['area_id']);
$area_id = $_POST['area_id'];
show_new_order_form($area_id);
}
else if (isset($_POST['add_order'])) {
$area_id = $_POST['area_id'];
show_new_order_form($area_id);
}
else if (isset($_POST['add_order'])) {
+ set_last_selected("area_id", $_POST['area_id']);
$id = add_order();
if ($id !== false) {
echo "<p>Order placed.</p>\n";
$id = add_order();
if ($id !== false) {
echo "<p>Order placed.</p>\n";
}
}
else if ($_POST['show_in_area']) {
}
}
else if ($_POST['show_in_area']) {
+ set_last_selected("area_id", $_POST['area_id']);
$q = new AreaQuery;
$area = $q->findOneById($_POST['area_id']);
header(sprintf("Location: http%s://%s/%s/in/area/%s/%d%s", ($_SERVER['HTTPS']) ? "s" : "", $_SERVER['HTTP_HOST'], $module, urlencode($area->getName()), $_POST['area_id'], get_order_state_query_uri(get_order_state_mask())));
exit;
}
else if ($_POST['show_in_city']) {
$q = new AreaQuery;
$area = $q->findOneById($_POST['area_id']);
header(sprintf("Location: http%s://%s/%s/in/area/%s/%d%s", ($_SERVER['HTTPS']) ? "s" : "", $_SERVER['HTTP_HOST'], $module, urlencode($area->getName()), $_POST['area_id'], get_order_state_query_uri(get_order_state_mask())));
exit;
}
else if ($_POST['show_in_city']) {
+ set_last_selected("city_id", $_POST['city_id']);
$q = new CityQuery;
$city = $q->findOneById($_POST['city_id']);
header(sprintf("Location: http%s://%s/%s/in/city/%s/%d%s", ($_SERVER['HTTPS']) ? "s" : "", $_SERVER['HTTP_HOST'], $module, urlencode($city->getName()), $_POST['city_id'], get_order_state_query_uri(get_order_state_mask())));
$q = new CityQuery;
$city = $q->findOneById($_POST['city_id']);
header(sprintf("Location: http%s://%s/%s/in/city/%s/%d%s", ($_SERVER['HTTPS']) ? "s" : "", $_SERVER['HTTP_HOST'], $module, urlencode($city->getName()), $_POST['city_id'], get_order_state_query_uri(get_order_state_mask())));
+ /* Retrieve the last selected choice in a select field. */
+ function get_last_selected($name) {
+ $key = "last_$name";
+ $ret = $_SESSION[$key];
+ error_log("last selected $name is $ret");
+ return $_SESSION[$key];
+ }
+
+ /* Remember the last selected choice in a select field. */
+ function set_last_selected($name, $value) {
+ if (isset($value)) error_log("setting last selected $name to $value");
+ else error_log("not setting last selected $name null");
+ if (! isset($value)) return;
+ $key = "last_$name";
+ $_SESSION[$key] = $value;
+ }
+