Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Populate a gravityform dropdown based on date dropdown WordPress

  • SOLVED

I am trying to populate a gravityform dropdown based upon a date in the same form. I have managed to get the date using jQuery focus out event. But I'm having a problem passing the variable to php so I use the gform_pre_render filter. Don't think I've got the ajax right, or perhaps there's a better way?

Here is my jQuery in functions.php

add_filter("gform_pre_render_3", "monitor_dropdown");
function monitor_dropdown($form){
?>
<script type="text/javascript">
jQuery(document).ready(function(){

jQuery('#input_3_11_3').bind('focusout', function()
{
//get selected value from drop down;
var dob = jQuery("#input_3_11_2").val() + jQuery("#input_3_11_1").val() + jQuery("#input_3_11_3").val();
alert(dob);

jQuery("#input_3_65").val(dob);

//push the value of dob to php for processing
$.ajax({
url: '<?php echo admin_url('admin-ajax.php'); ?>',
data: { playerDob : dob , action: 'get_player_team' },
type: 'POST',
success: function(data) {
//Optional callback function on success
}
});
});
});
</script>
<?php
return $form;
}


Here is my code to populate the dropdown

add_filter( 'gform_pre_render', 'populate_choices' );

//Note: when changing choice values, we also need to use the gform_pre_validation so that the new values are available when validating the field.
add_filter( 'gform_pre_validation', 'populate_choices' );

//Note: when changing choice values, we also need to use the gform_admin_pre_render so that the right values are displayed when editing the entry.
add_filter( 'gform_admin_pre_render', 'populate_choices' );

//Note: this will allow for the labels to be used during the submission process in case values are enabled
add_filter( 'gform_pre_submission_filter', 'populate_choices' );
function populate_choices( $form ) {

//only populating drop down for form id 5
if ( $form['id'] != 3 ) {
return $form;
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (isset($_POST['dob'])) {
//Should really validate/sanitise data here
$dob = 'Date of Birth is: ' . $_POST['dob'];
}
}

//set the age group. need to write rule to work out age group and then set $age. Set to U14 for testing
$age = 'U14';

//test value of dob
print_r($dob);


//Reading posts for "sp_team" post type;
$args = array(
'numberposts' => -1, // -1 is for all
'post_type' => 'sp_team', // or 'post', 'page'
'orderby' => 'title', // or 'date', 'rand'
'order' => 'ASC', // or 'DESC'
);
$posts = get_posts($args);

//Creating item array.
$items = array();

//Add a placeholder to field id 38, is not used with multi-select or radio, will overwrite placeholder set in form editor.
$fields = $form['fields'];
foreach( $form['fields'] as &$field ) {
if ( $field->id == 38 ) {
$field->placeholder = 'Please select your team below';
}
}

//Adding post titles to the items array
foreach ( $posts as $post ) {
if ( strpos($post->post_title, $age) !== false ) {
$items[] = array( 'value' => $post->post_title, 'text' => $post->post_title );
}
}

//Adding items to field id 38. Replace 38 with your actual field id. You can get the field id by looking at the input name in the markup.
foreach ( $form['fields'] as &$field ) {
if ( $field->id == 38 ) {
$field->choices = $items;
}
}

return $form;
}

Answers (3)

2019-05-30

Arnav Joy answers:

same 403 problem

2019-05-30

Rempty answers:

there is an error in your jquery script
$.ajax({
change it to
jQuery.ajax({

you can't use $


Also if you are using multiple pages for the form (steps) you can populate from one page to other.
https://docs.gravityforms.com/gform_pre_render/
there is an example in item 3.

Send me an email to [email protected] if you are interested in a fast solution.

2019-05-30

Hugo Gonçalves answers:

Hi!
Could you post a link of this form?

Thank you
Hugo


User182293 comments:

http://www.mjfc.naked-digital.co.uk/product/mjfc-player-registration/


Hugo Gonçalves comments:

this link shows a 403 Forbidden error.


User182293 comments:

What is your IP?


Hugo Gonçalves comments:

188.250.93.134


User182293 comments:

Removed IP restriction


Hugo Gonçalves comments:

Thank you.

Could you send me a copy of gravityforms for local testing?


User182293 comments:

How shall I do that?


Hugo Gonçalves comments:

Send the zip to my email: [email protected] please.

Thank you


User182293 comments:

Sent


Hugo Gonçalves comments:

OK, thank you.


User182293 comments:

I found this, but couldn't get it to work
https://wpquestions.com/gravityform_use_dropdown_choice_as_variable_in_gform_pre_render/7940


Hugo Gonçalves comments:

Ok.
Give me 15 minutes to finish a couple of things, and i'll look into this.


Hugo Gonçalves comments:

Hi!
I've been giving it a go, and really would like to help you, but it's a bit more complex for the time i have available.

Did you manage to solve your problem with Rempty's help?

If not, in the mean time, i will be trying.
Could you explain in more detail what you are trying to achieve?