Ajax不返回php echo

时间:2019-07-01 10:06:33

标签: javascript php wordpress

我试图检查onblur条目号是否已经在数据库中可用。当存在时,我想显示表。在localhost xampp上一切正常,但是当我尝试在wordpress页面上执行此操作时,ajax不返回任何内容。当我在<?php上方编写文本时,ajax返回此内容,但从php no回显。区别在哪里?当我在wordpress页面上使用它时,我必须在代码中添加些什么吗?

    <script>
    function checkAvailability() {

      //alert(document.getElementById("f_numer").value);
        jQuery.ajax({
        url: "check_availability.php",
        data:'f_numer='+document.getElementById("f_numer").value,
        type: "POST",
        success:function(data){
        console.log(data);
            $("#tab").html(data);
        if(data != 0)
        {
          var element = document.getElementById("tablica");
          element.scrollIntoView({behavior: "smooth", block: "start", inline: "nearest"});

        }


        },
        error:function (){
      }
        });
    }
    </script>

 <?php

require("/wp-content/zwroty_edycja/dbcontroller.php");
$db_handle = new DBController();

if(!empty($_POST["f_numer"])) {

$query = "SELECT * FROM bledy WHERE `Nr_zamówienia`='" . $_POST["f_numer"] . "'";

$numer_count = $db_handle->numRows($query);
if($numer_count>0) {
 $conn = $db_handle->connectDB();

      $result = $conn->query($query);
      //echo ('<p id="tekst">'.$sql.'</br>Liczba pozycji: '.$result->num_rows."</p>");
      echo ('<p id="tekst">Liczba pozycji: '.$result->num_rows."</p>");
      if ($result->num_rows > 0) {

          while($row = $result->fetch_assoc()) {
            $kolumny[] = $row;
          }

          $kolumny = array_reverse($kolumny,true);

          foreach($kolumny as $row)
          {
              echo "
              <tr><td>{$row["Id"]}</td>
              <td>{$row["Data"]}</td>
              <td>{$row["Firma"]}</td>
              <td>{$row["Nr_zamówienia"]}</td>
              <td>{$row["Imię_nazwisko"]}</td>
              <td>{$row["Zwrot_wymiana"]}</td>
              <td>{$row["Model_auta"]}</td>
              <td>{$row["Rodzaj_produktu"]}</td>
              <td>{$row["Przyczyna_zwrotu"]}</td>
              <td>{$row["Sposób_rozwiązania"]}</td>
              <td>{$row["Koszty_łączne"]}</td>
              <td>{$row["Przyjmująca_zamówienie"]}</td>
              <td>{$row["Osoba_odpowiedzialna_nr_1"]}</td>
              <td>{$row["Osoba_odpowiedzialna_nr_2"]}</td>
              <td>{$row["Osoba_odpowiedzialna_nr_3"]}</td>
              <td>{$row["Osoba_przyjmująca"]}
              </td></tr>";
          }


      }

    $conn->close();


}else{

}
}
?>

enter image description here

1 个答案:

答案 0 :(得分:0)

您必须正确使用ajax

 $(document).ready(function(){
            $('#example').DataTable({
                'processing': true,
                'serverSide': true,
                'serverMethod': 'post',
                'ajax': {
                    'url':'force_ajaxfile.php',
                    data: {
                            source1:  "<?php echo $ser_off; ?>"
                        },
                },  'columns': [
                    { data: 'full_name' },
                    { data: 'service_email' },
                    { data: 'service_mobile' },
                    { data: 'service_name_record_office_address' },
                    { data: 'elector_type' },

                ]
            });

Force-ajaxfiles.php

    $serv_id = $_POST['source1'];



$draw = $_POST['draw'];
$row = $_POST['start'];
$rowperpage = $_POST['length']; // Rows display per page
$columnIndex = $_POST['order'][0]['column']; // Column index
$columnName = $_POST['columns'][$columnIndex]['data']; // Column name
$columnSortOrder = $_POST['order'][0]['dir']; // asc or desc
$searchValue = $_POST['search']['value']; // Search value
## Search 
$searchQuery = " ";
if($searchValue != ''){
    $searchQuery = " and (full_name like '%".$searchValue."%' or 
        service_email like '%".$searchValue."%' or 
        service_mobile like'%".$searchValue."%' ) ";
}
## Total number of records without filtering
    $sql = "select count(*) as allcount from `personal-records`
        where `service_service_regment_id` = '$serv_id'";
    $sel = mysqli_query($connect,$sql);
    $records = mysqli_fetch_assoc($sel);
    $totalRecords = $records['allcount'];

    ## Total number of records with filtering
    $sel = mysqli_query($connect,"select count(*) as allcount from `personal-records`
                            WHERE 1 ".$searchQuery."
                            and `service_service_regment_id` = '$serv_id'");
    $records = mysqli_fetch_assoc($sel);
    $totalRecordwithFilter = $records['allcount'];

    ## Fetch records
    $empQuery = "select * from `personal-records`
                WHERE 1 ".$searchQuery."
                and  `service_service_regment_id` = '$serv_id'
                order by ".$columnName." ".$columnSortOrder."
                limit ".$row.",".$rowperpage;
    $empRecords = mysqli_query($connect, $empQuery);
    $data = array();
while ($row = mysqli_fetch_assoc($empRecords)) {


    $data[] = array(
            "full_name"=>$row['full_name'],
            "service_email"=> $row['service_email'],
            "service_mobile"=> $row['service_mobile'],
            "service_name_record_office_address"=> $row['id'],
            "elector_type"=>$row['addres']
        );
}
## Response
$response = array(
    "draw" => intval($draw),
    "iTotalRecords" => $totalRecords,
    "iTotalDisplayRecords" => $totalRecordwithFilter,
    "aaData" => $data
);

echo json_encode($response);