Error in ajax when sending the form

Asked

Viewed 30 times

1

I have that code.

The problem is that it always shows the error message Some problem occurred, Please Try Again.

What can I do to fix it? I’m using new Formdata(this) because I have file type fields in the form.

$(document).ready(function(e){
    $("#form_cadastros_empresa").on('submit', function(e){
        e.preventDefault();
        $.ajax({
            type: 'POST',
            url: 'form_cadastro.php',
            data: new FormData(this),
            contentType: false,
            cache: false,
            processData:false,
            beforeSend: function(){
                $('.submitBtn').attr("disabled","disabled");
                $('#form_cadastros_empresa').css("opacity",".5");
            },
            success: function(msg){
                $('.resultado_form_cadastros_empresa').html('');
                if(msg == 'ok'){
                    $('#form_cadastros_empresa')[0].reset();
                    $('.resultado_form_cadastros_empresa').html('<span style="font-size:18px;color:#34A853">Form data submitted successfully.</span>');
                }else{
                    $('.resultado_form_cadastros_empresa').html('<span style="font-size:18px;color:#EA4335">Some problem occurred, please try again.</span>');
                }
                $('#form_cadastros_empresa').css("opacity","");
                $(".submitBtn").removeAttr("disabled");
            }
        });
    });


});

My Html

<form enctype="multipart/form-data" id="form_cadastros_empresa" novalidate="">


<div class="form-group row">
                    <div class="col-sm-6">
                        <label for="Numero">Usuário</label>
                        <input type="text" class="form-control" id="nomeUsuario" placeholder="Usuário">
                    </div>
                    <div class="col-sm-6">
                        <label for="Complemento">Senha</label>
                        <input type="text" class="form-control" id="senha" placeholder="Senha">
                    </div>

                </div>



<div class="form-group row">
                    <div class="col-sm-6">
                        <label for="Nome">Categoria</label>
                        <select class="form-control" name="categoria" id="categoria" required="">
    <option value="">Selecione</option>                 
<option value="22">Saúde</option><option value="23">Beleza</option><option value="24">Presentes</option><option value="25">Veículos</option>


    </select> 

                    </div>
                    <div class="col-sm-6">
                        <label for="SobreNome">Subcategoria</label>
                        <select class="form-control" name="subcategoria" id="subcategoria" required="">

                        </select>

                    </div>
                </div>
<br>

                <hr><br>

                <div class="form-group row">
                    <div class="col-sm-12">
                        <label for="Nome">Nome da Empresa</label>
                        <input type="text" class="form-control" id="Nome" placeholder="Nome" required="">
                    </div>

                </div>
                <br>

                <hr><br>

                <div class="form-group row">

                    <div class="col-sm-6">
                        <label for="funcionamento">Horário de Funcionamento</label>
                        <input type="text" class="form-control" id="funcionamento" placeholder="Horário de Funcionamento">
                    </div>
                </div>


                <div class="form-group row">
                    <div class="col-sm-6">
                        <label for="email">Email</label>
                        <input type="email" class="form-control" id="email" placeholder="Email" required="">
                    </div>

                </div>




                <div class="form-group row">
                    <div class="col-sm-6">
                        <label for="descricao">Descrição da Empresa</label>
                        <textarea rows="20" cols="50" id="descricao" placeholder="Descrição da Empresa">
</textarea>


                    </div>
                    <div class="col-sm-6">
                        <label for="Site">Site</label>
                        <input type="text" class="form-control" id="Site" placeholder="Site">
                    </div>
                </div>




                <br>

                <hr><br>

                <div class="form-group row">
                    <div class="col-sm-4">
                        <label for="Facebook">Facebook</label>
                        <input type="text" class="form-control" id="Facebook" placeholder="Facebook">
                    </div>
                    <div class="col-sm-4">
                        <label for="Intsagram">Instagram</label>
                        <input type="text" class="form-control" id="Instagram" placeholder="Intsagram">
                    </div>
                    <div class="col-sm-4">
                        <label for="Twitter">Twitter</label>
                        <input type="text" class="form-control" id="Twitter" placeholder="Twitter">
                    </div>
                </div>







