Forums

Pre-Select dropdown field according to URL

OlliMuc 27 May, 2016
Hi there,

I would like to design a ChronoForm with one dropdown field where I'd like to pre-select the right entry according to the URL of the page the form is displayed on.

GreyHead already suggested a great solution whenever the field is a post-fix to the URL, such as in website.com/form.hrml?field=value123 . No need to do anything here ...

What I'm looking for is a little more complex. I'd like to scan the URL for keywords, and the adjust the dropdown accordingly.

I wrote some code for a previous version of CF, and it worked quite well - although I'm not sure as to incorporate it in CF5. It goes like:

<?php
$topic = "novalue";

$topiclist = array ("apple", "carrot");
$chose = ''; $counter = 0;
$urli = $_SERVER['REQUEST_URI'];;

foreach ($topiclist AS $check)
   {
$counter = 0;
  $counter = substr_count($urli,$check);
if ($counter != 0) {$this = $check;}
}
if ($this == "apple") {$topic = "fruit";}
else if ($this == "carrot") {$topic = "vegetable";}
?>


Now, what the code does: It scans the URL for keywords, and if found, returns the appropriate value for $topic.

For website.com/carrotsales.html, it would return "vegetable" for $topic.
For website.com/wedontlikenoapples.html, it would return "fruit" for $topic.

I've tried to include the code in the Designer in a custom field, and just made sure I also used "topic" as a field name for the drop-down. Didn't work. Any suggestions on this one?
GreyHead 29 May, 2016
Answer
1 Likes
Hi OlliMuc,

You need to add the result to the $form->data[''] array as e.g.
$form->data['topic'] = 'vegetable';
And make sure that your Custom Code action is before the HTML (Render form) action.

Bob
OlliMuc 16 Jun, 2016
Thank you so much, Bob - worked like a charm !!!
This topic is locked and no more replies can be posted.