Nipun Sachinda
Nipun Sachinda

Reputation: 430

ajax request get error when the one data have apostrophe mark(invalid json response)

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

error code enter image description here

how i change query according to the above requirement?

Upvotes: 0

Views: 68

Answers (1)

Smough
Smough

Reputation: 212

try adding htmlentities and stripslashes on your $sub_array.

$sub_array[] = htmlentities(stripslashes($row["Section_Id"])); $sub_array[] = htmlentities(stripslashes($row["Section_Name"])); $sub_array[] = htmlentities(stripslashes($row["dept_Name"]));

Upvotes: 1

Related Questions