KingJ
KingJ

Reputation: 123

Ajax live search data not working if two data is send

I have a live search table ajax which i send data from dropdown. The live table is working if a single data is send. But if there is two data, it wont work. Below is the example of non working ajax.

 $(document).ready(function(){

  load_dataa();

  function load_dataa(query1='',query2='')
  {

   $.ajax({
    url:"viewattendance.php",
    method:"POST",
    data:{query1:query1,query2:query2},
    success:function(data)
    {
     $('#table2 tbody').hide().html(data);
     $('#table2 tbody').hide().fadeIn( 1000);
    }
  })

 };
 $('#year').change(function(){
  $('#hidden_year').val($('#year').val());
  var query1 = $('#hidden_year').val();
  load_dataa(query1);
 });
 $('#month').change(function(){
  $('#hidden_month').val($('#month').val());
  var query2 = $('#hidden_month').val();
  load_dataa(query2);
 });
  });

and PHP file

if($_POST["query1"] != '' && $_POST["query2"] != '' )
{

$query1=$_POST["query1"];
$query2=$_POST["query2"];
 $query = "
 SELECT user.user_type,user.user_id,user.first_name,attendance_year,attendance_month,one,two FROM attendance INNER JOIN user ON user.user_id = attendance.student_id
 WHERE   attendance_year IN (".$query1.") AND attendance_month IN (".$query2.")
 ";
}

The problem is, the live table is not working when i tried to pass two data. IF i pass one data like this, it will work(working) :

$(document).ready(function(){

  load_dataa();

  function load_dataa(query1='')
  {

   $.ajax({
    url:"viewattendance.php",
    method:"POST",
    data:{query1:query1},
    success:function(data)
    {
     $('#table2 tbody').hide().html(data);
     $('#table2 tbody').hide().fadeIn( 1000);
    }
  })

 };
 $('#year').change(function(){
  $('#hidden_year').val($('#year').val());
  var query1 = $('#hidden_year').val();
  load_dataa(query1);
 });
 $('#month').change(function(){
  $('#hidden_month').val($('#month').val());
  var query2 = $('#hidden_month').val();
  load_dataa(query2);
 });
  });

with single data php file(working)

if($_POST["query1"] != '' )
{


$query1=$_POST["query1"];
 $query = "
 SELECT user.user_type,user.user_id,user.first_name,attendance_year,attendance_month,one,two FROM attendance INNER JOIN user ON user.user_id = attendance.student_id
 WHERE   attendance_year IN (".$query1.")
 ";
}

Appreciate any form of help ty.

EDIT: solved it by editing some of the code:

 $(document).ready(function(){

  load_dataa();

  function load_dataa(query1,query2)
  {

   $.ajax({
    url:"viewattendance.php",
    method:"POST",
    data:{query1:query1,query2:query2},
    success:function(data)
    {
     $('#table2 tbody').hide().html(data);
     $('#table2 tbody').hide().fadeIn( 1000);
    }
  })

 };
 $('#year').change(function(){
  $('#hidden_year').val($('#year').val());
  var query1 = $('#hidden_year').val();


 $('#month').change(function(){
  $('#hidden_month').val($('#month').val());
  var query2 = $('#hidden_month').val();
  load_dataa(query1,query2);
 });
  });
   });

Upvotes: 0

Views: 119

Answers (1)

Amarat
Amarat

Reputation: 630

Check your code and fetch step by step

  1. first check your both variables (query1, query2) getting in load_dataa() function or not
  2. now check your both variables ($_POST["query1"], $_POST["query2"]) getting in your php file(viewattendance.php) or not
  3. if you getting your both variable in php file than print your sql query($query) and run that sql query in your db (if use localhost than http://localhost/phpmyadmin/server_sql.php)

enter image description here

  1. If sql query run successfully than run than check your success:function(data){} section.

Upvotes: 1

Related Questions