Jump to content

Send dropdown value to PHP using jquery-ajax


thara

Recommended Posts

I am using this jquery code to send value from a dropdown select box to a php script.

This is my jquery code :

    $('#filter-value').change(function(){
        var filterValue = $(this).val();
        //console.log(filterValue);
        
        $.ajax({
            type: 'post',
            url: 'table.php',
            dataType: 'html',
            data: {filter: filterValue},
            success:function(data){
                alert(data);
            },
            error:function (xhr, ajaxOptions, thrownError){
                //On error, we alert user
                alert(thrownError);
            },
            complete: function(){
                //alert('update success');
            }
        });
    });

This is my HTML code for dropdown
 

        <form method="post" action="">
            <select id="filter-value" name="filter">
                <option value="10">10</option>
                <option value="20">20</option>
                <option value="30">30</option>                
            </select>
        </form>


Now I need to make a mysql query in my `table.php` page with value from dropdown and after making the query I want to get date to main page again.

But in my php page I tried something to check is there the value come from dropdown but still I couldn't get it to echo.

This is php :
 

    if ( isset($_POST['filter'])) {
        $filter = $_POST['filter'];
        echo $filter;
    }

Cay you please tell me how can I do this?
Thank you.
 

I have limited ajax experience, but I don't think you can do a post. You must do a get, passing your dropdown value as an argument of your url. Also - you can't do an echo in the background script since that is how you return answers to the caller. You can only do debugging type echos by running the script standalone until you are done developing it. At least that's how I do it.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.