-1

I am building a data-input form using ajax & jquery for few tables.

The insert query is working fine for all the tables. But for one table where I need to add data into one field as a subtraction of two values coming through form input is not working.

Probably I am making some mistake how to catch the values in js, manipulate them, and call php insert script. I tried a lot bot no success.

Here is input form code. The two fields 'Point-Height' AND 'Adjustment Value' are to get values to get Installation-Height = Point-Height - Adjustment Value and add Installation-Height in the database

    <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>

Here is js for reading values from form, subtract (point height-logger_height), and call php to insert values into database.

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

Here is php insertt form to receive data from js to insert values into 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;

                        ?>
Jeff
  • 6,895
  • 1
  • 15
  • 33
XCeptable
  • 1,247
  • 6
  • 25
  • 49
  • _sidenote:_ those `echo " – Jeff Jan 13 '19 at 15:56
  • can you plz see the js function where I am trying to get value by subtraction. The error is 'Undefined index: installation_height in insertMeasuringDevices.php'. Is it right way ? The two values point-Height & Logger Height are the fields to get 2 values. Their subtraction is a field in db 'Installation_Height' that I am trying to input data. – XCeptable Jan 13 '19 at 16:06
  • 1
    **Warning:** You are wide open to [SQL Injections](http://php.net/manual/en/security.database.sql-injection.php) and should really use parameterized [Prepared Statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) instead of manually building your queries like that. Specially since you're not escaping the user inputs at all! – Dharman Jan 13 '19 at 16:20

1 Answers1

1

Your don't send submit6 in post to your php ajax file, so if(isset($_POST["submit6"])) return false, and it skips the insert code. You shall change your if as below:

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

or send submit6 with the post ajax.

Batsheva Hansav
  • 316
  • 2
  • 11
  • Basically this code is working for me for few other forms in same file. The conversion here start trouble. i.e. I am getting two values through form(point_height & Logger_Height), catch them in js ajax function and subtract those to get 3rd value i.e. Installation_Height to insert into database. – XCeptable Jan 13 '19 at 16:11
  • Try to parse the values before subtracting. – Batsheva Hansav Jan 13 '19 at 16:17
  • Before passing installation_height can you debbug in your browser and tell me wich value it has? – Batsheva Hansav Jan 13 '19 at 16:19
  • Thank you for the help all. Basically I make it working by instead of subtracting values in js, I received values as is in js and pass to php insert. In php, before inserting, I did the subtraction and inserted the it into query. There was some trouble the way I was trying to do subtraction in js as an empty 'Installation_Height' was passed one from it to php after subtraction. – XCeptable Jan 13 '19 at 16:34
  • I am glad to help:) I guess it's because your value were float numbers and js couldn't do the subtraction. but anyway I am glad for you it works no matter how. my advice - I prefer doing all my calculations in the server side. – Batsheva Hansav Jan 13 '19 at 17:27