K2 Form on Item page

EricPrin 17 Oct, 2016
Has anybody had any success with populating a form field with a K2 Extra Field value?

I have a , http://www.zululandreservations.co.za/ , site with popular tourist destinations listed with K2 with a Chronoform inserted via the plugin ( i.e. {chronoforms5}lodge-contact-captcha{/chronoforms5} ) but would like to pull in the establishment name into a field on the enquiry form.

I have tried following and implementing the tutorial in these forums and have had no success.

I would appreciate any advice or help.

Or

Is there also no way to attach a string value to a form link perhaps? Possibly I could have the form in a central location and pull in a string value for the establishment vial a hidden field into the form before sending?

Thank you for your assistance in advance. :-)
GreyHead 17 Oct, 2016
Hi EricPrin,

You can add a Parameter in the plug-in link {chronoforms5}lodge-contact-captcha&estab=xxx{/chronoforms5}

Or you can add a parameter to a link . . .&chronoform=lodge-contact-captcha&estab=xxx

Bob

PS It's also possible to hack the code to add a parameter to a menu item so that you can use menu-driven SEF URLs
EricPrin 17 Oct, 2016
Thank you for your prompt reply! :-)
In the example above would I have to change the field settings on the "Establishment" item in the form to add the value (this would be preferred so the information is saved with the email in the database) or add some code to the Emails under Setup to include the passed value?
EricPrin 17 Oct, 2016
Hi, I have found a work-around solution using your suggestion above.
I insert the Establishment name in the plugin for each lodge, see example below:
{chronoforms5}lodge-contact-captcha&establishment=Amakhosi Private Game Lodge{/chronoforms5}
This pre-inserts the name into the form field.

However it would be great to have the K2 extra field for that lodge entered in the plugin string via PHP code. Any suggestions?

Thank you once again for you help so far. We are making progress! :-)
EricPrin 17 Oct, 2016
:-(
The code stopped working??
When I enter the plugin code with the extension is fails to open the form now?
Any suggestions please?
{chronoforms5}lodge-contact-captcha&establishment=Amakhosi Private Game Lodge{/chronoforms5}
EricPrin 17 Oct, 2016
says:
Form not found or is not published

But if I take out the extension it works?
GreyHead 17 Oct, 2016
Hi EricPrin,

Possibly there is a problem with the spaces in the name - does it work if the name has no spaces? You could also try URL encoding the name and see if that works.

I have no idea what a K2 extra field is but you could probably use JavaScript to find the value and set it in the form input.

Bob
EricPrin 18 Oct, 2016
Tank you, I found the gremlin...

My Text editor kept changing the & to & !

Busy experimenting now to insert the form via the item.php file in K2.

In Joomla 3.x and K2 the following code will display the exrta field:

Place this near the top:
<!-- START: Call to prepare extra fields -->
<?php
//convertArray to use ids as key
$extrafields = array();
foreach($this->item->extra_fields as $item)
{
$extrafields[$item->id] = $item->value;
}
?>
<!-- END: Call to prepare extra fields -->

Insert this where you want it displayed.
<?php if(isset($extrafields[1]) === true):?>
Any text or html - <?php echo $extrafields[1];?>
<?php endif; ?>
where [1] is the extra field id of the value wishing to be displayed.

Found this and thanks here..
http://jbeginner.com/tutorials/extensions/how-to-display-k2-extra-fields/
EricPrin 18 Oct, 2016
Making Progress but I get the plugin code displayed instead of the form?
If I interrogate the HTML on the page I see it as
" {chronoforms5}lodge-contact-captcha&establishment=<?php echo $extrafields[1];?>{/chronoforms5}
"
With the quotes inserted. Possibly this is why the form is not loading?
EricPrin 18 Oct, 2016
sorry my bad the php is not pulling in the extra field either.

Show on the page item text as :
{chronoforms5}lodge-contact-captcha&establishment={/chronoforms5}
GreyHead 18 Oct, 2016
Hi EricPrin,

You can't add PHP to an article - it will do nothing except break the code. You have to use JavaScript for this. You need a script that will run on page load - identify the value of the K2 field and add that as the value of a hidden input in your form.

Bob
EricPrin 18 Oct, 2016
OK the saga continues.
I have progressed to have the full plugin code display in the item text with the K2 extra field. However the form does not display?
It just shows the plugin code?

Any suggestions?
:-)
GreyHead 18 Oct, 2016
Hi EricPrin,

Nope, how are you doing that? Please post a link to the form page so I can take a quick look.

Bob
EricPrin 18 Oct, 2016
Thank you so much for you help.
I believe we are close.
http://www.zululandreservations.co.za/where-to-stay/private-game-lodges/phinda-private-game-reserve/item/16-forest-lodge
I have changed the code to allow K2 Extra fields to be displayed in the K2 item. This now shows the extra field correctly as part of the plugin string. - {chronoforms5}lodge-contact-captcha&establishment=Forest Lodge{/chronoforms5}
I have kept the usual plugin string as part of the item content text and this is displaying the form correctly so I know the form plugin is working as per your suggested solution previously.
I have added the following code to the full text section of the page template:-
<?php
$str = "{chronoforms5}lodge-contact-captcha&establishment=";
$str2 = "{/chronoforms5}";
echo $str .$extrafields[1] .$str2;

?>
(Excuse the clunky code but am not a pro :-) )

As you can see the form displays from the usual text insert but only displays the plugin code for the latter, it displays this directly below the form. I have tried removing the normal plugin code to see if it was preventing the form displaying twice but this had no effect.
GreyHead 18 Oct, 2016
Hi EricPrin,

Where on the page can i see the K2 extra field? - I can see 'Forest Lodge' many times but none of them look like a K2 extra field to me.

I suspect that your PHP doesn't work because it runs after the plug-in replacement has happened.

In this case one way to do what you need would be to grab the page Title, that is probably available when the form loads. Please see this FAQ

Bob
GreyHead 19 Oct, 2016
Hi EricPrin,

Sorry, but it isn't explaining itself to me :-(

As I said earlier - I suspect that the PHP code you are using to create the second link is running after the plug-in content has been added to the page so it doesn't work.

If you have the value of the K2 Extra field somewhere in the page then you can probably copy it using JavaScript - see the FAQ I linked to for getting this from the page title.

Bob

PS I still have no idea what a K2 Extra Field is or does so may be missing something here.
This topic is locked and no more replies can be posted.