I am trying a simple ajax request that does not need to refresh the page upon submit. But I think I did it the wrong way. Any help would be much appreciated.
HTML/JS PAGE
<script>
$(function () {
$('form#person').on('submit', function(e) {
$.ajax({
type: 'post',
url: 'addAPerson.php',
data: $(this).serialize(),
success: function (o) {
=====> console.log(o); <=== I TRIED DOING THIS BUT NOTHING IS PRINTED
alert('MUST ALERT TO DETERMINE SUCCESS PAGE');
}
});
e.preventDefault();
});
});
</script>
<form id="person" action="addAPerson.php" method="post">
Firstname: <input name="fname" />
Lastname: <input name="lname" />
<input type="reset" style="float:right"name="cancel" value="cancel"/>
<input type="submit" name="submit" value="save"/>
</form>
addAPerson.php
<?php
$fname = $_POST['fname'];
$lname =$_POST['lname'];
$con = mysql_connect("localhost","root","");
mysql_select_db("person",$con);
$sql = "INSERT INTO person(firstname, lastname) VALUES('$fname', '$lname')";
mysql_close();
?>
<input name="fname" />needs to be changed to<input type="text" name="fname" />and do the same for the other one.type="text"is the default.action="addAPerson.php", is it still necessary as the form action? I remember seeing many times, that it wasn't necessary to be inside the form, just the Ajax, as long as it point to the form's ID, am I correct?