Ajax form doesn't works on second submit.
Created 7 years ago by lckamalI am using entries ajax form functionality. The form submits and works in first time successfully. When I submit the form with different value it gives such error: http://take.ms/apUbq
Modal Form:
<div class="modal fade" id="video-form">
<div class="modal-dialog">
<div class="modal-content">
{% set videoForm = form('profiles', 'profiles').fields(['video']).entry(profile).ajax(true).get() %}
{{ videoForm.open|raw }}
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title">{{ trans('module::site.add') }} Video</h4>
</div>
<div class="modal-body">
<div id="video-form-msg"></div>
<div class="form-group">
<label for="">Video Embed URL from Youtube</label>
<input class="form-control" type="text" name="video" value="{{ profile.video }}" placeholder="http://" />
</div>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-primary">Save</button>
</div>
{{ videoForm.close|raw }}
</div>
</div>
</div>
Javascript:
$('#video-form form').on('submit', function(e){
e.preventDefault();
$.ajax({
url: $(this).attr('action'),
data: $(this).serialize(),
dataType: 'json',
method: 'POST',
}).done(function( d ) {
if(d.success){
jobing.alert('#video-form-msg', 'success', "{{ trans('theme::message.video_success') }}");
}else{
jobing.alert('#video-form-msg', 'danger', "{{ trans('theme::message.video_error') }}");
}
});
});
any idea?
What is happening is that after a successful submission the cached form is removed from the system.
Currently the cached form is being removed from the system automatically after 24 hours, so maybe the form cache never needs to be cleared until it expires naturally.
This is something that will need to be discussed with @ryanthompson. I'm not sure exactly what secondary effects this solution might have.
Anyway, I've hidden this error in my system by hiding the form on successful submission and showing it again after the page is refreshed. This limits to one successful submission per page load, but it works in my case.
Hmm.. Interesting. I suppose the value could be not cleared TBH I am not entirely sure why it clears now but for garbage collection.
@lckamal can you try removing these lines and testing multiple submissions:
@ryanthompson - after commenting the lines it worked.
Cool! Ill probably just do that then after some additional testing. Let me know if anything weird pops up!
@lckamal done: https://github.com/anomalylabs/streams-platform/commit/5329384cfce21fd0dc1d584a906c3ecb3f6cea77