How to reuse 1 php-action for multiple ajax calls with parameters
-
I’m creating a plugin to build my post types, meta boxes and custom fields. I would like to have auto complete functionality on most of the custom fields. I’ve written the following, that won’t work, but describes what I like to achieve:
function se_wp_head() {?> <script type="text/javascript"> var se_ajax_url = '<?php echo admin_url('admin-ajax.php'); ?>'; jQuery(document).ready(function() { jQuery('#cf_label, #cf_band_name').suggest(se_ajax_url + '?action=se_lookup&cf='+jQuery(this).attr('id')); }); </script><?php } add_action('admin_head', 'se_wp_head'); function se_lookup() { global $wpdb; $search = like_escape($_REQUEST['q']); $cf = $_REQUEST['cf']; $query = "SELECT distinct meta_value FROM " . $wpdb->postmeta . " WHERE meta_value LIKE ' . $search . '% AND meta_key = ' . $cf . ' ORDER BY meta_value ASC"; foreach ($wpdb->get_results($query) as $row) { echo $row->meta_value . "\n"; } die(); } add_action('wp_ajax_se_lookup', 'se_lookup');
I insert jQueries suggest functionality for text boxes #cf_label and #cf_band_name in the header and try to send the ID of the actual control where the text is entered to the lookup php function to be a part of my query.
I’d like to be able to have one function that queries the DB with what I type and the control I’m in (the custom field name) to retrieve a distinct list of previous entries. It shouldn’t be that hard, but whatever I try I’m failing, so I definitely miss something..
Hope someone can help. Thanks!
- The topic ‘How to reuse 1 php-action for multiple ajax calls with parameters’ is closed to new replies.