Gravity Form Form Name Attribute - php

The Page Analytics I use gathers the form data automatically. Gravity Forms create tag without any name attribute. I am trying to figure out a way to assign form tag a name attribute.
The closest I have reached is this example:
<?php
add_filter("gform_form_tag", "form_tag", 10, 2);
function form_tag($form_tag, $form){
if ($form["id"] == 3){
$form_tag = preg_replace("|action='(.*?)'|", "action='custom_handler.php'", $form_tag);
return $form_tag;
}
}
?>
But I am not sure how to use preg_replace to create a name attribute for form in question.

I figured out a solution to suite myself. Sharing it for any other troubled soul. for me all I wanted was to add a name attribute for the forms so that my analytics picks up something understandable instead of ids like form-1234 so I browsed plugin/gravityforms/forms_display.php and edited it where it creates a new form tag. can be found between line 435 to 440. created a new variable to hold value of form title, edited it to remove spaces. and inserted it to the form tag string.
//Edited For Analytics
$cm_form_name = str_replace(" ", "-", $form['title']);
$form_string .= apply_filters("gform_form_tag_{$form_id}", apply_filters("gform_form_tag", "<form method='post' enctype='multipart/form-data' {$target} id='gform_{$form_id}' name='{$cm_form_name}' {$form_css_class} action='{$action}'>", $form), $form);
//End Editing
//Orginal String
//$form_string .= apply_filters("gform_form_tag_{$form_id}", apply_filters("gform_form_tag", "<form method='post' enctype='multipart/form-data' {$target} id='gform_{$form_id}' {$form_css_class} action='{$action}'>", $form), $form);

I wrote a plugin that also makes adding a name attribute (or modifying/adding/removing any other attribute for the form tag) a breeze.
http://gravitywiz.com/gravity-forms-tag-editor/
Here's an example for adding the name attribute:
new GW_Tag_Editor( array(
'tag' => 'form',
'name' => 'form_{formId}',
) );

I had a slightly different goal, but thanks to Jeff's idea I came up with the following approach that I think is worth sharing.
It appends a sanitized form name data attribute to the form tag, without touching any other attributes.
function add_form_name_data_attr($form_tag, $form){
$form_tag = str_replace('>', ' data-form-name="' . sanitize_title($form['title']) . '">', $form_tag);
return $form_tag;
}
add_filter('gform_form_tag', 'add_form_name_data_attr', 10, 2);
I can then use that data attribute in JavaScript to differentiate the various forms when calling a certain analytics API.

Related

using if/else statement with str_replace to add variables via shortcode

