theUnforgiven
4 years ago
45
37
General

Laravel, Ajax, Post updating

Posted 4 years ago by theUnforgiven

Hi all,

I'm a newbie when it comes to ajax and jQuery, but having a go at updating a row in my database on form submit which is returning a 500 Internal Server Error

Here's the form:

{{ Form::model($product, ['method' => 'PUT', 'route' => ['admin.products.update', $product->id], 'class' => 'form-horizontal','id' => 'editForm', 'files' => true]) }}



            Product Name
                 
                     name }}">
                 
            



            <div class="form-group">
            <div class="col-sm-2">Category</div>
                 <div class="col-lg-3">
                 <?php $selected_category = ($product->slug ?: null) ?>
                    {{ Form::select('category_id', $category, $selected_category, ['class' => 'form-control'])}}

                 </div>
            </div>



            @foreach($options as $option)
               id }}" id="option_id">
               Size
               
                   size; ?>
                   {{ Form::select('size', $sizes, $selected_size, ['class' => 'form-control', 'id' => 'size'])}}
               

              Colour
               
               colour; ?>
                   {{ Form::select('colour', $colours, $selected_colour, ['class' => 'form-control', 'id' => 'colour'])}}
               
               Stock
                
                    stock }}" id="stock">
                
            @endforeach
           



            <div class="form-group">
            <div class="col-sm-2">Status</div>
                 <div class="col-lg-3">
                  <?php $options = array('Inactive', 'Active'); ?>
                  {{ Form::select('status', $options, Input::old('status'), array('class' => 'form-control')) }}
                 </div>
            </div>

            <div class="form-group">
                 <div class="col-lg-3">
                     <button class="btn btn-success">Update</button>
                 </div>
            </div>

   {{ Form::close() }}

Here's the ajax

 $("document").ready(function(){
        var base_url = 'http://localhost';

        $("#editForm").submit(function(e){
            e.preventDefault();
            var option_id = $("input#option_id").val();
            var size = $("input#size").val();
            var colour = $("input#colour").val();
            var stock = $("input#stock").val();

            var dataString = 'size='+size+'&amp;optionID='+option_id+'&amp;stock='+stock+'&amp;colour='+colour;
            $.ajax({
                type: "POST",
                url : base_url + "/admin/updateProductOption",
                data : dataString,
                success : function(data){
                    console.log(data);
                }
            },"html");

    });
 });//end of document ready function

here is the method to handle the update.

public function updateProductOption()
    {
        $data = Input::all();
        if(Request::ajax())
        {
            $id = Input::get('id');
            $option = Options::where('id', $id)->first();
            $option->size = $data['size'];
            $option->colour = $data['colour'];
            $option->stock = $data['stock'];
            $option->update();
        }
    }

But it's not updating and throwing a 500 error. Anyone have any idea's/advice help to give on this please ?

Please sign in or create an account to participate in this conversation.