0

I need to make a mysql query according to the value of dropdown list. Here I use ajax to send dropdown value to the server. I think this part is working for me. But problem is I can not get it to php. Note: both are in the same page.

This is my Jquery code :

$('#filter-value').change(function(){
    var filterValue = $(this).val();
    //console.log(filterValue); 

    $.ajax({
        type: 'post',
        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 HTML form

    <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>

This is my PHP code that I am trying on the top of the page :

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

But this php code is always going to else part and print 'bad'

Can anybody tell me where I am going wrong?

Thank you.

9
  • Add echo json_encode($_POST) to the else clause.
    – Barmar
    Jun 29, 2013 at 2:09
  • Your code checks out. Tell me what do you get if you do var_dump($_POST); exit; on the top of the PHP
    – Starx
    Jun 29, 2013 at 2:11
  • @Starx I tried it and display this array(0) { } on the page
    – TNK
    Jun 29, 2013 at 2:17
  • @Barmar it print this bad[]
    – TNK
    Jun 29, 2013 at 2:18
  • @TNK, It is not the value Dude, specify the url now even if it the same page.
    – Starx
    Jun 29, 2013 at 2:25

2 Answers 2

0

You have missed to specify the URL of the script. Be ensure that you are querying the correct file from the AJAX.

$.ajax({
    type: 'post',
    url: 'yourpage.php', // This one 
    //.....
});
0

Change your php to (and say save as xyz.php):

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

and url to your ajax call:

$.ajax({
    url: "xyz.php",
    type: 'post',
    dataType: 'html',
    data: {filter: filterValue},
    success:function(data){ 
        alert(data); 
    }, 
5
  • you need to post to the file with this php script through your ajax call
    – raj
    Jun 29, 2013 at 2:20
  • RAJ please tell me what do you mean in above comment
    – TNK
    Jun 29, 2013 at 2:22
  • Answer edited. Note that the url you post to in the ajax call hits your php script
    – raj
    Jun 29, 2013 at 2:29
  • do I need to add php to new page?
    – TNK
    Jun 29, 2013 at 2:39
  • not necessarily. you can post to the same page. however, you need to make sure you have the url parameter accordingly in your ajax
    – raj
    Jun 29, 2013 at 2:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.