Zkip Zkip - 16 days ago 6
Ajax Question

Ajax response in console it's ok, but in php page doesn't arrive

I was searching for some time an answer for my problem, but every solution I tried doesn't work.

I have a html select

<select id="exampleid">
<option>Option1</option>
<option selected>Option2</option>
<option>Option3</option>
</select>


Ajax request

$('#exampleid').change(function(){

var aabbcc = $(this).val().trim();
$.post("_ws/rca.php?action=brands", {aabbcc:aabbcc}, function(response) {
console.log('search is: ' + aabbcc + ', Response from PHP script: ' + response);
});
})


This return in console:

search is: Option2, Response from PHP script: array(1) {
["aabbcc"]=>
string(27) "Option2"
}


PHP

if($action == 'brands'){ //action is relevant for link where send the ajax
var_dump($_POST['aabbcc']);
exit;
}


The problem is, the $_POST returns empty array.
I cheched with print_r and var_dump also.
Apparently AJAX sends correct request, but in PHP doesn't arrive.

I checked for 404 errors or something similar, but everything seems to be fine.

Thanks in advance.

Answer

Your main page:

<select id="exampleid">
         <option>Option1</option>
         <option>Option2</option>
         <option>Option3</option>
    </select>

<div class="result">none</div>

<script type="text/javascript">
        $('#exampleid').change(function(){  
            var aabbcc = $(this).val().trim();
            $.post("cosmin_rca.php?action=brands", {aabbcc:aabbcc}, function(response) {
                console.log('Search is: ' + aabbcc + ', Response from PHP script: ' + response);
                $( ".result" ).html( response );
            });
        })
</script>

And this is your callback rca.php

if($_GET['action']=='brands') {
        echo 'PHP: '.$_POST['aabbcc'];
        return;
    }