0

I'm new to ajax and hoping someone can help figure out how to add two form inputs. I have a hidden input that is getting the total sheets from a database. I also have an addSheets input that is getting the total of sheets to add. I'm posting code from my modal as well as the ajax and action. Any advice is greatly appreciated.

(index)
<html>
<body>
<div class="modal fade" id="addSheetsModal">
   <div class="modal-dialog modal-dialog-centered">
      <div class="modal-content">
         <!-- Modal Header -->
         <div class="modal-header">
         <h4 class="modal-title">Add Paper</h4>
         <button type="button" class="close" data-dismiss="modal">&times;</button>
         </div>
         <!-- Modal body -->
         <div class="modal-body px-4">
                    <form action="" method="post" id="form-addSheets-data">
                        <input type="hidden" name="id" id="id">
                        <input type="hidden" name="sheets" id="sheets">
                        <div class="form-group">
                            <input type="text" name="addSheets" class="form-control" id="addSheets" placeholder="">
                        </div>
                        <div class="form-group">
                            <input type="submit" name="addSheetQty" id="addSheetQty" value="Add Paper" class="btn btn-success btn-block">
                        </div>
                    </form>
                </div>   
            </div>
            </div>
        </div>
</body>
</html>

<script language="javascript" type="text/javascript">

// AJAX Edit Paper - Receive (Gets ID)
                    $("body").on("click", ".receiveBtn", function(e){
                        e.preventDefault();
                        r_edit_id = $(this).attr('id');
                        $.ajax({
                            url:"action.php",
                            type:"POST",
                            data:{r_edit_id:r_edit_id},
                            success:function(response){
                                data = JSON.parse(response);
                                console.log(data);
                                $("#id").val(data.id);
                                $("#sheets").val(data.sheets);
                            }
                        });
                    });

                    // Edit Paper - Update (Sums)
                    $("#addSheetQty").click(function(e){
                        if($("#form-addSheets-data")[0].checkValidity()){
                            e.preventDefault();
                            $.ajax({
                                url:"action.php",
                                type:"POST",
                                data: $("#form-addSheets-data").serialize()+"&action=addSheetQty",
                                success:function(response){
                                    console.log(response);
                                    Swal.fire({
                                        title: 'Sheets added successfully!',
                                        icon: 'success',
                                        showConfirmButton: false,
                                        timer: 1500
                                    })
                                    $("#addSheetsModal").modal('hide');
                                    $("#form-addSheets-data")[0].reset();
                                    
                                    showAllPapers();
                                }
                            });
                        }
                    });
</script>

<?php

// ACTION
if(isset($_POST['r_edit_id'])){
    $id = $_POST['r_edit_id'];
    $row = $db->getPaperById($id);
    echo json_encode($row);
}


if(isset($_POST['action']) && $_POST['action'] == "addSheetQty"){
    $id = $_POST['id'];
    $sheets = $_POST['sheets'] + $_POST['addSheets'];
    $db->updateSheetQty($id,$sheets);
}

?>

In the database the sheets column is varchar(255). In the php_error.log I'm getting this response: PHP Warning: A non-numeric value encountered in /Applications/MAMP/htdocs/PaperInventory/action.php on line 116 which is this line ($sheets = $_POST['sheets'] + $_POST['addSheets'];) in my code above.

Thanks for any input and help understanding this.

  • take a look at this: https://stackoverflow.com/questions/42044127/warning-a-non-numeric-value-encountered/42044413 – brunobraga Dec 20 '21 at 18:46
  • Thank you! I figured it out. I converted my varchar sheets column to an int, updated my form input types to number for sheets and addSheets, my name attributes in my modal and all is working. :) – user3629984 Dec 20 '21 at 19:22

0 Answers0