Cannot retrieve posted data from vue.js ajax call - codeigniter

I'm giving a try to Vue.js, It's much simpler than Angular and then I stuck on this problem. I'm using CodeIgniter for the backend process. Basically I just want to send data using ajax call via Vue.js then I want to retrieve the data in mmy controller. But I can't retrive the data using $this->input->post().
Here is my view. I'm using form on a modal.
<div class="modal fade modal-primary" id="modalObat" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title">Tambah Obat</h4>
</div>
<form #submit.prevent="onSubmit" class="form-horizontal">
<div class="modal-body">
<div class="form-group">
<label for="nama-obat" class="col-sm-2 control-label">Nama Obat</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="namaObat" placeholder="nama obat" v-model="newObat.nama">
</div>
</div>
<div class="form-group">
<label for="harga" class="col-sm-2 control-label">Harga</label>
<div class="col-sm-10">
<input type="number" class="form-control" id="harga" placeholder="harga" v-model="newObat.harga">
</div>
</div>
<div class="form-group">
<label for="Stok" class="col-sm-2 control-label">Stok</label>
<div class="col-sm-10">
<input type="number" class="form-control" id="stok" placeholder="jumlah" v-model="newObat.stok">
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-outline pull-left" data-dismiss="modal">Close</button>
<button type="submit" id="simpen" class="btn btn-outline">Save Changes</button>
</div>
</form>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
This is how I send the data on vue
Then here is my controller
public function tambahObat()
{
$data = array(
'nama' => $this->input->post('nama'),
'harga' => $this->input->post('harga'),
'stok' => $this->input->post('stok')
);
$data1 = $this->input->post('newObat');
$query = $this->obat_m->save($data, null);
if( $query == true )
{
$message = $this->session->set_flashdata('message', 'berhasil menyimpan');
}
else
{
$message = $this->session->set_flashdata('message', 'berhasil mengupdate');
}
redirect('obat');
}
I succeed to send the data as I check on the console
I think there is a problem with the controller but I dont know why. Could you help me, please??

You are trying to access this from inside the post callback, where this refers to something other than your vue instance. Try this format:
this.$http.post('url/to/post',{},function(){
//because of "bind", 'this' is vue
}.bind(this));

Related

How do I solve the modal update issues in Laravel?

I have a modal in which I want to update my Question but the modal keeps updating the same question so can someone help me because my modal keeps taking the same $id even if I click on different entries.
This is my QuestionController
public function update($questionId)
{
$this->validate(request(),[
'label' => 'required',
]);
$data = request()->all();
$question = Question::find($questionId);
$question->label = $data['label'];
$question->save();
return redirect('/question');
}
this is my Modal in the blade.php file
<!-- Modal Edit -->
<div class="modal" id="editMcqModal" tabindex="-1" role="dialog"
aria-labelledby="editMcqModalLabel" aria-hidden="true">
<div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalCenteredLabel">Edit Question</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form class="" action="/question/{{$question->id}}" method="post">
#csrf
#method('PATCH')
<div class="form-group">
<textarea name="label" class="form-control" placeholder="Please Enter Question" cols="5"
row="15">{{$question->label}}</textarea>
</div>
<div class="form-group">
<button type="submit" name="add" class="btn btn-success float-right">Update
Question</button>
</div>
</form>
</div>
</div>
</div>

How to retrieve uploaded file edit view blade using Laravel

I have a project in Laravel-5.8 for upload of files like excel and images.
I have successfully inserted it. I saved the file name in the table and the file itself in a directly. Where I have a problem is how to retrieve the file in the edit view blade.
Controller
public function update_employee_mid_year_comment(UpdateSelfReviewRequest $request, $id) {
$goal = Goal::find($id);
$goal->employee_mid_year_comment = $request->employee_mid_year_comment;
if ($request->employee_mid_year_attachment != "") {
$employee_mid_year_attachment = $request->file('employee_mid_year_attachment');
$new_name = rand() . '.' . $employee_mid_year_attachment->getClientOriginalExtension();
$employee_mid_year_attachment->move(public_path('storage/documents/mid_year'), $new_name);
$goal->employee_mid_year_attachment = $new_name;
}
$goal->save();
DB::commit();
Session::flash('success', 'Comment is Successfully Updated');
return redirect()->back();
}
I am using a modal form:
edit.blade
<div class="modal fade" id="edit{{ $goal->id }}" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<form action="{{route('mid_year_setups.update_employee_mid_year_comment',['id'=>$goal->id])}}" method="post" enctype="multipart/form-data" id="edit_comment-form">
{{ csrf_field() }}
<div class="modal-header">
Update Self-Review Comment
</div>
<div class="col-md-12">
<div class="form-group">
<label class="control-label">Comment:<span style="color:red;">*</span></label>
<textarea rows="2" name="employee_mid_year_comment" class="form-control" placeholder="Enter Comment here" value="{{old('employee_mid_year_comment',$goal->employee_mid_year_comment)}})}}" required data-validation-required-message="This field is required">{{old('employee_mid_year_comment',$goal->employee_mid_year_comment)}}</textarea>
</div>
</div>
<div class="col-md-12">
<div class="form-group">
<label class="control-label"> Attachment:</label>
<div class="custom-file">
<div class="custom-file">
<input value="{{old('employee_mid_year_attachment',$goal->employee_mid_year_attachment)}}" type="file" name="employee_mid_year_attachment" class="custom-file-input" id="customFile">
<label class="custom-file-label" for="exampleInputFile">Choose file</label>
</div>
</div>
</div>
</div>
<div class="modal-footer">
<button type="submit" id="edit_comment_btn-submit" class="btn btn-success btn-ok">Save</button>
</div>
</form>
</div>
</div>
</div>
The file name is employee_mid_year_attachment while the file path is: storage/documents/mid_year
When I rendered the edit view blade, it didn't retrieve the attached document. How do I achieve this?
Thanks
use in value input path to file
<input
value="{{asset('storage/documents/mid_year/'.$goal>employee_mid_year_attachment')}}"
type="file" name="employee_mid_year_attachment" class="custom-file-input"
id="customFile">

