Help with Ajax does not send data to the database

Asked

Viewed 111 times

0

I’m trying to submit a form to my database. Ajax says that the user’s registration was carried out successfully but when I go to look in the database there is nothing there.

My Form.

<form class="form3" method="POST" enctype="multipart/form-data">
    <div id="ressult"></div>

    <p>Nome completo</p>
    <input type="text" name="Nome_us" placeholder="Nome completo *">

    <p>Nome de usuário</p>
    <input type="text" name="Nick_us" placeholder="Nick da conta *">

    <p>E-mail</p>
    <input type="text" name="Email_us" placeholder="Informe seu E-mail *">

    <p>Senha</p>
    <input type="password" name="Senha_us" placeholder="Informe a sua senha *">

    <input type="hidden" name="Imagem_us" value="/images-profile/Profile_img_00_TSN.png">
    <input type="submit" id="Bot_Cria_login" name="Cadastrar_us" value="Criar conta">
</form>

My ajax.

<script>
    $(function(){
        $('.form3').submit(function(event){
            event.preventDefault();
            var formDados = $('.form3');

            $.ajax({
                location:'/arquivos/conta_usuario.php',
                type:'POST',
                data:formDados,
                cache:false,
                contentType:false,
                processData:false,
                success:function(data){
                    $('#ressult').html(data);
                    alert('Cadastrado com sucesso!');
                },
                dataType:'html'
            });
            return false;
        });
    });
</script>

My php where I want to send the data I filled.

<?php
    require_once('conex.php');

    if(isset($_POST['Cadastrar_us'])){
        $Nome_user      = $_POST['Nome_us'];
        $Nick_user      = $_POST['Nick_us'];
        $Email_user = $_POST['Email_us'];
        $Senha_user = $_POST['Senha1_us'];
        $Imagem_user    = $_POST['Imagem_us'];

        if($Nome_user == ""){
            echo 'Preencha o campo Nome corretamente.';
        }
        else{
            $SQL_Cadastro = mysqli_query($conex,"INSERT INTO Usuarios_login_s_n (Nome_us_sn, Nick_us_sn, Email_us_sn, Senha_us_sn, Foto_us_sn) VALUES ('$Nome_user', '$Nick_user', '$Email_user', '$Senha_user', '$Imagem_user')");
        }
    }
    else{

    }
?>
  • Take a look at the answer.

1 answer

2


In the row below, you are sent the form HTML , and not the data:

var formDados = $('.form3');

To store form data to variable, use the method .serialize():

var formDados = $('.form3').serialize();

The contentType is also wrong. Remove it that will be sent from the default type. Also change the location of Ajax by url.

The fact that you are receiving the alert does not mean that the data has been received, it just means that something is returning without errors.

Your Ajax will look like this:

$(function(){
  $('.form3').submit(function(event){
      event.preventDefault();
      var formDados = $('.form3').serialize();

      $.ajax({
          url:'/arquivos/conta_usuario.php',
          type:'POST',
          data:formDados,
          cache:false,
          processData:false,
          success:function(data){
              alert('Cadastrado com sucesso!');
          },
          dataType:'html'
      });
      return false;
  });
});
  • 1

    And to complement: The form will not send the value of the "Register" button. No php, to check if there is a POST, just use if ( $_SERVER["REQUEST_METHOD"] == "POST" ) { ... }

  • Thank you so much for your help.

Browser other questions tagged

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