Ask your WordPress questions! Pay money and get answers fast! (more info)

Can you help fix gform_populate_post using ajax return? WordPress

  • SOLVED

No values are being passed (see attachment). Options field displays with two radio button choices but nothing else. May not be passing field values in ajax correctly (ie urlData = 'rxtype='+input_211+'). Ask question here prior but code did not work. Here's site: http://www.seemeoptical.com/?page_id=13722

//HERE ARE HOOKS


add_filter( 'gform_pre_render_23', 'populate_posts' );
add_filter( 'gform_pre_validation_23', 'populate_posts' );
add_filter( 'gform_pre_submission_filter_23', 'populate_posts' );
add_filter( 'gform_admin_pre_render_23', 'populate_posts' );
function populate_posts( $form ) {

foreach ( $form['fields'] as &$field ) {

if ( $field->type != 'select' || strpos( $field->cssClass, 'populate-posts' ) === false ) {
continue;
}

// you can add additional parameters here to alter the posts that are retrieved
// more info: [http://codex.wordpress.org/Template_Tags/get_posts](http://codex.wordpress.org/Template_Tags/get_posts)
$posts = get_posts( 'numberposts=-1&post_status=publish' );

$choices = array();

foreach ( $posts as $post ) {
$choices[] = array( 'text' => $post->package_name, 'value' => $post->price );
}

}

return $form;


//HERE'S AJAX REQUEST

}



<script type="text/javascript">
urlData = 'rxtype='+input_211+'&lens='+input_212+'&od_sphere='+input_59+'&od_cylinder='+input_20+'&os_sphere='+input_56+'&os_cylinder='+input_8+'&impact_resistant='+input_231+'&prism='+input_29+'&tracking='+trackingdis;
        var packages_content = "<select name='material_and_thickness' id='material_and_thickness' onChange='PackageClicked();'><option value='0'>None</option>";

        $.ajax({
                url: '/package_check.php',
                type: 'POST',
                dataType: 'text',
                data: urlData,
                success: function(data) {
                        var packages = $.parseJSON(data);
                        $.each( packages, function( key, value ) {
                                var price = parseFloat(value['price']).toFixed(2)
                                packages_content += "<option value='"+value['package_name']+"#"+price+"#"+value['ar']+"'>"+value['package_name']+" $"+price+"</option>";
                e

 if     })
                        packages_content += "</select>";
                        $('#container_lens_package').html(packages_content);
                }
        })
</script>

Answers (2)

2015-10-25

Romel Apuya answers:

http://www.seemeoptical.com/?page_id=13722

this looks different from the screenshot.

2015-10-25

Rempty answers:

Your code have some mistakes. Read the comments.


add_filter( 'gform_pre_render_23', 'populate_posts' );
add_filter( 'gform_pre_validation_23', 'populate_posts' );
add_filter( 'gform_pre_submission_filter_23', 'populate_posts' );
add_filter( 'gform_admin_pre_render_23', 'populate_posts' );
function populate_posts( $form ) {
foreach ( $form['fields'] as &$field ) {
//Why this
if ( $field->type != 'select' || strpos( $field->cssClass, 'populate-posts' ) === false ) {
continue;
}
//Is better work with input ID
//if ( $form->'id' != 5 ) {
// return $form;
// }

// you can add additional parameters here to alter the posts that are retrieved
// more info: [http://codex.wordpress.org/Template_Tags/get_posts](http://codex.wordpress.org/Template_Tags/get_posts)
$posts = get_posts( 'numberposts=-1&post_status=publish' );
$choices = array();
foreach ( $posts as $post ) {
$choices[] = array( 'text' => $post->package_name, 'value' => $post->price );
}
//Need to asign the values to the select
$field->choices = $choices;
}

return $form;
//HERE'S AJAX REQUEST

}


ProjectAgile comments:

I sent you a request at rempty3. If wrong name, please send me your skype.

TM Poynter


ProjectAgile comments:

if you seriously want to help, best to just answer question here on wpquestions no skype or email.


ProjectAgile comments:

Can you give me an example of what values I'm using to assign to select?
Thanks