Value for 'selected' in form wizard (drop down) from a php variable?

Set a default selected value in a CF dropdown from a PHP variable.

Overview

The issue occurs when trying to pre-select an option in a CF dropdown using a PHP variable, as direct PHP code in the form wizard's 'Selected Values' field is not processed.
Add a Custom Code action before the form's HTML action to assign the variable value to the form's data array using the correct Joomla input method for your version.

Answered
to tondorius03 12 Feb, 2014
Hi,
i wrote a interface between chronoform and sobi pro. It works fine but for drop down lists one problem occurs.
I have a php string variable (extern script) which holds the selected option value from a sobi pro drop down list called $selected_value. I want to place the value of this variable in the selected-field in the form wizard so that the option value ist automatically selected in chronoform.

Variable from sobi pro:
$selected_value="blue";

Drop down list in chronoform:
blue="blue";
yellow="yellow;"

-> blue must be automatically selected

I can't write directly php code in the selected field in the form wizard for example:
Selected: <?php echo "$selected_value"; ?>

Is there a way to solve this problem?

thanks & regards!!
JM
Gr GreyHead 12 Feb, 2014
Answer
Hi JM,

If you add it to the $form->data array then the ChronoForms republisher should do what you need.
<?php
$form->data['input_name'] = $your_variable;
?>
to tondorius03 12 Feb, 2014
You are great!
I bought a "beer-ticket" on your website;-) Cheers!
JM
an andrea74 16 Apr, 2014
Hi,
I'm using CF V5 and I have the same problem, but I don't understand how to use the array trick.
In the "Option" field I put:

GENERAL=General Information
TANUR=Tanur
REF=REF


and in "Selected Values" field:

<?php echo $_GET["product"]; ?>


but doesn't work. (if I stamp the variable "product" I can correctly see the value).
Could you help me please?
Thank you in advance.
Max_admin Max_admin 16 Apr, 2014
Hi Andrea,

what's the name of your select field ? assuming its "product" then simply add a "custom code" action before the HTML action and use Bob's code!

Regards,
Max
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
an andrea74 17 Apr, 2014
Dear Max,
if "info_on" is the name of the select field and "product" the name of variable, I put in Setup->JavaScript->JS Code the string
<?php
$form->data['info_on'] = $product;
?>

without success.
Is it necessary to put something in "Selected Values" field of the select (Edit element settings window)?
Thank you for the support.
an andrea74 17 Apr, 2014
I also try to move the code
<?php
$form->data['info_on'] = $product;
?>

in a Custom Code field (Designer->Advanced), but it still does not work.
Gr GreyHead 17 Apr, 2014
Hi Andrea,

The value of $product needs to be set - it won't magically appear. Using Joomla! 2.5+ the code will need to be something like:
<?php
$jinput = JFactory::getApplication()->input;
$product = $jinput->get('product', '', 'STRING');
$form->data['info_on'] = $product;
?>

Bob
an andrea74 17 Apr, 2014
Thank you very much Bob, I could not have solved without your help.
This topic is locked and no more replies can be posted.