Multiple form elements in dynamic form section - php

I am building a form where users will need to be able to add multiple 'children' and fill in details for them. Each child has multiple inputs so to keep it simple we'll say a user is trying to add 2 children and each child requires a first name and a surname.
I aim in using javascript to add the children to a list so a user can enter a first name and surname then press 'add' and this will create hidden inputs for these details. What I am struggling with is geting these form inputs to be grouped by child so effectively what I end up with will be a $_POST array like the following:
$_POST['child'][0]['firstname'] = 'asdads';
$_POST['child'][0]['surname'] = 'vcbcvbc';
$_POST['child'][1]['firstname'] = 'asdads';
$_POST['child'][1]['surname'] = 'vcbcvbc';
Is it as simple as just setting the 'name' attribute on the inputs to name="child[][firstname]" or can someone offer a suitable solution?

javascript:
var children = [[{name:value},{name:value}],[{name:value},{name:value}],....];
php:
for($i=0; $i<sizeof($_POST['children']); $i++)
{
foreach($_POST['children'][$i] as name => value)
{
// process
}
}

Related

Gravityforms populate selected value on dropdown within theme functions.php

I am working in gravityforms to pre-populate a form with database values dynamically, which is working. I need to be able to specity which of these options is selected by default when the form is built, but I can't find the option to do so. I have seen the placeholder, but I presume this doesnt actualy select anything on the dropdown. I have been unable to find any docs or references that allow me to set the "selected" options once I have built all the text/value pairs in the array and set the choices.
The function (which I have redacted here) works fine and populates the field, I just need to populate a placeholder and/or default selected value.
add_filter('gform_pre_render_1', 'getFieldValues');
add_filter('gform_pre_validation_1', 'getFieldValues');
add_filter('gform_pre_submission_filter_1', 'getFieldValues');
function getFieldValues($form) {
global $wpdb;
foreach ($form['fields'] as $field) {
if ($field->id == '40') {
// get the list of values from the Database
$list1Q = "SELECT <data> FROM <table> WHERE <params> = <value>";
$list1R = $wpdb->get_results($list1Q);
// Generate a nice array that Gravity Forms can understand
if (!empty($list1R)) {
$list1A[] = array();
foreach ($list1A as $listItem) {
// Add current value to the array for dropdown choices
$list1C[] = array('text' => $listItem->variable, 'value' => $listItem->variable);
}
}
// Set choices to field
$field->choices = $List1C;
***** THIS IS WHERE I WOULD LIKE TO SET THE SELECTED VALUE *****
}
}
return $form;
}
If there is a better way to go about populating this field, I am open to suggestions at it seems that the form loads a bit slow using this method. Otherwise, I would love to know how to set a selected choice value after populating the choices.
the quick answer is, there is an option that i found for "isSelected" that can be included in the array when defining choices. I added a ternary operation to match on what I wanted selected and it set the "selected value for me.
$isSelected = ($listItem->variable == "value I want to select") ? true : false;
$list1C[] = array('text' => $listItem->variable, 'value' => $listItem->variable, 'isSelected' => $isSelected);
Adding the ternary choice and changing the array push allowed me to set a value as selected.

Allow users to update their gravity form single unique entry

I am trying to make a form which will allow them to update their entered unique entry. Suppose there is form which is used to collect, the User Specializations
And this Specializations field is gravity forms multiselect field which is dynamically set. And as they update their specializations also updated in entries tab.
So, i just want to allow they to have this multiselect field updation.
I came across a solution to add multiselect choice dynamically but that only populate the field with all the choices not the user selected also.
So, is there is way to set the user entered choices dynamically?
Possible Solution:
Like we can get the entries and then set search criterion to current user and will get a unique entry and then after we can set these selected values in the choices.
Issue Exists with the above possible solution:
but still there is issue like it will be creating new entry as form dont know it is update operation. So can anyone help?
//Auto Populate Service Field in Form ID=
add_filter('gform_pre_render_2', 'populate_services');
add_filter('gform_pre_validation_2', 'populate_services');
add_filter('gform_pre_submission_filter_2', 'populate_services');
add_filter('gform_admin_pre_render_2', 'populate_services');
function populate_services($form)
{
foreach ($form['fields'] as &$field) {
if ($field->type != 'multiselect' || strpos($field->cssClass, 'populate_services') === false) {
continue;
}
$services = get_specializations();
$choices = array();
foreach ($services as $service) {
$title = stripslashes($service->name);
$choices[] = array(
'value' => $service->ID,
'text' => esc_html($title)
);
}
// update 'Select Duration' to whatever you'd like the instructive option to be
$field->placeholder = 'Select Services';
$field->choices = $choices;
}
return $form;
}

How to display value option from dropdown list in Elgg

Elgg is build on the MVC framework. My main agenda is to be able to save the value selected from the dropdown list, after which is to then display the chosen value the item listing. The following code is actually constructed in PHP that is following the Elgg framework closely.
What I have managed to do is to make use of the existing Elgg framework to display the dropdown list. In which, the dropdown list is created by the creation of a form in the following directory: mod/plugin/views/default/forms/plugin/form.php. I have hence made use of the existing Elgg framework (input/dropdown)to create my dropdown list as a form.
Secondly, I have managed to save the values chosen in the dropdown list and display the value in a success message. This is done in the action directory which will allow the values to be saved into the database when user clicks on 'save' button.
Code for saving and displaying value:
<?php
/**
* Elgg options uploader/submit action
*
* #package ElggFile
*/
// get the input variables
$list = get_input('OptionItems');
$container_guid = (int) get_input('container_guid', 0);
if ($container_guid == 0)
{
$container_guid = elgg_get_logged_in_user_guid();
}
$my_select_guid = (int) get_input (file_guid);
//create a new my_select object
$my_select = new ElggObject();
$my_select -> dropdown = $list;
$my_select ->container_guid = $container_guid;
//save to database and get id of the new my_blog
$my_select_guid = $my_select->save();
if($my_select_guid){
system_message("Your action post = " . $list);
//to add new muy_select object to river
add_to_river('river/object/file/create', 'create', elgg_get_logged_in_user_guid(), $list->guid);
}
else{
register_error("Your action post is not saved");
}
However, at this point, I am stuck in displaying the chosen value of the dropdown list as an extended view, within the view/default/object/file/
How am I able to do this?
It's all in Elgg documentation about the views that I linked for you before: http://learn.elgg.org/en/1.12/guides/views.html#extending-views

Create gravity form poll based on post types

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;
}

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'))
);

Categories