Submiting Symfony 2 embedded form with file using ajax - ajax

I'm creating embedded file uploading form with file validation in Symfony 2. For file uploading I used this example http://symfony.com/doc/current/cookbook/doctrine/file_uploads.html and for embedded forms this http://symfony.com/doc/current/cookbook/form/form_collections.html. It's work perfect, but I must submit a form using ajax, how can I do it?
Below is a example how I submit form using ajax.
$("#submit_form").click(function() {
var $form = $(this).parents('form:first');
var $that = $(this);
$.ajax({
type: 'POST',
dataType: 'json',
cache: false,
data: $form.serialize(),
success: function($data) {
if ($data.status == 'ok') {
$that.parents('#lightbox').html($data.template);
}
},
url: $form.attr('action')
});
return false;
});
So the problem is I can't pass files with ajax.

Use this plugin http://malsup.com/jquery/form/ . Works like a charm!
This can submit the form and also upload files. All you need to do is call ajaxSubmit and then implement the method that handles the response.
$('#myForm2').submit(function() {
$(this).ajaxSubmit({success: showResponse});
});
function showResponse(responseText, statusText, xhr, $form) {
alert('status: ' + statusText + '\n\nresponseText: \n' + responseText +
'\n\nThe output div should have already been updated with the responseText.');
}

Related

Check Form Before Submit Use Ajax Django

I use Ajax to send data from form to server. But I want to check data in form before Submit by Ajax:
<form id='#id_form' onsubmit='return checkInputSubmit();' >
...
</form>
In Ajax:
$('#id_form').submit(function(e) {
e.preventDefault();
$.ajax({
type: 'POST',
url: url,
dataType: 'json',
data: $('#id_form').serialize(), // serializes the form's elements.
success: function(data) {
if(data.result == true) {
//My code
}
else {
//My code
}
}
});
});
But checkInputSubmit() can't prevent submit from Ajax. You can explain for me and give me solution to check data in form before Submit by Ajax.
Thanks.
in the $('#id_form').submit(function(e) event, you can check/edit/return... any thing you want before call Ajax. Within Ajax, i think it won't work and don't good. (we just check ajax result, not input)
#Blurp, I found the solution by use your help.
$('#id_form').validate({
submitHandler: function(form) {
e.preventDefault();
$.ajax({
type: 'POST',
url: url,
dataType: 'json',
data: $('#id_form').serialize(), // serializes the form's elements.
success: function(data) {
if(data.result == true) {
//My code
}
else {
//My code
}
}
});
}
});

How to update content of a Bootstrap modal loaded using Ajax?

I've a bootstrap Modal with users list, inside which I'm displaying another modal for adding a new user. The Add new User Modal is fetched using Ajax. When I add a new user using Ajax, I want to update the div with Ajax response message(.alert-success or .alert-danger in bootstrap).
Right Now the user is loaded but I can't show the message as the modal itself is loaded using Ajax.
$('body').on("click", "#createGroupUserModual .submit", function (e) {
var alertContainer = $(document).find('#groupUserFormCreate').find("#alerts");
$.ajax({
url: $('#groupUserFormCreate').attr('action'),
type: 'POST',
dataType: "json",
cache: false,
data: $('#groupUserFormCreate').serialize(),
success: function(response) {
console.log("success " + response.message);
var returnMessage = "<div class='alert alert-success'>"+response.message+"</div>";
//this below line is not updating the contents with the message.
alertContainer.html(returnMessage);
},
error: function(response) {
console.log("failure is here " + response.message);
var returnMessage = "<div class='alert alert-danger'>"+response.message+"</div>";
alertContainer.html(returnMessage);
}
});
});
I see that not same code for redraw de alertContent, it only exists when response is success but not in errro, under $.ajax query
So:
error: function(response) {
console.log("failure is here " + response.message);
var returnMessage = "<div class='alert alert-danger'>"+response.message+"</div>";
alertContainer.html(returnMessage);
}
Try this code, added the redraw html in error.
Expect will be.

Returning a form using ajax/jquery that includes html within an input

So, I'm trying to return a hidden field (which is part of a form submission) which contains a string of html formatted text, there are reasons for this, the html is being rendered internally into pdf. I am currently posting the form back with ajax. The form serializes but when the form contains the html string, it returns a 500 error when trying to find the controller.
Code:
$(function () {
$('#preview').click(function (evt) {
//prevent the browsers default function
evt.preventDefault();
var $form = $('#sform');
$.ajax({
type: $form.prop('method'),
url: $form.prop('action'),
data: $form.serialize(),
dataType: "json",
traditional: true,
success: function (response) {
var newURL = window.location.protocol + "//" + window.location.host + "//" + response;
document.getElementById('myIframe').src = newURL;
}
});
});
});
Probably you need to turn off request validation. You can mark up your controller method with
[ValidateInput(false)]
Oh, and I think that for later versions of MVC you'll also need
<system.web>
<httpRuntime requestValidationMode="2.0"/>
...
</system.web>

ajax pager on views page load by ajax

I load a vieschs views_embed_vesch using Ajax.
This views included Ajax PAGER. It does not work.
Drupal.attachBehaviors does not work too.
I think in Drupal.settings need added data from views.
(function($){
Drupal.behaviors.ajax_views = {
attach: function (context, settings) {
//alert('dddd');
$('a.views-link').click(function(){
// alert('sfsdfsd')
var relArr = $(this).attr('rel').split(' ');
$.ajax({
type: "POST",
url: Drupal.settings.basePath + 'ajax-views/'+relArr[0]+'/'+relArr[1],
dataType: 'json',
success: function (datad){
alert(datad.seet.views);
// Drupal.settings.views = datad.seet.views ;
$('div#block-system-main > div.content').append(datad.view);
},
});
return false;
});
}
};
})(jQuery);
Try this module to load content via ajax. It will automatically attach behaviour with the ajax loaded content.
https://drupal.org/project/ajax_links_api

jqGrid multiselect not submiting the selected rows

I am able to get jqGrid display my data. When I do multi-select and submit the form (jqGrid is obviously inside the form) then I am not able to get the selected rows in Spring mvc controller.
Here is my onClick event
$("#GetDataDetailsLink").click( function() {
var selectedRows;
selectedRows = jQuery("#DataList").jqGrid('getGridParam','selarrrow');
var dataToSend = JSON.stringify(selectedRows);
alert("dataToSend is =" + dataToSend);
alert("selectedRows is =" + selectedRows);
//non-ajax submit
var url = "/underwriting-ui-web/taskDetail" + "?selectedTasks="+selectedRows;
$("#DataGridForm").attr("action", url);
$("#DataGridForm").attr("method", "get");
alert("action url is = " + $("#taskGridForm").attr("action"));
$('#DataGridForm').submit();
//ajax way of submiting
/*
$.ajax({
url:'/dataDetail',
type: 'POST',
contentType: 'application/json; charset=utf-8',
data: dataToSend,
dataType: 'json',
success: function (result) {
alert('success: ' + result.result);
}
});
return true;
*/
});
Some how jqGrid is overwriting my url and writing it's own url something like this
http://localhost:8080/myProject-ui-web/DataDetail?jqg_taskList_8=on&jqg_taskList_9=on
Instead of
http://localhost:8080/myProject-ui-web/DataDetail?selectedTasks=8,9
In ajax submit it is working. But I wanted it in non-ajax submit.
Any idea why this is not working in non-ajax submit?

Resources