Cannot get value from summernote ,when using ajax update

when i click edit button (i used modal bootstrap) all value exist except textarea with summernote.
if you input something(in summernote) and cancel it ,your value doesn't disappear ... it should be clear .
forgive me, my english so bad .
here is my modal form :
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close" ><span aria-hidden="true">×</span></button>
<h3 class="modal-title">Formulir Berita</h3>
</div>
<div class="modal-body form">
<form action="#" id="form" class="form-horizontal">
<input type="hidden" value="" name="id_berita"/>
<div class="form-body">
<div class="form-group">
<label class="control-label col-md-2">Tanggal penulisan</label>
<div class="col-md-9">
<div class="input-group">
<span class="input-group-addon">
<i class="fa fa-calendar"></i>
</span>
<input name="tgl" placeholder="yyyy-mm-dd" class="form-control datepicker" type="text">
<span class="help-block"></span>
</div>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-2">Judul</label>
<div class="col-md-9">
<input name="judul" placeholder="Judul" class="form-control" type="text">
<span class="help-block"></span>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-2">Penulis</label>
<div class="col-md-9">
<input name="penulis" placeholder="Penulis" class="form-control" type="text">
<span class="help-block"></span>
</div>
</div>
<div class="form-group" id="photo-preview">
<label class="control-label col-md-2">Gambar</label>
<div class="col-md-4">
(Tidak ada gambar)
</div>
</div>
<div class="form-group">
<label class="control-label col-md-2" id="label-photo">Unggah Foto </label>
<div class="col-md-7">
<div class="fileupload fileupload-new" data-provides="fileupload">
<div class="input-append">
<div class="uneditable-input">
<i class="fa fa-file fileupload-exists"></i>
<span class="fileupload-preview"></span>
</div>
<span class="btn btn-default btn-file">
<span class="fileupload-exists">Ganti Foto</span>
<span class="fileupload-new">Pilih File</span>
<input name="gambar" type="file" />
</span>
<span class="help-block"></span>
Remove
</div>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-2">Isi</label>
<div class="col-md-9">
<textarea name="isi" class="form-control" id="summernote" >
</textarea>
</div>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" id="btnSave" onclick="save()" class="btn btn-primary">Simpan</button>
<button type="button" class="btn btn-danger" data-dismiss="modal">Batal</button>
</div>
</div>
</div>
</div>
i 've been trying some code :
$('#summernote').summernote('code');
$('#summernote').summernote('reset');// and for resetting modal while Add Data
it doesn't happen anything
my ajax function :
function edit_berita(id)
{
save_method = 'update';
$('#form')[0].reset(); // reset form on modals
$('.form-group').removeClass('has-error'); // clear error class
$('.help-block').empty(); // clear error string
//Ajax Load data from ajax
$.ajax({
url : "<?php echo site_url('sistem/berita/ajax_edit/')?>/" + id,
type: "GET",
dataType: "JSON",
success: function(data){
// $('#summernote').summernote('code');
$('[name="id_berita"]').val(data.id_berita);
$('[name="tgl"]').datepicker('update',data.tgl);
$('[name="judul"]').val(data.judul);
$('[name="isi"]').val(data.isi);
$('[name="penulis"]').val(data.penulis);
$('#modal_form').modal('show'); // show bootstrap modal when complete loaded
$('.modal-title').text('Edit data'); // Set title to Bootstrap modal title
$('#photo-preview').show(); // show photo preview modal
if(data.gambar)
{
$('#label-photo').text(''); // label photo upload
$('#photo-preview div').html('<img src="'+base_url+'upload/berita/'+data.gambar+'" class="img-responsive" >'); // show photo
$('#photo-preview div').append('<input type="checkbox" name="remove_photo" value="'+data.gambar+'"/> Remove photo when saving'); // remove photo
}
else
{
$('#label-photo').text(''); // label photo upload
$('#photo-preview div').text('(Tidak ada gambar)');
}
},
error: function (jqXHR, textStatus, errorThrown)
{
alert('Error get data from ajax');
}
});
}
Text Area doesnot have value. jQuery .html() works in this case
$("textarea#summernote").html(data.isi);
Try to modify the summernote line like this :
$('#summernote').summernote('code', data.isi);
And to clear the content :
$('#summernote').summernote('code', '');
Please try this:
$("textarea#summernote").val(data.isi);
$( '[name="isi"]' ).val(data.isi);
$( '[name="isi"]' ).summernote();

