Populate / pre select dropdown from URL

OlliMuc 18 Jul, 2015
Hi there!

I'd like to streamline the forms on my website and have been looking into different forms of fixing my forms.
Here's what I'd like to do: My form contains one dropdown called "City" and another one called "Service".

I would like to get CF to pre-select the right dropdown field from the URL of the page the form is on.
So, if it is "yaddiya-losangeles-yuckyuck.html" - the pre selected dropdown field under city should be Los Angeles.

I was able to do this in CF4, but it took a LOT of customization. It looked something like this:

<?php
$cities = array ("losangeles", "seattle");
$city = '"'; $counter = 0;
$urli = $_SERVER['REQUEST_URI'];

foreach ($cities AS $checkcity)
   {
$counter = 0;
  $counter = substr_count($urli,$checkcity);
if ($counter != 0) {$city = $checkcity;}
   }
?>

<select  id="select_0" size="1" title="" name="city">
 <option value="LosAngeles"<?php if ($city == "losangeles") {echo ' selected';}?>>Los Angeles</option>
<option value="Seattle"<?php if ($city == "seattle") {echo ' selected';}?>>Seattle</option>
<option value="sumthinelse">other city</option>


Now - is there any way to do this with a little less hassle? I'm really looking to learn how to get a hang of this and not walk on some crutches all the time ... Thanks to Bob, I do know about how to pass paremeters on via URL easily - but in this scenario (and especially with JoomSEF at work), it's not really the solution I'm looking for.

Any input greatly appreciated ...
GreyHead 18 Jul, 2015
Hi OlliMuc,

Maybe something like this:
<?php
$uri = JFactory::getURI();
$url_string = $uri->toString();
$url_array = explode('-'. $url_string);
$form->data['city'] = $url_array[1]; // check the correct element
?>

The set your drop-down up with options to match the URLs
losangeles=Los Angeles
sanfrancisco=San Francisco
newyork=NewYork

There are various ways this could be improved but the basics should be OK.

Bob
This topic is locked and no more replies can be posted.