1

In laravel, I'm trying to update several tables and rows. i have items that are to be received and checked by 2 different users.

in my show.blade.php, i have this verify button and submit button depending on the user.

@if (($current_user_id  != $saved_receiver_id) && ($saved_receiver_id != $not_yet_received))    

    <div class="row padder m-b">
        <div class="col-md-12">
            <label for="received_by" class="pull-left" >Received by:</label> <span class="fa"></span>
            <input type="hidden" name="receiver_id" value="{{ $saved_receiver_id }}" >{{ $receiver_username }}</input>
        </div>
    </div>
    <div class="row padder m-b">
        <div class="col-md-12">
            <label for="received_date" class="pull-left" >Received date:</label> <span class="fa"></span>
            <input type="hidden" name="received_date" value="{{ $received_date }}" >{{ $received_date }}</input>
        </div>
    </div>
    <input type="hidden" name="purchase_orders_id" value="{{ $purchase_order_number }}">
    <input type="hidden" name="checker_id" value="{{ $current_user_id }}">
    <div class="row padder m-b">
        <div class="col-md-12">
            <label for="final_checker_remarks" class="pull-left" >Final Remarks</label>
            <textarea class="form-control col-md-12" name="final_checker_remarks" value="{{ $final_checker_remarks }}" id="final_checker_remarks">{{ $final_checker_remarks }}</textarea>
        </div>
    </div>
    <br />
    <div class="row">
        <div class="col-md-12">
            <button type="button" class="pull-right btn btn-success btn-sm submit-btn" id="update-form-submit" data-action="verified">Verified</button>
        </div>
    </div>
    @else
    <div class="pull-right">
        <div class="btn-group">
            <button type="button" class="btn btn-info btn-sm submit-btn" id="update-form-submit" data-action="submit">Submit List</button>
        </a>
    </div>
@endif

now in my ReceivesController.php, i have this postUpdate function,

public function postUpdate(Request $request)
{
    if (! $request->ajax()) {
        abort(404);
    }

    $items = json_decode($request->items);
    $action = json_decode($request->action);        
    if(!count($items) && !count($items->purchase_item_id)){
        return false;
    }

    $cnt = count($items->purchase_item_id);     
// Receiver Submit function 
    if ($action == "submit") {

// Saves the received id of the one who received to purchase order table        
        DB::table('purchase_orders')
        ->where('id', $items->purchase_orders_id)
        ->update([
            'receiver_id'       =>  $items->receiver_id,

        ]);


// Saves the quantity received and receiver remarks to purchase items table 
        for($i=0; $i<$cnt; $i++){
            DB::table('purchase_items')
            ->where('id', $items->purchase_item_id[$i])
            ->update([
                    'quantity_received' =>  $items->quantity_received[$i], 
                    'receiver_remarks'  =>  $items->receiver_remarks[$i],
                ]);
        }   
// Items Received Success Message           
        $message = 'Items Received';
    } 
// QA or Checker Finalize function  
    else {

// Saves the checker id, and final checker remarks of the one who made the QA to purchase order table   
        DB::table('purchase_orders')
        ->where('id', $items->purchase_orders_id)
        ->update([
            'checker_id'            =>  $items->checker_id,
            'final_checker_remarks' =>  $items->final_checker_remarks,
        ]);
// Saves the quality received and checker remarks to purchase items table   
for($i=0; $i<$cnt; $i++){                       

            $quality_received = $items->quality_received;
            if(is_array($items->quality_received)){
                $quality_received = $items->quality_received[$i];
            }

            $checker_remarks = $items->checker_remarks;
            if(is_array($items->checker_remarks)){
                $checker_remarks = $items->checker_remarks[$i];
            }

            $quantity_received = $items->quantity_received;
            if(is_array($items->quantity_received)){
                $quantity_received = $items->quantity_received[$i];
            }

            $receiver_remarks = $items->receiver_remarks;
            if(is_array($items->receiver_remarks)){
                $receiver_remarks = $items->receiver_remarks[$i];
            }


            DB::table('purchase_items')
            ->where('id', $items->purchase_item_id[$i])
            ->update([
                    'quality_received'  =>  $quality_received, 
                    'checker_remarks'   =>  $checker_remarks,                       
                    'quantity_received' =>  $quantity_received, 
                    'receiver_remarks'  =>  $receiver_remarks,
                ]);
// Increments or Adds the quantity received to items table  
            DB::table('items')
            ->where('purchase_items.id', $items->purchase_item_id[$i])
            ->join('purchase_items', 'items.id', '=', 'purchase_items.item_id')
            ->increment('items.measurement', $items->quantity_received[$i]);

        }
/   / Items Finalized Success Message       
        $message = 'Items Verified';
    }
// Returns Success Message 
    return response()->json([
            'success' => true,
            'message' => $message
        ]);
}

