nzrnfourtwenty nzrnfourtwenty - 2 months ago 8
Ajax Question

Select function in ajax

My php file

$option1 = filter_input(INPUT_POST, 'key', FILTER_SANITIZE_SPECIAL_CHARS);

if(isset($option1) && !empty(option1)) {
$sql = "SELECT * FROM fantasy WHERE os_id='$option1'";
}


My ajax

function filter(){
var str = $("#advanced-search1").val();
$.ajax({
dataType: 'html',
url: "filter.php",
type: "POST",
data: {"key": str},
success: function(response){
$('#result').html(response);
}
});
}


From the above, stated that my select function id is #advanced-search1 whereas my submit button would be onclick="filter()". But it still doesn't work for me to execute the sql command in my php file. From my url browser, it stated the required select value, but it doesn't show anything in my #result div

Answer

Add event.preventDefault(); at the beginning of filter function :

function filter(event){

    event.preventDefault();

   var str = $("#advanced-search1").val();
   $.ajax({
     dataType: 'html',
     url: "filter.php",
     type: "POST",
     data: {"key": str},
     success: function(response){
         $('#result').html(response);
     }
   });
}

However, you need to change the call also to pass the event argument :

<button onclick="filter(event);" >..</button>

Instead of :

<button onclick="filter();" >..</button>

If does not work , you have to report your PHP log as well as your HTML skeleton.

Comments