使用ajax将值从JS传递到PHP

时间:2019-01-15 13:58:43

标签: javascript php html

我正在尝试将JS的值传递给PHP,但是它不起作用。我找不到错误的原因。

这是JS代码:

function updateRecords(field_id) {
    // get values
    // Update the details by requesting to the server using ajax
    $.post("ajax/edit-field.php", {
            field_id: field_id
       },
        function readRecords (data, status) {
            // reload Users by using readRecords();
            readRecords();
        }
    );

}

这是PHP代码:

 if(isset($_POST["field_id"]))    
      {
       $field_id = $_POST["field_id"]; 
    }
     try {
            $stmt = $conn->prepare("SELECT field_id, description, corner_points, damming_level_distance_map, pipeline_distance_map, notes FROM fields where field_id = $field_id");
            $stmt->execute();
            $stmt->setFetchMode(PDO::FETCH_ASSOC);
            foreach($stmt->fetchAll() as $row)
            { }
        }

以下是错误消息:

Notice: Undefined variable: field_id in C:\ajax\edit-field.php on line 43
Catched exception at line 44 (code : 42601) : SQLSTATE[42601]: Syntax error: 7 ERROR: syntax error at end of input LINE 1: ..., pipeline_distance_map, notes FROM fields where field_id = ^

0 个答案:

没有答案