Checking dynamically populated data using ajax with user entered value

Discussion in 'PHP' started by Michelle04, Apr 11, 2020.

  1. Michelle04

    Michelle04

    Joined:
    Apr 11, 2020
    Messages:
    2
    Likes Received:
    0
    I am creating a script for creating invoices. Using ajax for populating database data on autosearch. But the products has a field called min_selling_price, user should not be able to put price below min_selling_price. As my entries are dynamic, i am not getting where and how to check it.

    Here is my form

    Code (Text):
    <div class="table-responsive">
                                 <table class="table table-active table-bordered table-sm">
                                 <thead class="thead-active"><tr>
                            <th><input class='check_all' type='checkbox' onclick="select_all()"/></th>
                                <th>Name</th>
                                <th>Description</th>
                                <th>UOM</th>  
                                <th>Price</th>

                                <th>Qty</th>

                                <th colspan="2"></th>
                            </tr> </thead>

                                <tr>
                          <td><input type='checkbox' class='case'/></td>
                         <td><input type="text" class="form-control form-control-sm" id="productname_1" name="productname[]" required style="width:120px;"></td>
                                   <input type="hidden" class="form-control" id="productcode_1" name="productcode[]">
                                    <td><textarea class="form-control form-control-sm" id="description_1" name="description[]"></textarea></td>


                                    <td><select name="uom[]" class="form-control form-control-sm" id="uom_1" style="width:80px;">
                                    <option value="">UOM</option>
                                    <?php $su1 = mysqli_query($con, "select * from uom");  while($su2 = mysqli_fetch_array($su1)) {
                                    $uoptions .= "<option value='". $su2['uom_name'] . "'>" .$su2['uom_name'] . "</option>";  
                                    ?>
                                    <option value="<?php echo $su2['uom_name']; ?>"><?php echo $su2['uom_name']; ?></option>
                                    <?php
                                    }
                                    ?>
                                    </select>

                                   </td>    
                                    <td><input type="text" class="form-control form-control-sm price" required id="price_1" name="price[]"></td>

                                    <td><input type="text" class="form-control form-control-sm quantity" required id="quantity_1" name="quantity[]"></td>

                                   <input type="hidden" class="form-control amount" id="amount_1" name="amount[]">

                                   <td><button type="button" class='btn btn-danger delete'>-</button></td>
                            <td><button type="button" class='btn btn-success addmore'>+ </button></td>
    </tr>


    </table>
    </div>
    <script type="text/javascript">

      var options1 = "<?= $uoptions; ?>";
    </script>

    <script type="text/javascript" src="js/auto.js"></script>
    auto.js

    Code (Java):
    $(".delete").on('click', function() {
        $('.case:checkbox:checked').parents("tr").remove();
        $('.check_all').prop("checked", false);
        check();
      });
    var i = $('table tr').length - 1;


      $(".addmore").on('click', function() {
        count = $('table tr').length - 1;
        var data = "<tr><td><input type='checkbox' class='case'/></td><td><input class='form-control form-control-sm' type='text' id='productname_" + i + "' name='productname[]' required /></td><input class='form-control' type='hidden' id='productcode_" + i + "' name='productcode[]'/><td> <textarea class='form-control form-control-sm' id='description_"+ i + "' name='description[]'></textarea></td><td><select class='form-control form-control-sm uom' id='uom_" + i + "' name='uom[]'><option value=''>UOM</option>" + options1 + "</select></td><td><input class='form-control form-control-sm price' required type='text' id='price_" + i + "' name='price[]'/></td><td><input class='form-control form-control-sm quantity' required type='text' id='quantity_" + i + "' name='quantity[]'/></td><input class='form-control amount' type='hidden' id='amount_" + i + "' name='amount[]'/></tr>";
        $('table').append(data);
        row = i;
         $('#productname_' + i).autocomplete({
          source: function(request, response) {
            $.ajax({
              url: 'ajax.php',
              dataType: "json",
              method: 'post',
              data: {
                name_startsWith: request.term,
                type: 'items_table',
                row_num: row

              },

              success: function(data) {
                response($.map(data, function(item) {
                  var code = item.split("|");

                  return {
                    label: code[0],
                    value: code[0],

                    data: item

                  }
                }));
              }
            });
          },

         autoFocus: true,
          minLength: 0,
          select: function(event, ui) {
            var names = ui.item.data.split("|");
            id_arr = $(this).attr('id');
            id = id_arr.split("_");
            $('#productcode_' + id[1]).val(names[1]);
            $('#description_' + id[1]).val(names[2]);
            $('#uom_' + id[1]).val(names[3]);
            $('#price_' + id[1]).val(names[4]);
            //$('#tax_' + id[1]).val(names[5]);

          }
        });


        i++;


      });

      function select_all() {
        $('input[class=case]:checkbox').each(function() {
          if ($('input[class=check_all]:checkbox:checked').length == 0) {
            $(this).prop("checked", false);
          } else {
            $(this).prop("checked", true);
          }
        });
      }




      function check() {
        obj = $('table tr').find('span');
        $.each(obj, function(key, value) {
          id = value.id;
          $('#' + id).html(key + 1);
        });
      }

    $('#productname_1').autocomplete({
        source: function(request, response) {
          $.ajax({
            url: 'ajax.php',
            dataType: "json",
            method: 'post',
            data: {
              name_startsWith: request.term,
              type: 'items_table',
              row_num: 1
            },
            success: function(data) {
              response($.map(data, function(item) {
                var code = item.split("|");
                return {
                  label: code[0],
                  value: code[0],
                  data: item
                }
              }));
            }
          });
        },
       autoFocus: true,
        minLength: 0,
        select: function(event, ui) {
          var names = ui.item.data.split("|");
           $('#productcode_1').val(names[1]);
          $('#description_1').val(names[2]);
          $('#uom_1').val(names[3]);
          $('#price_1').val(names[4]);

        }

      });
    ajax.php

    PHP:
    if($_POST['type'] == 'items_table'){
        $row_num = $_POST['row_num'];
        $name = $_POST['name_startsWith'];
        $query = "SELECT * FROM items WHERE status='Active' AND name LIKE '".strtoupper($name)."%'";

        $result = mysqli_query($con, $query);
        $data = array();
        while ($row = mysqli_fetch_assoc($result)) {
            $name = $row['name'].'|'.$row['item_id'].'|'.($row['description']).'|'.$row['uom'].'|'.$row['selling_price'].'|'.$row_num;

            array_push($data, $name);  
        }  
        header('Content-Type: application/json');
        echo json_encode($data);
    }
    in items table, i have a filed 'min_selling_price' , i want to check for each line item whether the user entered price is above , if not it should not allow to proceed. Not getting how to do it?
     
    Michelle04, Apr 11, 2020
    #1
    1. Advertisements

  2. Michelle04

    sunfighter

    Joined:
    Nov 27, 2019
    Messages:
    109
    Likes Received:
    14
    You should check current offer against the minimum price during the buyer offer. not on the invoice. buyer should be told their bid is rejected when they enter it.
     
    sunfighter, Apr 11, 2020
    #2
    1. Advertisements

  3. Michelle04

    Michelle04

    Joined:
    Apr 11, 2020
    Messages:
    2
    Likes Received:
    0
    Yes. I am developing it for my client and he needs it in this way only. His office staff will enter the data so it should gets checked.
     
    Michelle04, Apr 11, 2020
    #3
  4. Michelle04

    sunfighter

    Joined:
    Nov 27, 2019
    Messages:
    109
    Likes Received:
    14
    I am on a number of sites. I am not going to answer you on more then one. It is considered bad manners to post the same question on multiple sites. People don't like it and will stop giving help if it continues. "I have spoken."
     
    sunfighter, Apr 11, 2020
    #4
  5. Michelle04

    rajeshmasiwaltechgeek

    Joined:
    Apr 11, 2020
    Messages:
    1
    Likes Received:
    0
    Location:
    India
    I am not able to figure out the problem, from this post.
     
    rajeshmasiwaltechgeek, Apr 11, 2020
    #5
  6. Michelle04

    sunfighter

    Joined:
    Nov 27, 2019
    Messages:
    109
    Likes Received:
    14
    OK. No problem. I take it the price is the 5th cell, but nowhere is there a minimum accepted price. Nor is there a accept/reject notification cell.

    PS. You should man up and tell your contract the mistake he's making doing things this way. Not only about what I said before but hand entering data costs money and is open for mistakes. This process can and should be automated and you can get the job doing it.
     
    sunfighter, Apr 12, 2020
    #6
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.