I have a form with n tables involved and needed to leave all the insert in a single file, well this I already have. But I need it to be sent without refresh and for this I am using ajax. Here's the question how to do this in ajax, in a way that only the data that gets clicked from the button with the same name in form is sent? Here is a simple example I created now to demonstrate my idea.
index.php
<script type="text/javascript">
$(document).ready(function(){
$('#ajax_form').submit(function(){
var dados = jQuery( this ).serialize();
$.ajax({
type: "POST",
url: "processa.php",
data: dados,
success: function( data )
{
alert( data );
}
});
return false;
});
});
</script>
</head>
<body>
<form method="post" action="" id="ajax_form">
<label>Usuário: <input type="text" name="usuario" value="" /></label>
<label>Senha: <input type="text" name="senha" value="" /></label>
<label><input type="submit" name="enviar" value="Enviar" /></label>
</form>
<form method="post" action="" id="ajax_form">
<label>Rua: <input type="text" name="rua" value="" /></label>
<label>Número: <input type="text" name="numero" value="" /></label>
<label><input type="submit" name="enviar2" value="Enviar" /></label>
</form>
processa.php
if(isset($_POST['enviar'])){
$usuario = $_POST['usuario'];
$senha = $_POST['senha'];
$sql = "SELECT * FROM usuar";
$result = $conn->query($sql);
$row = $result->fetch_assoc();
$resultx= "INSERT INTO usuar (usuario, senha) VALUES ('$usuario', '$senha')";
$resultado = mysqli_query($conn, $resultx);
echo $resultx;
}
if(isset($_POST['enviar2'])){
$rua = $_POST['rua'];
$num = $_POST['num'];
$sql2 = "SELECT * FROM userend";
$result = $conn->query($sql2);
$row = $result->fetch_assoc();
$resulty = "INSERT INTO userend (rua, numero) VALUES ('$rua', '$num')";
$resultado = mysqli_query($conn, $resulty);
echo $resulty;
}