I am using bootstrap register form to take information of users and store them in my database I use ajax
request to submit the form information and store it after validation .
The information is validated correctly and then stored correctly in my database.
the problem is after storng the information the request does not return a response.
This is the validation bootstrap code and the ajax request :
$(document).ready(function(){
//To validate the registration form and save its value after validation
$('#registerForm').bootstrapValidator({
message: 'This value is not valid',
feedbackIcons: {
valid: 'glyphicon glyphicon-ok',
invalid: 'glyphicon glyphicon-remove',
validating: 'glyphicon glyphicon-refresh'
},
fields: {
email: {
validators: {
notEmpty: {
message: 'The email is required and cannot be empty'
},
emailAddress: {
message: 'The input is not a valid email address'
}
}
}
}
})
.on('success.form.bv', function(e) {
// Prevent form submission
e.preventDefault();
// Get the form instance
var $form = $(e.target);
// Get the BootstrapValidator instance
var bv = $form.data('bootstrapValidator');
// Use Ajax to submit form data
$.ajax({
type: "POST",
url: $form.attr('action'),
data:$(form).serialize(),
dataType: "json",
success: function(data)
{
alert(data);
},
error : function(XMLHttpRequest, textStatus, errorThrown) {
alert(XMLHttpRequest);
alert(textStatus);
alert(errorThrown);
alert(XMLHttpRequest.responseText);
}
});
});
});
The ajax function does not go inside any of the success
or error
functions as I said it works fine and store everything correctly but instead of return to the ajax function it prints the respons in the browser .
This is also the php page :
<?PHP
$server_root = "../";
include_once("{$server_root}include-sql/mysql.class.php");
include_once("{$server_root}config.php");
Global $db1;
$db1 = new db_mysql($conf['db_hostname'], $conf['db_username'], $conf['db_password'], $conf['db_name']);
$db1->query("SET NAMES utf8");
date_default_timezone_set('Asia/Istanbul');
$email = mysql_real_escape_string($_POST['email']);
$sql = $GLOBALS['db1']->query("INSERT INTO users (email) VALUES ('$email')");
if($sql)
{
$msg = 'Success';
}
echo json_encode($msg);
?>
The msg
is printed after submission I can not alert it because it is not returning to the ajax success function ??? Is there any thing wrong or did I miss any thing in this code ?