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
Related
I have made a webform using the sourcecode from Advanced Webform for Podio . I have a list made from a referenced categoryfield. When an item from the list is selected and the form is submitted, I want the text value from the referenced category field to be copied into the an other textfield.
After submit and before saving the data into Podio I can fetch the data from the
post array.
if ($_POST){
// all the values are stored in $_POST array
Then I get the value from the category field like this:
$value_from_selected_list = $_POST['uu center'];
After this I copy the value to the new text field
$_POST['organisation'] = $value_from_selected_list;
// set all the values in the Podio app
$podioform->set_values($_POST,$_FILES);
$podioform->save();
$podioform = 'Kære '.$_POST['navn'].' Tak for din tilmelding';
}
My problem is that $_POST['organisation'] only returns an id and not the text value.
I found the solution myself. $_POST['uu-center']
holds the item_id of the referenced app. So I found the field_id of the the referenced field and used the get_field_value method that takes the item_id and field_id as arguments
$item_id = $_POST['uu-center'];
$field_id = 154492804;
$uucenter = PodioItem::get_field_value($item_id,$field_id);
The method returns an array with the selected value of the field. Then I got the text string.
$value_to_be_copy = $uucenter[0][value];
Is there a way to create/update form poll fields based on a content type? For example, I've got a post type called Candidate and I'd like like to dynamically update a poll list when a new Candidate is added, or when one is removed.
The reason I'm looking for this is because I'm creating a voting mechanism for this client and they have requested that users see an image, the name, and brief Bio of who they are voting for. My idea is to tie in the names so that I can target a hidden Gravity Form poll on page so when the voter clicks, it updates the corresponding named checkbox.
I can of course add each Candidate one by one, and then add each candidate one by one in the form, but was hoping there was a way to do that in code. So far I haven't found much other than filters in the Gravity Forms Documentation.
To reiterate, my question here is not the frontend connection, rather how to dynamically update/add an option in a poll field in a form when content is created for a Candidate post type.
Any help would be appreciated.
I believe the solution you're after is documented here:
http://www.gravityhelp.com/documentation/gravity-forms/extending-gravity-forms/hooks/filters/gform_pre_render/
Probably a combination of solution examples #1 and #2 on that page will fit your needs? So -
Create a category of standard Wordpress pages (category name: "Candidates"), and the pages would contain the candidate information (bio, photo, etc).
In your functions.php file for your current theme, you'd add something like the following to pull out that category's worth of posts:
// modify form output for the public page
add_filter("gform_pre_render", "populate_checkbox");
// modify form in admin
add_filter("gform_admin_pre_render", "populate_checkbox");
function populate_dropdown($form) {
// some kind of logic / code to limit what form you're editing
if ($form["id"] != 1) { return $form; }
//Reading posts for "Business" category;
$posts = get_posts("category=" . get_cat_ID("Candidates"));
foreach ($form['fields'] as &$field) {
// assuming field #1, if this is a voting form that uses checkboxes
$target_field = 1;
if ($field['id'] != $target_field) { break; }
// init the counting var for how many checkboxes we'll be outputting
// there's some kind of error with checkboxes and multiples of 10?
$input_id = 1;
foreach ($posts as $post) {
//skipping index that are multiples of 10 (multiples of 10 create problems as the input IDs)
if($input_id % 10 == 0) { $input_id++; }
// here's where you format your field inputs as you need
$choices[] = array('text' => $post->post_title, 'value' => $post->post_title);
$inputs[] = array("label" => $post->post_title, "id" => "{$field_id}.{$input_id}");
// increment the number of checkboxes for ID handling
$input_id++;
}
$field['choices'] = $choices;
$field['inputs'] = $inputs;
}
// return the updated form
return $form;
}
I want to check a purchase code, entered by a user.
So on registration page, there is a custom profile field "purchase_code", which value I want to use, to check the code.
So my question:
With which function I can get value of the custom profile field "purchase_code"?
Something like "get_value_of_purchase_code"....
include_once($phpbb_root_path . 'includes/functions_profile_fields.' . $phpEx);
$user_id = 2; //The id of the user that you want to get the custom profiles fields
$cp = new custom_profile();
$user_fields = $cp->generate_profile_fields_template('grab', $user_id);
This creates a multidimensional array in $user_fields containing all the selected users custom fields, including the value and all the field data.
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.
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'))
);