AJAX запрос получить ошибку, когда одни данные имеют апостроф (недопустимый ответ JSON) - PullRequest
0 голосов
/ 24 октября 2019

Я пытаюсь получить все значения таблицы разделов, используя ajax request.it, но я обнаружил, что у моей проблемы есть какое-то значение. REGISTRAR'S OFFICE получит ошибку json. Я могу понять, почему это происходит. Но я до сих пор не могу найти способрешить - это проблема.

 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-запрос

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,
      },
    ],
  });

как получить все значения с помощью REGISTRAR'S OFFICE (ОФИС РЕГИСТРАТОРА - одинназвания раздела)

enter image description here

код ошибки enter image description here

как изменить запрос в соответствии ск вышеуказанному требованию?

1 Ответ

1 голос
/ 24 октября 2019

попробуйте добавить htmlentities и stripslashes на вашем $sub_array.

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

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...