<br>

                <hr><br>





                <div class="form-group row">
                    <div class="col-sm-6">
                        <label for="Planos">Planos</label>







          <select name="Planos" id="sectionChooser" class="form-control">


          <option value="">Selecione</option>                   
<option value="1">Diamante</option><option value="2">Ouro</option><option value="3">Prata</option><option value="4">Light</option><option value="5">Free</option><option value="19">Link</option>         


          </select>                    




<div class="panel" id="1" style="display: none;">





<div class="col-sm-12">
                                <label><b>Diamante</b></label>
                                </div>
                                <div class="form-group ">
                                <div class="row">

                                                        <div class="col-sm-12">
                        <label for="endereco_buscauiva">Endereço Buscauiva</label>
                         <input type="text" class="form-control" id="endereco_buscauiva" placeholder="http://buscaiuva.com.br/NomeDaEmpresa" required=""> 
                    </div>

                            </div>
                            </div>



                            <div class="form-group ">
                            <div class="row">

                                                            <div class="col-sm-12">
                        <label for="Logomarca">Logomarca</label>
                        <input type="file" class="form-control" id="Logomarca">
                        </div>

                                                        </div>
                            </div>

                    <div class="form-group ">
                    <div class="row">

                    <div class="col-sm-4">
                        <label for="imagem1">Imagem banner 1</label>
                        <input type="file" class="form-control" id="imagem1">
                    </div>
                                                            <div class="col-sm-4">
                        <label for="imagem2">Imagem banner 2</label>
                        <input type="file" class="form-control" id="imagem2">
                    </div>
                                                            <div class="col-sm-4">
                        <label for="imagem3">Imagem banner 3</label>
                        <input type="file" class="form-control" id="imagem3">
                    </div>


                                        </div>
                    </div>

<div class="form-group ">
<div class="row">                   


                                <div class="col-sm-4">
                        <label for="Telefone1">Telefone 1</label>
                        <input type="text" class="form-control" id="Telefone1" placeholder="Telefone 1" required="">
                    </div>




                    <div class="col-sm-4">
                        <label for="Telefone2">Telefone 2</label>
                        <input type="text" class="form-control" id="Telefone2" placeholder="Telefone 2">
                    </div>


                       <div class="col-sm-4">
                        <label for="Telefone3">Telefone 3</label>
                        <input type="text" class="form-control" id="Telefone3" placeholder="Telefone 3">
                    </div>




                <hr>
</div>
</div>


<div class="form-group ">
<div class="row">
                    <div class="col-sm-6">
                        <label for="Bairro">Bairro</label>
                        <select class="form-control" name="bairro1" id="bairro1" required="">
    <option value="">Selecione</option>                 
<option value="1">teste bairro</option><option value="2">teste bairro 2 3</option><option value="3">ação bairros</option><option value="4">Centro</option>


    </select> 
                    </div>
                    <div class="col-sm-6">
                        <label for="Rua">Rua</label>



<select class="form-control" name="rua" id="rua" required="">





    </select> 




                    </div>

                    <br><br><br><br>





<input type="hidden" class="form-control" id="cadastros_empresa" placeholder="cadastros_empresa" value="cadastros_empresa">
                <button type="submit" class="submitBtn btn btn-primarys px-4 float-right">Cadastrar</button>

            </form>
  • right, but the form only has the photo?

  • as file type yes

  • but in case you’re passing only one element

  • Wagner, you have a lot of unresolved questions. You simply abandon them, do not respond to comments of doubts etc... the purpose of the site is question + answer, and not only ask questions after the other and go abandoning the others.

1 answer

1


Missing information to be passed, try this alternative on Jquery:

var data=new FormData();
data.append("foto",$("campo_da_foto")[0].files[0]);
data.append("outras informações",$("outros campos").val());//de acordo com sua necessidade

Then pass that data to the country $.ajax({ data:data }).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.