0

I try to fetch the all the value of section table using ajax request.it work find but my problem it has some value REGISTRAR'S OFFICE will get json error .i can understand why it occur.but i still can't find way to solve is problem.

 if(isset($_POST["action"]))
{
    if($_POST["action"] == "fetch")
    {
        $query = "
        SELECT * FROM section 
        INNER JOIN department 
        ON department.dept_Id = section.dept_Id  
        ";
        if(isset($_POST["search"]["value"]))
        {
            $query .= '
            WHERE section.Section_Id LIKE "%'.$_POST["search"]["value"].'%" 
            OR section.Section_Name LIKE "%'.$_POST["search"]["value"].'%" 
            OR department.dept_Name LIKE "%'.$_POST["search"]["value"].'%" 
            ';
        }
        if(isset($_POST["order"]))
        {
            $query .= '
            ORDER BY '.$_POST['order']['0']['column'].' '.$_POST['order']['0']['dir'].'
            ';
        }
        else
        {
            $query .= '
            ORDER BY section.Section_Id ASC 
            ';
        }
        if($_POST["length"] != -1)
        {
            $query .= 'LIMIT ' . $_POST['start'] . ', ' . $_POST['length'];
        }

        $statement = $connect->prepare($query);
        $statement->execute();
        $result = $statement->fetchAll();
        $data = array();
        $filtered_rows = $statement->rowCount();
        foreach($result as $row)
        {
            $sub_array = array();
            $sub_array[] = $row["Section_Id"];
            $sub_array[] = $row["Section_Name"];
            $sub_array[] = $row["dept_Name"];
            $sub_array[] = '<button type="button" name="view_section" class="btn btn-info btn-sm view_section" id="'.$row["Section_Id"].'">View</button>';
            $sub_array[] = '<button type="button" name="edit_section" class="btn btn-primary btn-sm edit_section" id="'.$row["Section_Id"].'">Edit</button>';
            $sub_array[] = '<button type="button" name="delete_section" class="btn btn-danger btn-sm delete_section" id="'.$row["Section_Id"].'">Delete</button>';
            $data[] = $sub_array;
        }
        $output = array(
            "draw"              =>  intval($_POST["draw"]),
            "recordsTotal"      =>  $filtered_rows,
            "recordsFiltered"   =>  get_total_records($connect, 'section'),
            "data"              =>  $data
        );
        echo json_encode($output);
    }

Ajax request

var dataTable = $('#section_table').DataTable({
    "processing":true,
    "serverSide":true,
    "order":[],
    "ajax":{
      url:"section_action.php",
      type:"POST",
      data:{action:'fetch'}
    },
    "columnDefs":[
      {
        "targets":[0, 3, 4, 5],
        "orderable":false,
      },
    ],
  });

how do i fetch all the value with REGISTRAR'S OFFICE (REGISTRAR'S OFFICE is one of the name of the section)

enter image description here

how i change query according to the above requirement?

Code Kris
  • 447
  • 3
  • 19
  • 1
    your query is fine and there is no issue there to solve. Your problem is in the code of the API (or web service or website) that is responding to the Ajax request, or in the Ajax code itself. Add those two pieces of code to your question so we can help you. – Racil Hilan Oct 24 '19 at 03:51
  • okay bro i will update my code – Code Kris Oct 24 '19 at 03:53
  • i update my question – Code Kris Oct 24 '19 at 03:55
  • @tim-biegeleisen The duplicate question is not good. This question has nothing to do with SQL, the OP just got that wrong. Please reopen or we need to find a correct duplicate. – Racil Hilan Oct 24 '19 at 04:07

0 Answers0