I'm working with a custom shortcode that generates a custom paypal button & passes specific parameters to a paypal checkout form.
I'm using the str_replace function to pass attributes defined in the shortcode to the html form.
I'd like to set a default style class & default title if not defined as an attribute in the shortcode.
My php:
function paypal_button_func($attrs){
$class=$attrs['class']; //Added button style class variable
$title=$attrs['title']; //Added button title variable
if( isset( $atts['title'])) //add button title to the form html
{
return $html=str_replace('[title]',$title,$html);
}
//This sets the default title if not defined
else {
return $html=str_replace ('[title]','SIGN UP NOW',$html);
}
And this is my html
<input type="submit" class="[class]" value="[title]" /> <!-- updated class and value for addtl shortcode parameters -->
I was successful with using the following code to pass the shortcode attributes to the html, but the problem is that if the attributes aren't defined in the shortcode it outputs the [class] and [title] as values, which is not what i want to happen.
$html=str_replace('[title]',$title,$html); //add button title assigned to the form
$html=str_replace('[class]',$class,$html); //replaces class assigned to the form
simply make sure to initialize your replacements properly, i suggest using an if/else shorthand when creating your vars:
$class = isset($attrs['class']) ? $attrs['class'] : 'default-class-name'; //Added button style class variable
$title = isset($attrs['title']) ? $attrs['title'] : 'SIGN UP NOW'; //Added button title variable
this way your html will always get a replacement, either the attrs value or the defaults.
also i noticed you have a typo checking on isset($atts['title']) where you probably want $attrs and not $atts
good luck!

Yii Custom Form Field Id Ajax Validation CActiveForm

I'm making a system that stores Yii Forms in a database.
I'm having troubles getting one thing to work.
I have several Forms on the same page which are all called DynamicFormModel. To achieve validation on each field I had to use the 'id' and 'inputID' HtmlOptions on my fields and error message dom elements.
$domId = 'form_' . $dynamicFormModel->formModel->id . 'language_' . $dynamicFormModel->language->id . 'field_' . $formField->id;
echo $form->textArea($dynamicFormModel, $formField->label, array('id' => $domId));
echo $form->error($dynamicFormModel, $formField->label, array('inputID' => $domId));
Client side validation works fine. The problem I'm having is with Ajax Validation. The client side validation now makes the needed events relative to the needed field ID.
/*<![CDATA[*/
jQuery(function($) {
jQuery('#AppVersionDetailsForm').yiiactiveform({'attributes':[{'id':'AppVersionDetailsForm_defaultLanguage','inputID':'AppVersionDetailsForm_defaultLanguage','errorID':'AppVersionDetailsForm_defaultLanguage_em_','model':'AppVersionDetailsForm','name':'AppVersionDetailsForm[defaultLanguage]','enableAjaxValidation':true,'inputContainer':'div.control-group','summary':true},{'id':'AppVersionDetailsForm_selectedLanguages','inputID':'AppVersionDetailsForm_selectedLanguages','errorID':'AppVersionDetailsForm_selectedLanguages_em_','model':'AppVersionDetailsForm','name':'AppVersionDetailsForm[selectedLanguages]','enableAjaxValidation':true,'inputContainer':'div.control-group'}],'summaryID':'AppVersionDetailsForm_es_','errorCss':'error'});
jQuery('#form_1language_1').yiiactiveform({'validateOnType':true,'validateOnChange':true,'attributes':[{'id':'DynamicFormModel_Test','inputID':'form_1language_1field_1','errorID':'form_1language_1field_1_em_','model':'DynamicFormModel','name':'DynamicFormModel[Test]','enableAjaxValidation':false,'inputContainer':'div.control-group'},{'id':'DynamicFormModel_Radio','inputID':'form_1language_1field_2','errorID':'form_1language_1field_2_em_','model':'DynamicFormModel','name':'DynamicFormModel[Radio]','enableAjaxValidation':false,'inputContainer':'div.control-group'}],'errorCss':'error'});
jQuery('#form_2language_1').yiiactiveform({'validateOnType':true,'validateOnChange':true,'validationUrl':'/index.php/appVersion/submitDynamicForm','attributes':[{'id':'DynamicFormModel_Test','inputID':'form_2language_1field_3','errorID':'form_2language_1field_3_em_','model':'DynamicFormModel','name':'DynamicFormModel[Test]','enableAjaxValidation':true,'inputContainer':'div.control-group'},{'id':'DynamicFormModel_test_radio','inputID':'form_2language_1field_4','errorID':'form_2language_1field_4_em_','model':'DynamicFormModel','name':'DynamicFormModel[test radio]','enableAjaxValidation':true,'inputContainer':'div.control-group'},{'id':'DynamicFormModel_Dropdown','inputID':'form_2language_1field_5','errorID':'form_2language_1field_5_em_','model':'DynamicFormModel','name':'DynamicFormModel[Dropdown]','enableAjaxValidation':true,'inputContainer':'div.control-group'},{'id':'DynamicFormModel_Check_this','inputID':'form_2language_1field_6','errorID':'form_2language_1field_6_em_','model':'DynamicFormModel','name':'DynamicFormModel[Check this]','enableAjaxValidation':true,'inputContainer':'div.control-group'},{'id':'DynamicFormModel_Upload_a_file','inputID':'form_2language_1field_7','errorID':'form_2language_1field_7_em_','model':'DynamicFormModel','name':'DynamicFormModel[Upload a file]','enableAjaxValidation':true,'inputContainer':'div.control-group'},{'id':'DynamicFormModel_Upload_an_image','inputID':'form_2language_1field_8','errorID':'form_2language_1field_8_em_','model':'DynamicFormModel','name':'DynamicFormModel[Upload an image]','enableAjaxValidation':true,'inputContainer':'div.control-group'}],'errorCss':'error'});
jQuery('#form_2language_3').yiiactiveform({'validateOnType':true,'validateOnChange':true,'validationUrl':'/index.php/appVersion/submitDynamicForm','attributes':[{'id':'DynamicFormModel_Test','inputID':'form_2language_3field_3','errorID':'form_2language_3field_3_em_','model':'DynamicFormModel','name':'DynamicFormModel[Test]','enableAjaxValidation':true,'inputContainer':'div.control-group'},{'id':'DynamicFormModel_test_radio','inputID':'form_2language_3field_4','errorID':'form_2language_3field_4_em_','model':'DynamicFormModel','name':'DynamicFormModel[test radio]','enableAjaxValidation':true,'inputContainer':'div.control-group'},{'id':'DynamicFormModel_Dropdown','inputID':'form_2language_3field_5','errorID':'form_2language_3field_5_em_','model':'DynamicFormModel','name':'DynamicFormModel[Dropdown]','enableAjaxValidation':true,'inputContainer':'div.control-group'},{'id':'DynamicFormModel_Check_this','inputID':'form_2language_3field_6','errorID':'form_2language_3field_6_em_','model':'DynamicFormModel','name':'DynamicFormModel[Check this]','enableAjaxValidation':true,'inputContainer':'div.control-group'},{'id':'DynamicFormModel_Upload_a_file','inputID':'form_2language_3field_7','errorID':'form_2language_3field_7_em_','model':'DynamicFormModel','name':'DynamicFormModel[Upload a file]','enableAjaxValidation':true,'inputContainer':'div.control-group'},{'id':'DynamicFormModel_Upload_an_image','inputID':'form_2language_3field_8','errorID':'form_2language_3field_8_em_','model':'DynamicFormModel','name':'DynamicFormModel[Upload an image]','enableAjaxValidation':true,'inputContainer':'div.control-group'}],'errorCss':'error'});
jQuery('#form_2language_4').yiiactiveform({'validateOnType':true,'validateOnChange':true,'validationUrl':'/index.php/appVersion/submitDynamicForm','attributes':[{'id':'DynamicFormModel_Test','inputID':'form_2language_4field_3','errorID':'form_2language_4field_3_em_','model':'DynamicFormModel','name':'DynamicFormModel[Test]','enableAjaxValidation':true,'inputContainer':'div.control-group'},{'id':'DynamicFormModel_test_radio','inputID':'form_2language_4field_4','errorID':'form_2language_4field_4_em_','model':'DynamicFormModel','name':'DynamicFormModel[test radio]','enableAjaxValidation':true,'inputContainer':'div.control-group'},{'id':'DynamicFormModel_Dropdown','inputID':'form_2language_4field_5','errorID':'form_2language_4field_5_em_','model':'DynamicFormModel','name':'DynamicFormModel[Dropdown]','enableAjaxValidation':true,'inputContainer':'div.control-group'},{'id':'DynamicFormModel_Check_this','inputID':'form_2language_4field_6','errorID':'form_2language_4field_6_em_','model':'DynamicFormModel','name':'DynamicFormModel[Check this]','enableAjaxValidation':true,'inputContainer':'div.control-group'},{'id':'DynamicFormModel_Upload_a_file','inputID':'form_2language_4field_7','errorID':'form_2language_4field_7_em_','model':'DynamicFormModel','name':'DynamicFormModel[Upload a file]','enableAjaxValidation':true,'inputContainer':'div.control-group'},{'id':'DynamicFormModel_Upload_an_image','inputID':'form_2language_4field_8','errorID':'form_2language_4field_8_em_','model':'DynamicFormModel','name':'DynamicFormModel[Upload an image]','enableAjaxValidation':true,'inputContainer':'div.control-group'}],'errorCss':'error'});
jQuery('#form_2language_6').yiiactiveform({'validateOnType':true,'validateOnChange':true,'validationUrl':'/index.php/appVersion/submitDynamicForm','attributes':[{'id':'DynamicFormModel_Test','inputID':'form_2language_6field_3','errorID':'form_2language_6field_3_em_','model':'DynamicFormModel','name':'DynamicFormModel[Test]','enableAjaxValidation':true,'inputContainer':'div.control-group'},{'id':'DynamicFormModel_test_radio','inputID':'form_2language_6field_4','errorID':'form_2language_6field_4_em_','model':'DynamicFormModel','name':'DynamicFormModel[test radio]','enableAjaxValidation':true,'inputContainer':'div.control-group'},{'id':'DynamicFormModel_Dropdown','inputID':'form_2language_6field_5','errorID':'form_2language_6field_5_em_','model':'DynamicFormModel','name':'DynamicFormModel[Dropdown]','enableAjaxValidation':true,'inputContainer':'div.control-group'},{'id':'DynamicFormModel_Check_this','inputID':'form_2language_6field_6','errorID':'form_2language_6field_6_em_','model':'DynamicFormModel','name':'DynamicFormModel[Check this]','enableAjaxValidation':true,'inputContainer':'div.control-group'},{'id':'DynamicFormModel_Upload_a_file','inputID':'form_2language_6field_7','errorID':'form_2language_6field_7_em_','model':'DynamicFormModel','name':'DynamicFormModel[Upload a file]','enableAjaxValidation':true,'inputContainer':'div.control-group'},{'id':'DynamicFormModel_Upload_an_image','inputID':'form_2language_6field_8','errorID':'form_2language_6field_8_em_','model':'DynamicFormModel','name':'DynamicFormModel[Upload an image]','enableAjaxValidation':true,'inputContainer':'div.control-group'}],'errorCss':'error'});
jQuery('body').tooltip({'selector':'a[rel=tooltip]'});
jQuery('body').popover({'selector':'a[rel=popover]'});
jQuery('#yii_bootstrap_collapse_0').collapse({'parent':false,'toggle':false});
});
/*]]>*/
The Ajax Validation does not return with the ID of the field but the attribute name. This is the response I'm getting:
{"DynamicFormModel_test":["Test cannot be blank."],"DynamicFormModel_radio":["Radio cannot be blank."],"DynamicFormModel_Check":["Check cannot be blank."],"DynamicFormModel_this":["This cannot be blank."],"DynamicFormModel_Upload":["Upload cannot be blank.","Upload cannot be blank."],"DynamicFormModel_a":["A cannot be blank."],"DynamicFormModel_file":["File cannot be blank."],"DynamicFormModel_an":["An cannot be blank."],"DynamicFormModel_image":["Image cannot be blank."]}
The binding that is trying to be made will not work. The reason is that I get:
"DynamicFormModel_test":["Test cannot be blank."]
Whereas I need it to bind to for example: form_2language_6field_4_em_
Suggestions please!
Cause was due to a bug. See bugfix https://github.com/yiisoft/yii/issues/3144

Codeigniter Post Value From Form 1 Changes After Submitting Form 2

I ended up using a session array and storing data there so that I can reference it again later. I just added my post data from each form into this array and referenced it later in my else block. Thanks for the help!
I am using CodeIgniter for a school project. I have some experience with PHP but am relatively new to this framework. I am having trouble using two forms on one page.
I have one form that displays a dropdown of artists. After clicking the submit button for this form, it updates the second form (another dropdown) on the same page with the portfolios belonging to the artist selected in the first dropdown.
I am trying to echo the values from each form just for testing purposes right now, I will implement other features later. My issue is that after my second form is submitted, the post value for the first form is changed. If I echo the selected value of the first form before submitting the second form, it shows the value that was selected. If I echo the value of the first form after both forms have been submitted, it shows the first available value from that dropdown.
I need to be able to take the values from both of these forms and then use them later after both forms have been submitted. So I can't have the values changing right when I need to use them, obviously, any help would be appreciated. Thank you much.
Controller
public function formtest(){
//Making a call to the model to get an array of artists from the DB
$data = $this->depot_model->get_artists_list();
$this->form_validation->set_rules('artist', 'Artist');// Commenting this out for now, 'required');
$this->form_validation->set_rules('ports', 'Portfolios', 'required');
if ($this->form_validation->run() == FALSE)
{
//Building the artists dropdown form
$data['data'] = form_open('formtest', 'class="superform"')
. form_label('Artist<br/>', 'artist')
. form_dropdown('artist', $data)
. form_submit('mysubmit', 'Submit')
. form_close();
//Setting up a temp array of the selected artist's portfolios
$ports = $this->depot_model->get_portfolios(url_title($data[$this->input->post('artist')]));
//Culling out the names of the portfolios from my temp array
$newdata = array();
foreach($ports as $port){array_push($newdata, $port['name']);}
//Building the artist's portfolio dropdown
$newdata['data'] = form_open('formtest', 'class="superform"')
. form_label('Portfolios<br/>', 'ports')
. form_dropdown('ports', $newdata)
. form_submit('mysubmit', 'Submit')
. form_close();
//Send the information to my view
$this->load->view('formtest', $data);
$this->load->view('formtest', $newdata);
}
else
{
//This echos out the first available value from my dropdown rather than the one I selected.
echo $data[$this->input->post('artist')];
echo "success";
}
}
The forms are separate, so when the second gets submitted, there is in effect no value received from the first form, as it isn't included as a field in the second. So you can do that, include say a hidden field in the second form that has the value of the artist. eg:
$newdata['data'] = form_open(
'formtest',
'class="superform"',
array('artist' => $this->input->post('artist'))
);

Get gravity forms input values

How do i get the values of specific input elements inside the gform_after_submission hook in Gravity Forms?
I can get the labels with
foreach($form['fields'] as $k=>$v)
{
$label=$form['fields'][$k]['label'];
}
but how do I get the values?
Following the Gravity guidelines you set up the hook to call your own function - in the case below the function is after_submission().
You can access the input values of individual form elements using their IDs like so;
add_action("gform_after_submission", "after_submission", 10, 2);
function after_submission($entry, $form){
$name = $entry["2"];
$address = $entry["17"] . ', '. $entry["18"] .', '. $entry["19"];
}
The IDs are all there in the form fields title in the backend, just hover over each one and it'll give you the type and ID (ie 'Single Line Text: Field ID 2).
http://www.gravityhelp.com/documentation/page/Gform_after_submission

