Dropdown onchange using ajax

0 votes

My form has a dropdown that I used to filter the input. I am filtering the data based on the chosen list using the ajax onchange function.

My dropdown appeared to be like this:

<select id="opt_level" name="opt_level">
    <option value="level1">Level 1</option>
    <option value="level2">Level 2</option>
    <option value="level3">Level 3</option>
</select>

And here is the div that I wanted to display the onchange data :

<div id="opt_lesson_list">
    <!-- Some statement here -->
</div>

When there is onchange on the dropdown, it will go through this ajax function :

jQuery(document).ready(function($) {
    $("#opt_level").on('change', function() {
        var level = $(this).val();
        if(level){
            $.ajax ({
                type: 'POST',
                url: 'example-domain.com',
                data: { hps_level: '' + level + '' },
                success : function(htmlresponse) {
                    $('#opt_lesson_list').html(htmlresponse);
                    console.log(htmlresponse);
                }
            });
        }
    });
});

And going to the URL example-domain.com to check if there is a post made from ajax :

if(isset($_POST['hps_level'])){
    // Statement to select from database
}

Therefore, data inside the div id="opt lesson list">/div> should display the filtered data after filtering is complete. But instead, the entire page received an ajax response, which implies that my entire form is multiplying and being displayed in the div I was using to show onchange data.

Jul 31 in PHP by Kithuzzz
• 4,480 points
32 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In PHP

0 votes
1 answer

Error:Sending Form file with form using AJAX

Hello @kartik, Don't pass the files into the ...READ MORE

answered Jun 16, 2020 in PHP by Niroj
• 82,740 points
1,670 views
0 votes
0 answers

selected value get from db into dropdown select box option using php mysql error

I have to get the selected value ...READ MORE

Jul 31 in PHP by Kithuzzz
• 4,480 points
12 views
0 votes
1 answer

Connection with MySQL server using PHP. How can we do that?

Hey @kartik, You have to provide MySQL hostname, ...READ MORE

answered Mar 27, 2020 in PHP by Niroj
• 82,740 points
417 views
0 votes
1 answer

How to retrieve or obtain data from the MySQL database using PHP?

Hello kartik,  Actually there are many functions that  ...READ MORE

answered Mar 27, 2020 in PHP by Niroj
• 82,740 points
1,326 views
0 votes
1 answer

How can you upload a file using PHP?

Hello, To upload a file by using PHP, ...READ MORE

answered Mar 27, 2020 in PHP by Niroj
• 82,740 points
242 views
0 votes
1 answer

Error:“Notice: Undefined variable”, “Notice: Undefined index”, and “Notice: Undefined offset” using PHP

Hello @kartik, The best way for getting input string is: $value ...READ MORE

answered Apr 1, 2020 in PHP by Niroj
• 82,740 points
32,391 views
0 votes
0 answers

Edit PHP query code depending on image map clicked area using AJAX

I am working on an image map ...READ MORE

Jun 3 in PHP by Kichu
• 19,040 points
23 views
0 votes
0 answers

PHP "php://input" vs $_POST

I want to know what is the ...READ MORE

Jun 9 in PHP by Kichu
• 19,040 points
35 views
0 votes
0 answers

Fetch data from database in php through AJAX,

The index.php  I created a connection with the ...READ MORE

Jun 16 in PHP by narikkadan
• 11,280 points
128 views
0 votes
0 answers

Using Jquery Ajax to retrieve data from Mysql

list.php: A simple ajax code that I ...READ MORE

Jun 20 in PHP by Kithuzzz
• 4,480 points
35 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP