Insert mysql using ajax

Asked

Viewed 586 times

1

I am trying to enter registration in BD via ajax but am having problems, I was using the format: 'json' but I was giving permission error and Alz, now I’m using type: "POST", dataType: "jsonp". Anyway, this data for me to enter the record comes from a form, follows the code below:

JS

if(!document.getElementById('formulario')) return; 
                var f = document.getElementById( 'formulario' ); 

                var nome = f.elements[1].value;
                var email = f.elements[2].value;
                var senha = f.elements[3].value;
                var telefone = f.elements[4].value;
                var endereco = f.elements[5].value;
                var estado = f.elements[6].value;
                var cidade = f.elements[7].value;                

                var url = 'http://www.petfy.com.br/android/addUsuario.php?nome='+nome+'&email='+email+'&senha='+senha+'&telefone='+telefone+'&endereco='+endereco+'&estado='+estado+'&cidade='+cidade+'&imagem='+imagem+'';
                $.ajax({                                      
                    url: url,
                    type: "POST", 
                    dataType: "jsonp",
                    success: function(rows){
                        rows = $.parseJSON(rows);
                        for (var i in rows){    
                            var row = rows[i],          
                                id = row[0], 
                                nome = row[1]; 
                                alert(nome);
                        }       
                    },
                    error: function(rows){
                        alert('erro');
                    }
                });

now the PHP on the server:

header("Access-Control-Allow-Origin: *");

include '../banco.php';

$nome     = $_POST['nome'];
$email    = $_POST['email'];
$senha    = $_POST['senha'];
$telefone = $_POST['telefone'];
$endereco = $_POST['endereco'];
$estado   = $_POST['estado'];
$cidade   = $_POST['cidade'];
$imagem   = $_POST['imagem'];

$sql = mysql_query(" INSERT INTO usuarios VALUES ('','$nome','$email','$telefone','$endereco','$cidade','$estado','$senha','$imagem') ");

$usuario = array();
$sql2 = mysql_query("SELECT * FROM usuarios ORDER BY id DESC LIMIT 1");
while ($result = mysql_fetch_array($sql2)) {
    $usuario[] = $result;
}

echo json_encode($usuario);

the mistake he returns is this:

GET http://www.petfy.com.br/android/addUsuario.php?nome=Felipe%20Furlan&email=f…ype=large&callback=jQuery1707645740224979818_1407864204964&_=1407864215433 403 (Forbidden) 

2 answers

2

Hello friend review the changes I made, in the date you can pass the serialize of the complete form, instead of setting variables ... and something else since you are using jquery use in full instead of Document.getElementById = $('#id')

make due to adjustments as your needs

jQuery('#formulario').submit(function(e){
   e.preventDefault();
  var nome = $('input[name="nome"]').val();            
  var url = 'http://www.petfy.com.br/android/addUsuario.php';
                    $.ajax({                                      
                        url: url,
                        type: "POST",
                        data: jQuery(this).serialize(). 
                        dataType: "json",
                        success: function(rows){
                            rows = $.parseJSON(rows);
                            for (var i in rows){    
                                var row = rows[i],          
                                    id = row[0], 
                                    nome = row[1]; 
                                    alert(nome);
                            }       
                        },
                        error: function(){
                            alert('erro');
                        }
                    });
});
  • THANKS @Keven Jesus :)

1

SOLUTION

             var url = 'http://www.petfy.com.br/android/addUsuario.php';
            $.ajax({                                      
                url: url,
                type: "POST", 
                data:{
                    nome: nome, email: email, senha: senha, telefone: telefone, endereco: endereco, estado: estado, cidade: cidade
                },
                success: function(rows){
                    rows = $.parseJSON(rows);
                    for (var i in rows){    
                        var row = rows[i],          
                            id = row[0], 
                            nome = row[1]; 
                            alert(nome);
                    }       
                },
                error: function(rows){
                    alert('erro');
                }
            });
        });
  • 2

    It would be excellent to explain what makes this code solve the problem.

Browser other questions tagged

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