drupal adding a user reference field to a template

I have a template file that I want to print a the cck user reference form field on.
Is this possible? Note, this is not the field value, but the field form. Also asking for just the field, not creating a form first and then using drupal_get_form()
Thanks!
Edit: If this is not possible that's fine too, I just would like to know
Edit2: i just need the autocomplete mechanism so I can grab the uid in js from searching for the username
If you just need autocomplete mechanism I would suject you to use jQuery autocomplete plugin - http://docs.jquery.com/Plugins/autocomplete.
you can just output in the template something like that:
print '<input type="text" id="user-autocomplete">';
Then in javascript code
$(document).ready('function(){
$('#user-autocomplete').autocomplete(some-ajax-url-here)
}');
you also will need to create an ajax callback page somewhere in your module:
function YOUR_MODULE_NAME_menu(){
$items = array();
$items['user-autocomplete-ajax-page'] = array(
'title' => 'AJAX:get user',
'page callback' => 'get_user'
);
}
function get_user(){
$sql = 'SELECT uid, name FROM {users} WHERE name LIKE ("%s")';
$result = db_query($sql,$_GET['request']);
$res_str = '';
while($object = db_fetch_object($result)){
$res_str .= $object->name.' ['.$object->uid."]\n";
}
print $res_str;
}
I didn't test the code, but I guess it should work, may be with some minor changes.

Categories