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

gform_post_render and populate_post with ajax request WordPress

  • SOLVED

Need help dynamically populating choice drop down. Using gform_post_render, populate_post and an ajax request. Here are the fields I need to target in Ajax request however not sure how. For example, replace '+rxtype+' with actual form field ID to send values. I think '+#input_23_8'
Form ID#: 23
Option : Field ID 211 //RX type
Option : Field ID 231 //impact resistant
Drop Down :Field ID 59 //OD Sphere)
Drop Down :Field ID 20 //OD Cylinder)
Drop Down :Field ID 56 //OS Sphere)
Drop Down :Field ID 8 //OS Cylinder)
Checkboxes : Field ID 29 //Prism)
Radio Buttons : Field ID 212 //Lens type

Here's the snippet to do the ajax request in javascript to the check_package script


urlData = 'rxtype='+rxtype+'&lens='+lens+'&od_sphere='+od_sphere+'&od_cylinder='+od_cylinder+'&os_sphere='+os_sphere+'&os_cylinder='+os_cylinder+'&impact_resistant='+impact+'&prism='+prism+'&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);
                }
        })

Answers (2)

2015-10-24

Rempty answers:

Plz make a test, in the function populate_dropdown. On Right EYE(OD) select a value for Sphere (RE) and must appear on "RX SINGLE VISION (1C STK)".

function populate_dropdown($form){
foreach($form['fields'] as &$field){
if($field->id!='122')
continue;
if(isset($_POST['input_59'])){
$choices=array(
array('text'=>'Sphere RE '.$_POST['input_59'],'value'=>$_POST['input_59'])
);
}
$field->choices=$choices;
}
return $form;
}

If this work u can use json_decode, because all your data is on $_POST[]

$json = file_get_contents($url);
$obj = json_decode($json);


ProjectAgile comments:

Rempty

Put code in functions.php--right? Would need to specify form ID #23.


Rempty comments:

Yes i forgot

add_filter('gform_pre_render_23', 'populate_dropdown');


ProjectAgile comments:

FYI: The ajax request sends the customers' prescription values to the package_check.php to process the values, then ajax returns package options.



Rempty comments:

Yes, just want to know if, the post variables are accesible here function populate_dropdown($form), because each time you press next, the post variables are propagating via ajax.
With this $_POST variables you can get the json via file_get_contents with all your data. (all via php no js)


ProjectAgile comments:

Rempty
It works and you're on the right track but I need to pass the Sphere and Cylinder and other field option values to the ajax request so the package_check.php can determine the appropriate package options and prices. There are over 100 combinations so conditional logic won't work nor will the pre render filter. I will add more $

The question was how to include field #IDs in Ajax request. For example, replace '+rxtype+' with actual form field ID to send values. I think '+#input_23_8'

23 is Form ID and #8 is field for OS Cylinder

Here's ajax again and I attached the package_check.php

$.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);

}

})


Rempty comments:

If you want to continue via js, get the form ID "gform_21"

var formid=jQuery('#content .post-content form').attr('id');


Via PHP

//Formid
$_POST['gform_submit'];

//Field ID 59 //OD Sphere)
$_POST['input_59'];

//Field ID 20 //OD Cylinder)
$_POST['input_20'];
...
//Field ID 29 //Prism)
$_POST['input_29.1'];

//Field ID 212 //Lens type
$_POST['input_212'];



//To get the json sending post data
//Example
$postdata = http_build_query(
array(
'var1' => 'some content',
'var2' => 'doh'
)
);

$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $postdata
)
);

$context = stream_context_create($opts);

$json = file_get_contents('http://example.com/submit.php', false, $context);
$obj = json_decode($json);
//json will be a object


ProjectAgile comments:

Rempty
Gravity form's technical support told me I probably need to use the gform_post_render with the populate_dropdown but your method appears more efficient. However, I don't know if post variables are accessible but I would think so.


Rempty comments:

You can use gform_post_render and replace the ul content, adapt your code and get a li

<li class="gchoice_21_162_0"><input name="input_162" value="1.50 Standard|110" id="choice_21_162_0" tabindex="185" type="radio"><label price=" -$65.00" for="choice_21_162_0" id="label_21_162_0">1.50 Standard<span class="ginput_price"> -$65.00</span></label></li>


Rempty comments:

Now i understand your question, you dont know how get values via Jquery


var odsphere=jQuery("#input_21_59").val();


ProjectAgile comments:

Thanks. Your answers won the prize but I keep clicking on vote up and nothing happens. I will contact support to advise of issue.

2015-10-24

Kyle answers:

Can you describe the process you are trying to execute? I don't think you'll need an ajax request here, GF already has conditional logic and you are going to run into trouble by not having the write data passed to both pre_render and pre_submission


ProjectAgile comments:

thanks for your interest. Created an optical form that has about 100 package options so conditional logical no go. Needs to be populated dynamically based on customers prescription values so pre render will not work. Need to trigger ajax after values entered. Have all the code: ajax, check_package.php, Gravity form hooks gform_post_render and populate_dropdown however need help putting it all together

There are four parts to this process. I was going to post one at a time budgeted appropriately.
1. Customer enters eye prescription values into form (see http://www.seemeoptical.com/?page_id=13722)
2. Customer clicks "next" button and triggers gform_post_render and ajax request
3. Ajax return delivers the following which will be parsed to only package_name and price using populate_drop down.

{"package_name":"1.50 Standard","stock":"100.0000","surface":"145.0000","price":"145.0000","ar":"premium"},{"package_name":"PC Advanced","stock":"100.0000","surface":"190.0000","price":"190.0000","ar":"premium"},{"package_name":"1.6 Thin Lenses","stock":"100.0000","surface":"390.0000","price":"390.0000","ar":"premium"},{"package_name":"1.67 Super Thin","stock":"100.0000","surface":"270.0000","price":"270.0000","ar":"premium"},{"package_name":"1.74 Ultra Thin","stock":"100.0000","surface":"379.0000","price":"100.0000","ar":"premium"},{"package_name":"Trivex HD Performance","stock":"100.0000","surface":"220.0000","price":"220.0000","ar":"premium"}]