0

Hello I'm implementing a tool in Wordpress where the user can choose a source and a target language for a translation of a file. The source languages are insert into the options tag when the page is loaded, and when the user choose a source language the system need to make a query on the database for get the target languages that are available for the selected language and need to be insert into another. When I make the ajax request, the server respond me with a 500 Internal Server Error. The query is correct and I don't understand why it happen.

PHP of the dropdown menu:

<?php 
    $languageMatrix = array();
    $currentLanguage = "";
    function show_languages_list(){
    global $wpdb;
    $table_name = "wp_languages_price";
    $prepared_query = $wpdb->prepare("SELECT * FROM ".$table_name."");
    $results = $wpdb->get_results($prepared_query );
    echo '<label for="'.'source'.'">Select source language:</label><select name="'.'source'.'" id="'.'source'.'" onchange="showTargetLanguages(this.value)">';
    echo '<option value="" disabled selected>Select language</option>';
    $sourceLanguages = array();
    foreach($results as $languageCouple){
        //echo '<option>'.$languageCouple->languages_1.' - '.$languageCouple->languages_2.'</option>';
        $languageRow = array($languageCouple->languages_1, $languageCouple->languages_2, $languageCouple->price);
        array_push($languageMatrix, $languageRow);
        //Create an array of unique languages
        if(!in_array($languageCouple->languages_1, $sourceLanguages)){
            array_push($sourceLanguages,$languageCouple->languages_1);
            echo '<option value="'.$languageCouple->languages_1.'">'.$languageCouple->languages_1.'</option>';
        }
        


    }
    echo '</select>';
    echo '<label for="'.'target'.'">Select target language:</label><select name="'.'target'.'" id="'.'target'.'">';
    echo '<option value="" disabled selected>Select language</option>';
    echo '</select>';


}
show_languages_list();?>

Ajax call:

function showTargetLanguages(sourceLanguage){
            if (sourceLanguage == "") {
                document.getElementById("txtHint").innerHTML = "";
            return;
            } else {
                var xmlhttp = new XMLHttpRequest();
                xmlhttp.onreadystatechange = function() {
                    if (this.readyState == 4 && this.status == 200) {
                    document.getElementById("txtHint").innerHTML = this.responseText;
                    }
                };
                //Target get_stylesheet_directory_uri()."/gettarget.php"
            xmlhttp.open("GET",'/wp-content/themes/hello-theme-child-master/gettarget.php?q='+sourceLanguage,true);            
            xmlhttp.send();
        } 
}

PHP of the Ajax GET request:

<?php

    $q = ($_GET['q']);
    function get_target_languages(){
        global $wpdb;
        $table_name = "wp_languages_price";
        $prepared_query = $wpdb->prepare("SELECT * FROM ".$table_name." WHERE language_2'=".$q."'");
        $results = $wpdb->get_results($prepared_query );
    }
    get_target_languages();
?>

Am i doing something wrong? Thank you so much for the time!

LorenzoAcc
  • 21
  • 2
  • Well, you do not have any error handling in your php code, so you cannot possibly know what has gone wrong. – Shadow Mar 02 '21 at 10:27
  • Try to enable `WP_DEBUG` and `WP_DEBUG_LOG` in `wp-config.php`, make Ajax request and check log file `wp-content/debug.log`. [Codex](https://wordpress.org/support/article/debugging-in-wordpress/) – nmr Mar 02 '21 at 10:51

0 Answers0