here is the code:
$("#ajax_formulario").submit(function () {
$.post("ajax.php", {nombre:$(".ajax_nombre").val(), apellido:$(".apellido_ajax").val(), edad:$(".edad_ajax").val() }, function () {
alert("Exitos al enviar los datos");
});
});
This is the JS code, using the $.post
to post to the php ajax.php ; packing the variables and then an anonymous function, in php I receive them like this:
<?php
$nombre = $_POST["nombre"];
$apellido = $_POST["apellido"];
$edad = $_POST["edad"];
echo $nombre + $apellido + $edad;
?>
but i get the error
index $name undefined
as well as age and last name.
I will tell you the following about your exercise, although you are taking the values of your text boxes in JavaScript, the error that appears in PHP is because you are indicating that they are going to add 3 values in the form of name, age and surname and that is not what they are called.
That is, the labels of your inputs must be like this
I leave you an example similar to what you are looking for in your exercise applying AJAX
PHP
Here the form:
put the name to your forms like this
And to collect the data from the input , you do it like this
I think your problem is that you are collecting empty value.
Variables:
It would also be convenient to call the elements by their id (unique) and not by their class.
ex: