0

I am trying to make an ajax request to my PHP file.

The ajax request occurs when my "Country" select option menu changes. The result is suppose to be a new select option menu titled "State Province" and the options would be based off the choice made in the "Country" select option menu.

This is what I want it to look like:

ideal select option menus

The problem I'm having is when the ajax is making a request to the PHP, the PHP seems to be returning an empty array:

firefox developer edition response

Does anyone know what might be wrong?

Thank you!

HTML for the select option:

<select name="Country" class="form-control input-sm" id="Country">

</select>

Ajax code with the onchange function:

$("#Country").on("change",function(){
    var val = $('#Country').val();
    performAJAX(val,'Country','StateProvince');
});

function performAJAX(choice,prevSelect,newSelect){
    $.ajax({
            type: "post",
            url: "select-creation.php",
            data: {choice: choice, prevSelect: prevSelect,newSelect: newSelect},
            dataType: "json",
            success: function(data){
                var obj = $.parseJSON(data);
                console.log("meow meow");
            }
    });
}

PHP code:

<?php session_start();
    try{
        $choice = $_POST['choice'];

        $prevAttri = $_POST['prevSelect'];

        $nxtAttri = $_POST['newSelect'];

        $data = array();

        $sql = 'SELECT '.$nxtAttri.' FROM agents WHERE '.$prevAttri.' = :userChoice';
        include_once $_SERVER['DOCUMENT_ROOT'].'/inc/Database.class.php';
        $db = new Database();
        $conn = $db->getConnection();
        $query = $conn->prepare($sql);
        $query->bindValue(':userChoice',$choice,PDO::PARAM_STR);

        if($query->execute()){
            $data = $query->fetchAll(PDO::FETCH_ASSOC);
        }//stmt

        return json_encode($data);
    }catch(PDOException $e){
        echo $e->getMessage();
    }
?>
guradio
  • 15,524
  • 4
  • 36
  • 57
Bpicks
  • 33
  • 1
  • 5
  • The "meow meow" console log is meant to act as a test to see if I get a successful data response back from the php file – Bpicks Nov 06 '17 at 03:19
  • 2
    Im not PHP developer, but the php should use `echo json_encode($data)` instead of returning it? – Frankusky Nov 06 '17 at 03:23
  • are you getting data from maybe there is no data return – guradio Nov 06 '17 at 03:23
  • @Frankusky return and echo i think it does not matter it should give the same output of there is really an output to output – guradio Nov 06 '17 at 03:24
  • 2
    @guradio I just made a quick research (https://stackoverflow.com/questions/9387765/what-is-the-difference-between-php-echo-and-php-return-in-plain-english) and looks like isnt the same, return is used for functions and echo to print it, so since its an ajax, it takes the body of the answer as response, if he is `return`ing something, it may be not an json type which its reflected on the screenshot response (its showing an php object). Also, maybe you should need to add the correct headers to the PHP file – Frankusky Nov 06 '17 at 03:31
  • 1
    You definitely want to `echo` the `json`, `return` is not the same as writing. – Rasclatt Nov 06 '17 at 03:33
  • @Frankusky new things to learn everyday :) happy coding mate – guradio Nov 06 '17 at 03:44

1 Answers1

2

Your code return json_encode($data); seems to be nothing or invalid because your code doesn't use a function.

Just use echo json_encode($data);

In the ajax side, you don't need to use $.parseJSON(data); because you already specify the dataType to json it will immediately convert the data response by PHP to an object type.

Rasclatt
  • 12,498
  • 3
  • 25
  • 33
JB Dangoy
  • 36
  • 1