Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialViraj Kokane
17,531 PointsPass null value through ajax request json
I am using Laravel 5.1 and I need to pass the null value through ajax json request when a user removes the existing data from form and keeps the field blank. It works well if the user input some data it gets save but gives error 422 in javascript console when the data in the field is removed and kept blank. Please need urgent solution for it. Thank you in advance ! View:
<td>
<?php
$note=$user->notes->last();
$note_content = null;
if($note) {
$note_content = $note->content;
}
?>
{!! Form::open(array('route'=>'notes.store', 'class'=>'form-horizontal', 'role'=>'form')) !!}
{!! Form::textarea('content', $note_content, array('placeholder'=>'note', 'class'=>'form-control note-content', 'rows'=>'3')) !!}
{!! Form::hidden('user_id', $user->id) !!}
{!! Form::close() !!}
</td>
Javascript file:
<script type="text/javascript">
$(document).ready(function(){
var usersTable = $('#usersTable').DataTable({
"order": [[5, 'desc'], [0, 'desc']],
"iDisplayLength": 50
});
$('.note-content').blur(function () {
var form = $(this).parent();
$.ajax({
url : form.attr('action'),
type : form.attr('method'),
dataType: 'json',
data : form.serialize(),
success : function( data ) {
},
error : function( xhr, err ) {
alert('Error');
}
});
});
});
</script>
Using below Controller function:
public function store(NoteRequest $request)
{
$request['admin_id']= Auth::user()->id;
$request['from_admin']= Auth::user()->first_name;
$user = User::findOrFail($request['user_id']);
$note = $user->notes->last();
$note_content = null;
if($note) {
$note_content = $note->content;
}
if($request['content'] == $note_content) {
return response()->json();
}
$note = Note::create($request->all());
return response()->json();
}