Link to home
Start Free TrialLog in
Avatar of Pete Winter
Pete WinterFlag for United Kingdom of Great Britain and Northern Ireland

asked on

select2 search function not working correctly

I am using the select2 search function ( https://select2.org/ ) on the below links. However I am getting different results on my live site verses my dev site and can't work out why as I believe the code is the same. I assume there must be a difference, but I can't see it?

Dev site: https://dev.cmyuk.com/large-format-print/materials/guide/
Live site: https://www.cmyuk.com/large-format-print/materials/guide/

Please do the following...
Test the search function with the placeholder "Search by name" in the middle. As an example just type in "pongs", then select the first item, the click the search icon.
On the dev site this works correctly, but not on the live site. You will see the difference with the url parameter changes. If you inspect the code you will see the select2 value is different too.

I can't see where the code if different. Please help?


Avatar of leakim971
leakim971
Flag of Guadeloupe image

your dev return the following :
User generated image
prod return :
User generated image
User generated imageUser generated imageUser generated image
Avatar of Pete Winter

ASKER

Thanks for the reply and yes I am aware of that, but I am trying to find the code to change the output to what is seen on the dev site. Everything I have checked looks the same?
you send the exact same request but get different response.
the code you're looking for is on the server not about javascript or the select2 plugin
and I've no idea of your server side code... post it of check it

Thanks and yes the data is pulled from a php file as you have supplied, but I think a select2 function places the code in generated code. See the code that should control it below ( In this file https://www.cmyuk.com/large-format-print/materials/guide/js/form_select2.js?v=1.0.46 )

$('.select-remote-data-materials-name').select2({
            placeholder: 'Search by name',
            ajax: {
                url: '/large-format-print/materials/guide/material_search-name.php',
                dataType: 'json',
                delay: 250,
                processResults: function (data) {
                    return {
                      results: data
                    };
                },
                cache: true
            },
            minimumInputLength: 2,
        });

Open in new window


Select2 place the value in the value field seen below...

User generated image
I said :
you send the exact same request but get different response.

that's why I don't even check the client side code (but maybe I'm wrong...)
Yes and that is why I am puzzled and I am asking for assistance. The php file is the same on dev and live site and it connects to the same database. Below is the php code...
<?php

require_once($_SERVER['DOCUMENT_ROOT'] . "/templates/initialize.php"); 
require_once($_SERVER['DOCUMENT_ROOT'] . "/templates/data_tables/media.php");

$product_results = Media::search_by_product_name($_GET['q']);
$product_total_rows = count($product_results);

if($product_total_rows > 0) {

    $json = [];
    foreach ( $product_results as $product ) {
        
        $name_combined = $product->brand . " " . $product->title;
        $json[] = ['id'=>$product->id, 'text'=>$name_combined];
        
    }

    echo json_encode($json);
    
}

?>

Open in new window



I am just testing and outputting the php directly to check.
See the outputs...
Dev: https://dev.cmyuk.com/large-format-print/materials/guide/material_search-name-test.php
Live: https://www.cmyuk.com/large-format-print/materials/guide/material_search-name-test.php

You will see it's the same. So select2 decides what is puts in the select value area.


I am just testing and outputting the php directly to check.

Good, check :
https://dev.cmyuk.com/large-format-print/materials/guide/material_search-name.php?term=pongs&_type=query&q=pongs
https://www.cmyuk.com/large-format-print/materials/guide/material_search-name.php?term=pongs&_type=query&q=pongs

same query, results are different, issue on your PHP, caching issue, wrong folder, bad path config ?
same database (same records) ?
Yes they both point to the same database and records. Very confused!
create another file and use the following :
compare results

<?php


require_once($_SERVER['DOCUMENT_ROOT'] . "/templates/initialize.php");
require_once($_SERVER['DOCUMENT_ROOT'] . "/templates/data_tables/media.php");


$product_results = Media::search_by_product_name("pongs");
$product_total_rows = count($product_results);


var_dump($product_results);

Open in new window


Hey there,

I would start by checking the search_by_product_name() method on the Media class. It's clearly returning different results (particularly the product ID part).

As a quick-and-dirty debug, var_dump the $product_results

var_dump($product_results);

And call the script directly (i.e. in the browsers address bar, rather than through AJAX  Select2).
Thanks again. See the results:

https://dev.cmyuk.com/large-format-print/materials/guide/material_search-name-test2.php
https://www.cmyuk.com/large-format-print/materials/guide/material_search-name-test2.php

They look the same.

Why would my file on the dev:
https://dev.cmyuk.com/large-format-print/materials/guide/material_search-name-test.php
and your example:
https://dev.cmyuk.com/large-format-print/materials/guide/material_search-name.php?term=pongs&_type=query&q=pongs
Why would these be different? Below is my code:
<?php

require_once($_SERVER['DOCUMENT_ROOT'] . "/templates/initialize.php"); 
require_once($_SERVER['DOCUMENT_ROOT'] . "/templates/data_tables/media.php");

$product_results = Media::search_by_product_name("pongs");
$product_total_rows = count($product_results);

if($product_total_rows > 0) {

    $json = [];
    foreach ( $product_results as $product ) {
        
        $name_combined = $product->brand . " " . $product->title;
        $json[] = ['id'=>$product->id, 'text'=>$name_combined];
        
    }

    echo json_encode($json);
    
}

?>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Many thanks for your help,