1

I am using this ajax function to get options for two dropdown via ajax but it is not returning any output. Hi this is my ajax function :

 <script>
$('#department').on('change',function(){
    var department = $(this).val();
    var course = $('#course').val();
    if(department){
        $.ajax({
            type:'POST',
            url:'ajaxData.php',
            dataType: 'json',
            cache: false,
            data:{department: department, course: course },
            success: function(data){
                $('#head_name').html(data.head_name);
                $('#email').html(data.email);
            }
        }); 
    }else{
        $('#head_name').html('<option value="">Select Department first</option>'); 
        $('#email').html('<option value="">Select Department first</option>'); 
    }
});
</script>

And this is my query code:

if(isset($_POST["department"]) && isset($_POST["course"])){
    //Get all courses data

    $query = $db->query("SELECT head_name, email FROM head WHERE course = '".$_POST['course']."' AND department = '".$_POST['department']."' ");

    //Count total number of rows
    $rowCount = $query->num_rows;

    //Display result list
    if($rowCount > 0){
        while($row = $query->fetch_assoc()){

            $temp = array('head_name' => '<option value="'.$row['head_name'].'">'.$row['head_name'].'</option>', 'email' => '<option value="'.$row['email'].'">'.$row['email'].'</option>' );
            echo json_encode($temp);
        }
    }else{
        $temp = array('head_name' => '<option value="">Not avaialble </option>', 'email' => '<option value="">Not avaialble </option>' );
        echo json_encode($temp);
    }
}

But I'm not getting output result from it, what I'm doing wrong ?

5
  • use console.log(data) to see the return output and also parse json before using return data object. Commented Mar 18, 2017 at 11:03
  • try to output course via console.log after you assigned the result of $('#course').val(). If it's not defined, you will get no response. Commented Mar 18, 2017 at 11:03
  • @TobiasF. I have outputed the course as well as the department value , they are working fine. Commented Mar 18, 2017 at 11:06
  • I think you should try @scaisEdge s answer, it seems he is right. Commented Mar 18, 2017 at 11:09
  • In case you're not already aware, your code is vulnerable to SQL injection attacks. Use parameterised queries instead. Commented Mar 18, 2017 at 11:09

3 Answers 3

1

Your Ajax call expects JSON (dataType: 'json') but you do not return JSON via your PHP output because the default content type is text/html and not application/json. Add the appropriate Content-Type header before echo'ing your JSON output.

header('Content-Type: application/json');
echo json_encode(array('something' => 'else'));
Sign up to request clarification or add additional context in comments.

1 Comment

Actually I have used json before this too , and they were working fine with single post variable.
0

I have just change the variable name of course and my function starts working , don't know why like this:

data:{ department: department, courseid: course},

1 Comment

Is it giving you the expected results from your query, or just "not avaialble"?
0

you echo an array so you should access to data element as an array by index ..

eg for first element

       success: function(data){
            $('#head_name').html(data[0].head_name);
            $('#email').html(data[0].email);
        }

eventually try jsonParse data

       success: function(data){
             jsonData = JSON.parse(data);
            $('#head_name').jsonData(data[0].head_name);
            $('#email').jsonData(data[0].email);
        }

in your while you should use an array for store the values

        while($row = $query->fetch_assoc()){

        $temp[] = array('head_name' => '<option value="'.$row['head_name'].'">'.$row['head_name'].'</option>', 'email' => '<option value="'.$row['email'].'">'.$row['email'].'</option>' );
        echo json_encode($temp);
    }

5 Comments

still not working, and one more thing it is working If I'm sending only one variable in data
i have asked for see the result .of . var_dump( json_encode($temp)); .. if you let me see the result . i can eval it , otherwise .. i can't tell you nothing
var_dump( json_encode($temp)); does output anything
hey buddy looks like I just need to rename the variable value of course and the function is working
Hi @scaisEdge , I'm getting one more problem in this, actually this function was working good if there is single record in database but if there is more than one it does not showing values , may be I don't know how to populate the json array , can u please help me ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.