Now my problem is only the first letter of the word that is typed in the input area are being saved, and in others they are not saved, however, in other, it can be saved. I know its weird, but i cant find which part of my codes is doing such result, what is it that i need to do for me to update my tables correctly? Thanks in advance for the help.

Update: Here is my receive-form-function.js

    /* ========================================================================
 *  Initialize Pages
 * ======================================================================== */
    $(initialPages);

/* ========================================================================
 *  Major function
 * ======================================================================== */

/* ==== function to init this page === */
function initialPages($) {

    // if($('#receives-list-table').length){
    //  DataTables("#receives-list-table", "receives");
    // }

    if($('#receiveItems-list-table').length){
            $("#receiveItems-list-table").DataTable({
            responsive: true,
            ordering: false,
        });
    }

    $('#update-form-submit').on('click', function(){
        var action = $(this).data('action');
        updateReceiveItem(action);
    });
    clearInputs();
}

/* === dataTables === */
function DataTables(selector, controller) {
    $(selector).DataTable({
        responsive: true,
        processing: true,
        serverSide: true,
        ajax: url+'/'+controller+'/paginate'
    });
}

function updateReceiveItem(action){
    loadingModal('show','Updating ....');
    ajaxCsrfToken();

    var data = $('#receiveItems_id').serializeArray();
        data = JSON.stringify(data);
        // data = JSON.parse(data);
        data = JSON.stringify($('#receiveItems_id').serializeObject());
        // data = $('#receiveItems_id').serializeObject();

    $.ajax({
            url: url+'/receives/update',
            type: 'post',
            data: {'items':data, 'action': action},
            dataType: 'json',
            complete: function() {      
                loadingModal('close');
            },
            error: function(result) {

            },
            success: function(result) {

                successAlert('#receiveItems-result', result.message);
            // if (result.success) {

            //      $('input, select, textarea').attr('disabled', true);

            //  } else {
            //      alert(result.message);
            //  }
            }

        });
        console.log(data);
        return false;
}
/**
 * Use to format serialize data and convert to json data
 *
 * Usage: JSON.stringify($('form').serializeObject())
 */
$.fn.serializeObject = function() {
    var o = Object.create(null),
        elementMapper = function(element) {
            element.name = $.camelCase(element.name);
            return element;
        },
        appendToResult = function(i, element) {
            var node = o[element.name];

            if ('undefined' != typeof node && node !== null) {
                o[element.name] = node.push ? node.push(element.value) : [node, element.value];
            } else {
                o[element.name] = element.value;
            }
        };

    $.each($.map(this.serializeArray(), elementMapper), appendToResult);
    console.log(o);
    return o;
};
19
  • for all those rows that were chosen to be updated. Commented Feb 4, 2016 at 9:45
  • what are you getting when u dump simple value? Commented Feb 4, 2016 at 9:46
  • when i enter a word in an input, for example: hello the only one saved in the table is letter h. Commented Feb 4, 2016 at 9:49
  • once $items = json_decode($request->items); response back before saving to the database and see what are you gettin Commented Feb 4, 2016 at 9:50
  • now, when i have 3 rows being updated, it doesnt work anymore. im getting this error: QueryException in Connection.php line 636: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'where purchase_items.id is null' at line 1 (SQL: update items inner join purchase_items on items.id = purchase_items.item_id set items.measurement = items.measurement + where purchase_items.id is null) Commented Feb 4, 2016 at 9:51

1 Answer 1

1

my $.fn.serializeObject = function() above seems to have the bug, i tried using another $.fn.serializeObject = function(), and it gave me the json object that i want. here is the $.fn.serializeObject = function() that i am using now.

$.fn.serializeObject = function()
{
    var o = {};
    var a = this.serializeArray();
    $.each(a, function() {
        if (o[this.name] !== undefined) {
            if (!o[this.name].push) {
                o[this.name] = [o[this.name]];
            }
            o[this.name].push(this.value || '');
        } else {
            o[this.name] = this.value || '';
        }
    });
    return o;
};
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.