Insert mysql using ajax

1

I'm trying to insert a record into the DB via ajax but I'm having problems, I was using format: 'json' but it was giving permission error and talz, now I'm using type: "POST", dataType: "jsonp" . Anyway, this data for me to insert the record comes from a form, follow 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 PHP on 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 error that it returns to me is this:

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

2 answers

2

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

Make the right ones according to 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');
                        }
                    });
});
    
12.08.2014 / 20:30
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');
                }
            });
        });
    
12.08.2014 / 20:30