jincongho / PHP-Decision-Tree

Build decision tree, train the tree and classify new data. Supports multiple discrete value for each attributes, eg: attribute[wind] can have three values (strong, mild, weak).
15 stars 10 forks source link

Can't get data in POST form submit with ajax jquery #1

Open anoth12 opened 4 years ago

anoth12 commented 4 years ago

I'm trying to use your library, and there is a problem about that, when I use the classify () function with the usual array parameters, for example arrays ('data', 'data', 'data') can return the results of the classification results

but when I use an array with ajax POST parameters from the input form, suppose the array ($ _ POST ['data'], $ _POST ['data'], $ _POST ['data']) does not produce the expected classification.

dateset: https://archive.ics.uci.edu/ml/datasets/Car+Evaluation my code:

decision_action.php `<?php include_once '../config/varconfig.php'; include_once '../lib/PHP-Decision-Tree/vendor/autoload.php'; $Tree = new Jincongho\DecisionTree\DecisionTree;

if(isset($_POST['submit'])){ /**

and ajax code in decision.php `$("document").ready(function(){ // $("#hasil1").hide(); // $("#hasil2").hide();

$('#submit').click(function(){
    var buying = $("select[name=buying]").val();
    var maint = $("select[name=maint]").val();
    var doors = $("select[name=doors]").val();  
    var persons = $("select[name=persons]").val();  
    var lug_bot = $("select[name=lug_bot]").val(); 
    var safety = $("select[name=safety]").val();            
    $.ajax({
        method: 'POST',
        url: "<?= $var['BASE_URL']; ?>response/decision_action.php",
        cache: false,
        data: {buying: buying, maint:maint, doors:doors, 
                persons:persons, lug_bot:lug_bot, safety:safety, submit:"ada"},
        dataType: 'json',
            success: function(data){
                console.log(data);
                // console.log(data.hasil);
                $("#hasil1").show();
                $("#hasil2").show();
                $("#buying").text(data.data_buying);
                $("#maint").text(data.data_maint);
                $("#doors").text(data.data_doors);
                $("#persons").text(data.data_persons);
                $("#lug_bot").text(data.data_lug_bot);
                $("#safety").text(data.data_safety);
                $("#hasil_hitung").text(data.hasil);

                $('html, body').animate({
                    scrollTop: $("#as").offset().top
                }, 500);
            }
    });
});`

I hope there is a way to resolve the problem, thank you.

jincongho commented 4 years ago

Please run var_dump($dt) after your $dt = array($_POST['buying'], $_POST['maint'], $_POST['doors'], $_POST['persons'], $_POST['lug_bot'], $_POST['safety']); and put the output here. That's more helpful for debugging.

anoth12 commented 4 years ago

result var_dump($dt): array(6) { [0]=> string(5) "vhigh" [1]=> string(5) "vhigh" [2]=> string(1) "2" [3]=> string(1) "2" [4]=> string(5) "small" [5]=> string(5) "small" }

after I checked several times, maybe this is because in the dataset there is no data with the same array, the error obtained is the Internal Server Error [500].

when running a classification with $ Tree-> classify (array ('vhigh', 'vhigh', '2', '2', 'small', 'small')) produces the same error 500, because there is no row in the dataset like that.

but when running the classification $ Tree-> classify (array ('vhigh', 'vhigh', '2', '2', 'small', 'med')) can return the classification results

if the decision tree calculation in R language can return the classification result that is "unnac" even though in the dataset there is no row like that.

is there a solution to this problem? :)

jincongho commented 4 years ago

Will take some time this week to see if I can enhance it.