0
Currently I am doing so and every time I submit the form the alert returns error even with the fields filled correctly
$('.form-ajax').on('submit', function(e) {
e.preventDefault();
var form = $('#formnews');
$.ajax({
method: 'POST',
url: form.attr('action'),
async: true,
data: form.serialize(),
cache: false,
success: function(result) {
if (result.success) {
swal('Bom trabalho!', 'Sua inscrição foi efetuada com sucesso!', 'success');
$('#formnews').val('');
} else {
swal('Alguma coisa errada', result.message, 'warning');
}
},
error: function(data) {
swal('Erro!', 'Ocorreu um erro', 'error');
}
})
});
<form action="{{ route('newsletter.post.ajax') }}" method="POST" id="formnews">
{{ csrf_field() }}
<div class="field has-addons">
<div class="control is-expanded has-icons-left has-icons-right">
<div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">
<p class="control is-expanded has-icons-left has-icons-right">
<input id="email_newsletter" type="email_newsletter" class="input" name="email_newsletter" value="{{ old('email_newsletter') }}" placeholder="Seu melhor email" required>
@if ($errors->has('email'))
<span class="help-block">
<strong>{{ $errors->first('email') }}</strong>
</span>
@endif
<span class="icon is-small is-left">
<i class="fa fa-envelope"></i>
</span>
<span class="icon is-small is-right">
<i class="fa fa-check"></i>
</span>
</p>
</div>
</div>
<div class="control">
<button type="submit" class="button is-warning">
Cadastrar
</button>
</div>
</div>
</form>
When I use only id it validates normally, the problem is that I have other names on the page and I can’t use the same id to validate so I tried using class but it doesn’t work
– Madiel França
Sorry, but I didn’t understand at what point the use or not of the same id would interfere with the result validation problem. Maybe the problem is another. A good practice is to always create different Id for each form. It would be helpful if you made more snippets of your source code available.
– Caiuby Freitas
<form name="formcontato" class="form-horizontal form-ajax" method="POST" action="{{ route('page.student.post post') }}">
– Madiel França
I have two formulas one on each page but I need to pass them through a validation via java script and return a message with switalert more as do the same java script validate two different formularies
– Madiel França
<form action="{{ route('newsletter.post.ajax') }}" method="POST" id="formnews"> this code is from the second form
– Madiel França
Ok, but where the validation of fields happens?
– Caiuby Freitas
i had seen a way to envez to catch id or class pick the url of the page so I could use validate the form based on its url and not on its id
– Madiel França
the validation happens in the form itself
– Madiel França
I did some research but unfortunately I couldn’t find anything relevant. Frankly, I find it unusual not to opt for the use of Id, since it would greatly facilitate the work. I feel I can not help beyond this.
– Caiuby Freitas
I found the problem was by id even the problem was the logic of my controller that was wrong
– Madiel França