Hi,
Can someone give me a working example of a form submit using ajax.
I am trying to a make a small chat application, where the data should get saved using ajax.
Thanks,
Vaibhav
Hi,
Can someone give me a working example of a form submit using ajax.
I am trying to a make a small chat application, where the data should get saved using ajax.
Thanks,
Vaibhav
You can use jquery ajax form plugin. See the jQuery Ajax Form document or you can do manual with ajax
$('#form').submit(function(event){
event.preventDefault();
jQuery.ajax({
type:$(this).attr("method"),
url: $(this).hasAttr("action")?$(this).attr("action"):window.location.href,
data: $(this).serialize(),
success:function(data){
},
});
});
You can use somthing like this:
<?php $form = ActiveForm::begin([
'beforeSubmit' => new \yii\web\JsExpression('
function(form) {
jQuery.ajax({
url: "'. $this->context->createUrl('create') .'",
type: "POST",
dataType: "json",
data: form.serialize(),
success: function(response) {
console.log(response);
},
error: function(response) {
console.log(response);
}
});
return false;
}')
]);