使用ajax

时间:2019-01-13 15:32:53

标签: php jquery html ajax forms

我正在使用ajax和jquery构建一些表的数据输入表单。

insert查询对于所有表都工作正常。但是对于一张需要将数据添加到一个字段中的表,因为通过表单输入减去两个值是不起作用的。

可能我在如何捕获js中的值,操纵它们并调用php插入脚本时犯了一些错误。我尝试了很多机器人都没有成功。

此处是输入的表单代码。 “ Point-Height”和“ Adjustment Value”这两个字段用于获取值以获取Installation-Height = Point-Height - Adjustment Value并在数据库中添加Installation-Height

    <div>
     <div class="col-md-4 mb-3">
      <label for="validationCustom02">Point Height</label>
      <input type="number" step="0.01" class="form-control" name="point_height" id="point_height" placeholder="insert Point Height" value="0.00" required>
      <div class="valid-feedback">
        0.00
      </div>
    </div>
      <div class="col-md-4 mb-3">
      <label for="validationCustom02">Adjustment Value</label>
      <input type="number" step="0.01" class="form-control" name="logger_height" id="logger_height" placeholder="insert Logger Height" value="0.00" required>
      <div class="valid-feedback">
        0.00
      </div>
    </div>
    <div class="col-md-4 mb-3">
      <label for="validationCustom02">Logger Length</label>
      <input type="number" step="0.01" class="form-control" name="device_dimension" id="device_dimension" placeholder="insert Device Dimension" value="0.00" required>
      <div class="valid-feedback">
        0.00
      </div>
    </div>
      <div class="col-md-4 mb-3">
      <label for="validationCustom02">Notes</label>
      <input type="text" class="form-control" name="notes" id="notes" placeholder="insert notes" value="" required>
      <div class="valid-feedback">
        Looks good!
      </div>
    </div>
   </div>
<button class="btn btn-primary btn-sm" name="submit6" type="submit" onclick="insertMeasuringDevices()">Insert Records</button>

这里是js,用于从表单中读取值,减去(point height-logger_height),然后调用php将值插入数据库。

function insertMeasuringDevices() {
    // get values
        var point_height = $("#point_height").val();
        var logger_height = $("#logger_height").val();
        var notes = $("#notes").val();
        var installation_height = point_height-logger_height; //subtracting 
        var device_dimension = $("#device_dimension").val();

    // Add record
        $.post("ajax/insertMeasuringDevices.php", {
            notes: notes,
            installation_height: installation_height,
            device_dimension: device_dimension,
        }, function (data, status) {
            // close the popup
            // $("#add_new_record_modal").modal("hide");

            // read records again
            //    readRecords();
            /*
             // clear fields from the popup
             $("#first_name").val("");
             $("#last_name").val("");
             $("#email").val("");
            */
       });
}


// READ records after insert
function readSensors() {
    $.get("ajax/readMeasuringDevices.php", {}, function (data, status) {
        $(".MeasuringDevices_content").html(data);
    });
} 

这是php insertt表单,用于从js接收数据以将值插入db。

    <?php

            if(isset($_POST["submit6"])){

                  include("ajax/connection.php");

                try {

                        $sql = "INSERT INTO measuring_devices (notes, installation_height, logger_length)
                        VALUES ('".$_POST["notes"]."','".$_POST["installation_height"]."','".$_POST["device_dimension"]."')";
                        //echo "<meta http-equiv='refresh' content='0'>";
                        if ($conn->query($sql)) {
                            echo "<script type= 'text/javascript'>alert('New Record Inserted Successfully');</script>";
                            }
                            else{
                               echo "<script type= 'text/javascript'>alert('Data not successfully Inserted.');</script>";
                              }

                               $dbh = null;
                          }
                           catch(PDOException $e)
                           {
                               echo $e->getMessage();
                               exit('<b>Catched exception at line '. $e->getLine() .' (code : '. $e->getCode() .') :</b> '. $e->getMessage());
                           }

                      }   

                        //$conn = null;

                        ?>

1 个答案:

答案 0 :(得分:1)

您不要将post6发送到您的php ajax文件中,因此if(isset($ _ POST [“ submit6”]))返回false,它会跳过插入代码。 您应如下更改您的条件:

if(isset($_POST["notes"])) 

或发送带有ajax帖子的submit6。