<?php
public function ajax_autocomplete()
{
global $wpdb;
if (isset($_REQUEST['action']) && $_REQUEST['action'] == 'visual_form_builder_autocomplete') {
$term = esc_html($_REQUEST['term']);
$form_id = absint($_REQUEST['form']);
$field_id = absint($_REQUEST['field']);
if ($field_id == 1426) {
$fields = $wpdb->get_results("SELECT `subject` FROM $this->field_table_name");
$suggestions = array();
foreach($fields as $field) {
$pos = stripos((string)$field->subject, $term);
// // If a match was found, add it to the suggestions
if ($pos !== false) $suggestions[] = array(
'value' => (string)$field->subject
);
}
}
else {
$fields = $wpdb->get_results($wpdb->prepare("SELECT * FROM $this->field_table_name WHERE form_id = %d AND field_id = %d ORDER BY field_sequence ASC", $form_id, $field_id));
$suggestions = array();
foreach($fields as $field) {
$options = unserialize($field->field_options);
foreach($options as $opts) {
// Find a match in our list of options
$pos = stripos($opts, $term);
// If a match was found, add it to the suggestions
if ($pos !== false) $suggestions[] = array(
'value' => $opts
);
}
}
}
// Send a JSON-encoded array to our AJAX call
echo json_encode($suggestions);
}
die(1);
}