Reply To: autocomplete from mysql db

Home Forums Quform WordPress autocomplete from mysql db Reply To: autocomplete from mysql db

#35706
Icemanmx
Participant

Hello
I found this, is this something maybe i can use?

To add autocomplete functionality to a Quform input field using jQuery and MySQL, you can follow these steps:

    Set up the database:
        Create a MySQL database and table to store the data you want to use for autocompletion.
        For this example, let's assume you have a table named "products" with a column named "name" containing the product names.

    Create a custom WordPress plugin:
        Create a new directory in the wp-content/plugins directory of your WordPress installation. For example, you can name it "autocomplete-plugin".
        Inside the "autocomplete-plugin" directory, create a new PHP file, e.g., "autocomplete.php", and add the following code:


<?php
/*
Plugin Name: Autocomplete Plugin
*/

function enqueue_autocomplete_scripts() {
// Enqueue necessary scripts for autocomplete
wp_enqueue_script('jquery-ui-autocomplete');
}
add_action('wp_enqueue_scripts', 'enqueue_autocomplete_scripts');

function autocomplete_search_results() {
global $wpdb;

$term = $_GET['term'];

// Prepare the SQL query
$query = "SELECT name FROM ".$wpdb->prefix."products WHERE name LIKE '%".$term."%'";
$results = $wpdb->get_col($query);

// Format the results as JSON
echo json_encode($results);
exit;
}
add_action('wp_ajax_autocomplete_search_results', 'autocomplete_search_results');
add_action('wp_ajax_nopriv_autocomplete_search_results', 'autocomplete_search_results');

Activate the plugin:

    Log in to your WordPress admin dashboard.
    Go to the "Plugins" section.
    Find your "Autocomplete Plugin" and click the "Activate" button.

Modify the Quform form:

    Edit the Quform form you want to add autocomplete to.
    Find the input field you want to enable autocomplete for.
    In the "Advanced" section of the field settings, set the "Custom element class" to something unique, e.g., "autocomplete-field".

Create a custom template file:

    In your WordPress theme's directory, create a new PHP file, e.g., "quform-autocomplete-template.php".
    Add the following code to the template file:


<script>
jQuery(document).ready(function($) {
$(".autocomplete-field").autocomplete({
source: "<?php echo admin_url('admin-ajax.php'); ?>?action=autocomplete_search_results",
minLength: 1
});
});
</script>

Edit the Quform form shortcode:

    Locate the shortcode for your Quform form in the content area of your WordPress page or post.
    Add the include parameter to the shortcode, pointing to the custom template file:

[quform id="YOUR_FORM_ID" include="path/to/quform-autocomplete-template.php"]

    Replace "YOUR_FORM_ID" with the actual ID of your Quform.

    Save the changes to the template file and the Quform shortcode.

Now, when you view the page or post containing the Quform, the input field with the "autocomplete-field" class will have autocomplete functionality. As the user types in the field, an AJAX request will be sent to the server to retrieve matching results from the "products" table. The results will be displayed as suggestions in the autocomplete dropdown.

Can you check if i can use this, i don´t want a dropdown i just want to search on client textfield and that when right client show up i just choose it then all the others fields will be populatet…

Or do you have a develooper for me? who can make this plugin for me?

Best Regards
Icemanmx

Attachments:
You must be logged in to view attached files.
Be inspired. © 2022 ThemeCatcher Ltd. 20-22 Wenlock Road, London, England, N1 7GU | Company No. 08120384 | Built with React | Privacy Policy