Forums

Open A form and pass a value to "Field Value"

cirymarr 09 Aug, 2016
Hi

Scenario - one simple form with name / phone / email / subject / message fields. I would like to be able to (somehow) pass a value to the "Field Value" of the "subject" text field.

In simple terms, I have a Joomla category of approx 20 artists, and would like to have a "Contact us for more info about this artist" on each artist's page, that would then open the Chronoform (as above) with the subject field pre-populated with (for example): "Artist - Joe Blow"

Is this possible?
natloz 09 Aug, 2016
I do this on forms with this method: (NOTE: I set the field to READ-ONLY in the designer as it is just a reference).

In the Setup tab of the form....
In the On Load event...
Add a Custom Code element....(eg. GetArtistName)
Using PHP, and something like the following code (query from mysql).

<?php
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select($db->quoteName('problemTitle')) ->from($db->quoteName('#__cf_Problem')) ->where($db->quoteName('cf_id')." = ".$form->data['PID']);
$db->setQuery($query);
$results = $db->loadObjectList();
foreach($results as $result) {
  $form->data['problemTitle'] = $result->problemTitle;
}
?>
GreyHead 10 Aug, 2016
Hi cirymarr,

The simplest way is to add the subject to the link e.g. &artist=Artist%20-%20Joe%20Blow (this is URL encoded to preserve the spaces).

Then use a Custom Code action in the form On Load event to undo the URL encoding:
<?php
$form->data['artist'] = url_decode($form->data['artist']);
?>
and add a Hidden input with the name artist in the Designer tab,

Another way is to pass an artist ID as e.g. &artist=99 and then look up the artist data from the categories table in the form On Load event.

Bob

[[>> later : added missing ) <<]]
mmaack 04 Oct, 2016
At first the custom PHP code was blowing up my form, but then I noticed I needed to close the urldecode line with an end parentheses.

Just about exactly what I've been looking for! I have a similar challenge I'm addressing. Now to pass my variable to a destination URL!

Mark
mmaack 04 Oct, 2016
Next-challenge: I've successfully created a hidden field that I can pass a value to.

My client wants visitors to fill out a form prior to viewing the content. I want to re-use this form for multiple story links and use a variable to determine the destination re-direction after submittal of the form. I'm declaring the link value by using the menu item alias to the story as the hidden field. After completion, I wish to pass this alias field to the destination url i.e. http://somesite/(hidden variable)

In this case, my variable is 'story' and I wish to append this value to the url.

I've hit a bit of a roadblock...but I'm soooooo close! 😀
Mark
GreyHead 05 Oct, 2016
Hi Mark,

I'm not quite sure what you want to pass here. You can add a variable query string using the redirect action e.g. url=field_value

Or you can build a url in a Custom Code action and redirect from there.
<?php
$url = '. . .';
$app = \JFactory::getApplication();
$app->redirect($url);
?>

Bob

PS Apologies for the missing ) - now replaced.
mmaack 05 Oct, 2016
Thank you for the quick reply. The url code part works fine, but I'm having difficulty getting the variable to work when tagging it to the end of the url string.

I'm using the example code:
<?php
$form->data['story'] = url_decode($form->data['story']);
?>
in the on load section of the forrn and can pass that string to the hidden field in the form. However I can't seem to get that same value to work in the url string I pass to the redirect.

i.e. http://mysite.com/{story}
GreyHead 05 Oct, 2016
Hi mmaack,

Where exactly are you using http://mysite.com/{story} ? You can't use the {} syntax in PHP, use $form->data['story'] there.

Bob
GreyHead 05 Oct, 2016
Hi mmaack,

Where exactly are you using http://mysite.com/{story} ? You can't use the {} syntax in PHP, use $form->data['story'] there.

Bob
mmaack 05 Oct, 2016
I didn't think so, and I've been trying the following since I last posted. Obviously my syntax is wrong since I'm still getting errors, but here it is...

On Submit Custom Code
<?php
    $url = 'http://www.legasusgroup.com/($form->data['story'])';
    $app = \JFactory::getApplication();
   $app->redirect($url);
?>
GreyHead 05 Oct, 2016
1 Likes
Hi mmack.

Please try $url = 'http://www.legasusgroup.com/'.$form->data['story']; or $url = "http://www.legasusgroup.com/{$form->data['story']}"; either should work.

Or even $url = JURI::root().$form->data['story']; if this is on the same site.

Bob
mmaack 05 Oct, 2016
Life is easy when YOU"RE THE MAN!!!!!!

Your suggestion worked like a champ!

Many Thanks!
Mark
This topic is locked and no more replies can be posted.