Good morning, I am a student entering the world of programming trying to figure out how exactly the script works and I could use a help please, thank you.
<script type="text/javascript">
$(document).ready(function() {
$('.error').hide();
$("#enviar-btn").click(function() {
//Obtenemos el valor del campo nombre
var name = $("input#name").val();
//Validamos el campo nombre, simplemente miramos que no esté vacío
if (name == "") {
$("label#name_error").show();
$("input#name").focus();
return false;
}
//Obtenemos el valor del campo password
var password = $("input#password").val();
//Validamos el campo password, simplemente miramos que no esté vacío
if (password == "") {
$("label#pass_error").show();
$("input#password").focus();
return false;
}
//Construimos la variable que se guardará en el data del Ajax para pasar al archivo php que procesará los datos
var dataString = 'name=' + name + '&password=' + password;
$.ajax({
type: "POST",
url: "register.php",
data: dataString,
success: function() {
$('#register_form').html("<div id='message'></div>");
$('#message').html("<h2>Tus datos han sido guardados correctamente!</h2>")
.hide()
.fadeIn(1500, function() {
$('#message').append("<a href='index.php?action=see'>Ver usuarios registrados</a>");
});
}
});
return false;
});
});
runOnLoad(function(){
$("input#name").select().focus();
});
Exactly what is this ' $.Ajax
'? I understood that it was used to register in a database and replace the insert of the php, but it seems that it is not like that. In url, I see that this ' register.php
', but then we are calling the document where the script is as if it were an include? Because I do not see that he is passing any information there with the AJAX.
In data, it is equal to a declared variable, but the same structure of the variable (var dataString = 'name=' + name + '&password=' + password;)
I can not understand it, in other examples I have seen are much more different, as this (var data= 'valor': valor1)
or the structure I find it very different , when you use one and when the other or why?
/// register.php
<?php
$name = utf8_decode($_POST['name']);
$password = md5($_POST['password']);
$con = mysql_connect('localhost', 'usuario', 'password');
mysql_select_db("tu_base_de_datos", $con);
$insert = "INSERT INTO tu_tabla (name, password, date_add) VALUES
('$name', '$password', now())";
mysql_query($insert);
?>
First time I see the mysql_query($insert);
in a php file, does this have to do something with ajax?