kentster VIP
Total posts: 54
09 Фев 2014 18:00

I'm new to Cobalt and didn't see this addressed anywhere...

Is it possible to capture a variable from the URL and load this into a field? ex: www.website.com?referral=indeed... I want to capture the referral source "indeed" and store it into the database.

Kent

Последние изменения: 02 Март 2014


Sergey
Total posts: 13,748
10 Фев 2014 02:17

At the same time you want this field to be not editable in the form right?


kentster VIP
Total posts: 54
10 Фев 2014 07:35

Yes, correct--no edits. In another form product, I simply created a hidden field and used a little PHP to insert it.


Sergey
Total posts: 13,748
11 Фев 2014 07:26

You can do this like this

  1. Add text field

  2. Create copy of input template of the field

  3. In the add readonly attribute and default value from URL.

SOmething like this.


kentster VIP
Total posts: 54
13 Фев 2014 11:52

Very cool! As a PHP novice, it took me a little to sort through this, but it worked great. Very nice demonstration of the power of the template... Thanks so much!


kentster VIP
Total posts: 54
13 Фев 2014 12:03

Here is what I did for anyone else who may need this.

  1. Copied the text input template to /home/mysite/public_html/components/com_cobalt/fields/text/tmpl/input/url-variable.php

  2. In Edit Field, I changed the "Template on the form" to the above template.

  3. Edited the template as Sergey recommended. Code below.


<?php /** * Cobalt by MintJoomla * a component for Joomla! 1.7 - 2.5 CMS ( http://www.joomla.org ) * Author Website: http://www.mintjoomla.com/ * @copyright Copyright (C) 2012 MintJoomla ( http://www.mintjoomla.com ). All rights reserved. * @license GNU/GPL http://www.gnu.org/copyleft/gpl.html */ defined('_JEXEC') or die(); if (!empty($_GET["referral"]))# GRAB THE URL VARIABLE { $referral = $_GET["referral"]; } else { $referral = "TeamLMI"; } $class[] = $this->params->get('core.field_class', 'inputbox'); $required = NULL; if ($this->required) { $class[] = 'required'; $required = ' required="true" '; } $class = ' class="' . implode(' ', $class) . '"'; $size = $this->params->get('params.size') ? ' style="width:' . $this->params->get('params.size') . '"' : ''; $maxLength = $this->params->get('params.maxlength') ? ' maxlength="' . (int)$this->params->get('params.maxlength') . '"' : ''; $readonly = ' readonly="readonly"'; #FORCE IT TO READONLY $disabled = ((string)$this->params->get('disabled') == 'true') ? ' disabled="disabled"' : ''; $onchange = $this->params->get('onchange') ? ' onchange="' . (string)$this->params->get('onchange') . '"' : ''; $mask = $this->params->get('params.mask', 0); ?> <?php echo $this->params->get('params.prepend');?> # SET INPUT TO REFERRAL VALUE <input type="text" placeholder="<?php echo $this->params->get('params.mask.mask') ?>" name="jform[fields][<?php echo $this->id;?>]" id="field_<?php echo $this->id;?>" value="<?php echo $referral;?>" <?php echo $class . $size . $disabled . $readonly . $onchange . $maxLength . $required;?>> <?php echo $this->params->get('params.append');?> <?php if ($mask->mask_type) :?> <script type="text/javascript"> initMask(<?php echo $this->id;?>, "<?php echo $mask->mask;?>", "<?php echo $this->mask_type;?>"); </script> <?php endif; ?>

Sergey
Total posts: 13,748
14 Фев 2014 02:38

All your template could look like this

<?php

/**

 * Cobalt by MintJoomla

 * a component for Joomla! 1.7 - 2.5 CMS ( http://www.joomla.org )

 * Author Website:  http://www.mintjoomla.com/ 

 * @copyright Copyright (C) 2012 MintJoomla ( http://www.mintjoomla.com ). All rights reserved.

 * @license GNU/GPL  http://www.gnu.org/copyleft/gpl.html 

 */

defined('_JEXEC') or die();

$referral = JFactory::getApplication()->input->get('referral', 'TeamLMI');

$value = $this->value ? htmlspecialchars($this->value, ENT_COMPAT, 'UTF-8') : $referral;

?>

<input type="text" name="jform[fields][<?php echo $this->id;?>]" readonly="readonly" value="<?php echo $value;?>">

You need

$value = $this->value ? htmlspecialchars($this->value, ENT_COMPAT, 'UTF-8') : $referral;

Because when you edit, there is no referral in URL and value may be lost.


kentster VIP
Total posts: 54
15 Фев 2014 13:43

Sergey, works great and much more efficient. Thanks!

Работает на Cobalt