Forums

Populate field with array

dw1 17 Feb, 2014
Have been trying unsuccessfully to prepopulate a form field with an array.

Have created an array from K2 content elsewhere on site. Looking to pass the array into predefined "Theme" form field within ChronoForms. User should select an item and once arriving to form, field "Theme" is filled in with user selection.

Have tried many different solutions. Unfortunately, form field remains empty.

PHP Code to retrieve K2 Title (Works to populate array):
<?php
$K2Itemid = JRequest::getInt('id');
if($K2Itemid)
{
$db = JFactory::getDBO();
$db->setQuery("SELECT title FROM #__k2_items WHERE id = ".$K2Itemid );
$K2Title = $db->loadResult(); 
}
?>



Screen shots:
1. Show arrays in Custom Code action. Array "[theme] = Generic" is what I need in form field "Theme".
Using this php code in Custom Code action to see arrays at top of form page:
<?php 
 session_start(); 
 Print_r ($_SESSION);
 ?> 

[attachment=0]session.png[/attachment]

2. Field Theme
[attachment=0]theme-field.jpg[/attachment]


Form field code:
   <div class="ccms_form_element cfdiv_text">
        <label for="theme">Theme</label>
<input type="text" name="title" id="K2Title" class="inputbox"
value="<?php echo $K2Title; ?>" />
        <div class="clear"></div>
      </div>


Suggestions welcome.
Thanks in advance.

Dave
dw1 17 Feb, 2014
Added the Debugger to Events in Wizard.
Not seeing any information in the ChronoForms array. Confusing since I am able to Print Session and clearly see the "[theme] => Generic" array that I am needing to populate in form field above the ChronoForms Array info.

Data Array: 

Array
(
)
Validation Errors: 
Array
(
)
GreyHead 19 Feb, 2014
Hi dw1,

Please try this in a Custom Code action in the On Load event of your form:
<?php
$K2Itemid = JRequest::getInt('id','' );
$form->data['title'] = '';
if ( $K2Itemid ) {
  $db = JFactory::getDBO();
  $query = "
    SELECT `title`
      FROM `#__k2_items`
       WHERE `id` = {$K2Itemid}
  ";
  $db->setQuery($query);
  $form->data['title'] = $db->loadResult(); 
}
?>
The main difference from your code is that I'm adding the result to the $form->data array where ChronoForms will be able to find it and set the value of your input,

Bob
dw1 20 Feb, 2014
Thanks Bob.

Tried your suggestion and quite a few other options and not successful.
Correct me if I am wrong, but isn't the code to retrieve K2Title necessary on the preceding page prior to a user arriving at the final check out form. The K2 Title sample I am using is "Generic". If so, I had included the K2 Title retrieve code on the K2 item.php file so I can create a variable or array called 'title'. That seems to work okay. From that page I've printed the session and can see the "[title] => Generic".

Next step is to pass 'title' to ChronoForms page. It thought I can do that via href with code: href="pricing/signup?'title'". Not working.

Tried loading the Custom Code action in the On Load event $form->data['title'] and see nothing in the array however do see it populated in the Session.
GreyHead 23 Feb, 2014
Hi dw1,

Sorry but I can't work out form your posts what is happening where :-(

If you are linking from K2 then you need to include either the id - when the code I had should work, or the title, in which case that can be used directly.

How are you connecting from K2 to ChronoForms?

Bob
dw1 12 Mar, 2014
Hi Bob. Had to take a breather on other client projects. Just circling back to this. Thank you again for your assistance. It is greatly appreciated.

When you ask how am I connecting, not sure I understand. I thought I can pull the Title of a K2 article. Place the title in an array. Move to Chronoforms page and deposit array into field with K2 Article Title. So far, everything mentioned here is not working.

In my specific case, we develop web themes. Need prospective new clients to choose theme by selecting a button "Use This Theme". Theme name is the same as the K2 Article Title. Once that happens, new Chronoforms page appears with Theme name appearing in the Theme name field automatically.
GreyHead 26 Mar, 2014
HI dw1,

I'm still not clear where the problem is. Is your "Use This Theme" button a link or a form submit button? I'm guessing that it is a link - in that case the link URL needs to include an identifier for the theme. It could be the theme name (urlencoded if necessary) or, probably better, a theme identifier.

The in the ChronoForm you can access the name or identifier and look up the other necessary information.

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