Ajax form doesn't works on second submit.
Created 7 years ago by lckamal

I 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">&times;</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?

bloveless  —  7 years ago

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.

ryanthompson  —  7 years ago

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:

https://github.com/anomalylabs/streams-platform/blob/1.1/src/Http/Controller/FormController.php#L44-L46

lckamal  —  7 years ago

@ryanthompson - after commenting the lines it worked.

ryanthompson  —  7 years ago

Cool! Ill probably just do that then after some additional testing. Let me know if anything weird pops up!