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.
 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.