Laravel file upload from modal is returning blank

i have created a modal for file uploading, this is a client request, so i need to do it this way, i have already this:
{{-- Single Take Photo modal --}}
<div class="modal modal-success fade" tabindex="-1" id="takephoto_modal" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="{{ __('voyager::generic.close') }}"><span aria-hidden="true">×</span></button>
<h4 class="modal-title"><i class="voyager-photo"></i> Tomar Foto</h4>
</div>
<div class="modal-footer">
<form action="#" id="takephoto_form" method="POST" enctype="multipart/form-data" method="post" files="true">
{{ csrf_field() }}
<input type='file' accept="image/x-png, image/jpeg" capture="camera"/>
<input type="submit" class="btn btn-success pull-right" value="Tomar Foto">
</form>
<button type="button" class="btn btn-default pull-right" data-dismiss="modal">{{ __('voyager::generic.cancel') }}</button>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
Now, the js:
$('td').on('click', '.takephoto', function (e) {
$('#takephoto_form')[0].action = '{{ route('voyager.'.$dataType->slug.'.submitphoto', ['id' => '__id']) }}'.replace('__id', $(this).data('id'));
$('#takephoto_modal').modal('show');
});
And the function in controller:
// POST
public function submitPhoto(Request $request, $id)
{
$input = Input::all();
dd($input);
die();
}
Ok, everything is working, the js is taking the action to the function, and sending the id so i can update then the db, this is an edit function, this is what im getting, no files ,
Im using Laravel Voyager, its Laravel...so i can use any Laravel class.
Already fixed by adding an id on html:
<input type='file' accept="image/x-png, image/jpeg" name="photo" id="photo" capture="camera"/>
Then you can do $request->hasFile('photo');

Laravel - data stored in a variable but becomes null in database

I have a problem with my function that saves data into database,
Basically user puts name into a form and when I do console.log I do get that value, I then call my function in controller and everything looks sucessfully - no errors but when I check in mysql name = NULL, why?
My form:
<div class="modal fade" id="myModalHorizontal" tabindex="-1" role="dialog"
aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<!-- Modal Header -->
<div class="modal-header">
<button type="button" class="close"
data-dismiss="modal">
<span aria-hidden="true">×</span>
<span class="sr-only">Close</span>
</button>
<h4 class="modal-title" id="myModalLabel">
Website Name
</h4>
</div>
<div class="modal-body">
<form action="{{ action('BuilderController#postDB') }}"
class="form-horizontal" role="form" method = "POST">
<div class="form-group">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="hidden" id="code" name="newCode" value="">
<label class="col-sm-2 control-label"
for="website_name">Name</label>
<div class="col-sm-10">
<input type="text" class="form-control"
id="website_name" placeholder="Website Name"/>
</div>
</div>
</div>
<!-- Modal Footer -->
<div class="modal-footer">
<button type="button" class="form-control margin btn btn-danger"
data-dismiss="modal">
Close
</button>
<button onClick=" updateDatabase(this);" type="submit" class="form-control margin btn btn-success" id="getRequest changes">
Save Website
</button>
</div>
</div>
</div></form>
</div>
JS:
var web_name;
function updateDatabase()
{
code2 = document.getElementById("content-link2").innerHTML;
var newCode = document.getElementById('code').value = code2;
web_name = ($('#website_name').val());
console.log(web_name);
console.log(newCode);
Controller:
public function postDB(Request $request) {
$newName = $request->input('web_name');
$newLat = $request->input('newCode');
$websites = new Website();
$websites->name = $newName;
$websites->html = $newLat;
$websites->save();
// Now we go to our other function
return $this->website($newName);
}
public function website($newName)
{
// Return our "website" object
$html = Website::where('name', $newName)->first();
// Pass the contents of the "html" property to the view
return view('layouts/website', [
'html' => $html->html
]);
}
You need to add name attribute to input element:
<input name="website_name" ...

Resources