I am using a plugin called formidable which is similar to Gravity Forms in terms of purpose.
I have two fields of interest. The first field loads up all the values in the Town
column of my wp_locations
table. Using the code below.
function frm_populate_posts_town($values, $field){
if($field->id == 800){ //change 125 to the ID of the field to populate
global $wpdb;
$towns =$wpdb->get_results("SELECT Town FROM wp_locations");
$values['options'] = array();
foreach ($towns as $town) {
$values['options'][$town->Town] = $town->Town;
}
}
return $values;
}
The second field is a hidden field that automatically selects the value in the 'County' Column depending on the value of the first field. (btw $_POST['item_meta'][802]
represents the hidden field
add_filter('frm_validate_field_entry', 'my_custom_validation_town', 8, 3);
function my_custom_validation_town($errors, $posted_field, $posted_value){
if($posted_field->id == 802){ //change 25 to the ID of the hidden field
$town_b = $_POST['item_meta'][800];//Gets the value from the first field
global $wpdb;
$query = "SELECT County FROM wp_locations WHERE Town = $town_b";
$the_county = $wpdb->get_var($query);
$_POST['item_meta'][802] = $the_county;
}
return $errors;
}
The second does not work.
EDIT
I found out that I need to use the action hook frm_after_create_entry and then add a Javascript in order to do 'Text calculations' which is one of the functions the plugin has.
Unfortunately I do not know how to add the SQL query as a variable using the value of #field_800
e.g:
"SELECT County FROM wp_locations WHERE Town =`#field_800`"
<script type="text/javascript">
jQuery(document).ready(function($){
$('#field_800').change(function(){
var val1 = $("#field_800").val();//How can I add mySQL here
$("#field_802").val(val1);
});
});
</script>
$town_b
to select the value in the 'County' Column depending on the value of the 'town' chosen in the first field.it doesnt – Adzay Jul 13 '13 at 18:31$values['options'][$town->Town] = $town->Town;
to$values['options'][$town->Town] = $town->Town.'-'.$town->County;
( I changed the SQL toSELECT Town,County
) and then use jquery String split to split the values into different fields. Considering what you told me about XY problems, I don't think it is right to put that as an answer, because its a workaround. Do you agree? – Adzay Jul 14 '13 at 20:58