0

I had a registration form on my website and after my users register and create a new account the form does not go inside the success function but the information is saved correctly in the database.

The ajax function is :

$(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: $form.attr('type'),
                        url: $form.attr('action'),
                        data: $(form).serialize(),
                        dataType: json,
                        success: function(msg) {
                                alert(msg);
                        }
                    });
        });
}); 

The php function of the action of the form :

<?php
$server_root ="../";
include_once("{$server_root}include-structure/ajax_header.php");

$first_name = mysql_real_escape_string($_POST['firstName']);
$last_name = mysql_real_escape_string($_POST['lastName']);
$username = mysql_real_escape_string($_POST['username']);
$password = mysql_real_escape_string($_POST['password']);
$email = mysql_real_escape_string($_POST['email']);
$phone = mysql_real_escape_string($_POST['phone']);
function better_crypt($input, $rounds = 7)
{
    $salt = "";
    $salt_chars = array_merge(range('A','Z'), range('a','z'), range(0,9));
    for($i=0; $i < 22; $i++) 
    {
        $salt .= $salt_chars[array_rand($salt_chars)];
    }
    return crypt($input, sprintf('$2a$%02d$', $rounds) . $salt);
}

$password_hash = better_crypt($password);

$join_date = date('Y-m-d H:i:s');

$sql = $GLOBALS['db1']->query("INSERT INTO users (first_name, last_name, username, password, email, mobile, user_type, account_type, join_date) VALUES ('$first_name','$last_name','$username','$password_hash','$email',$phone,1,1,'$join_date')");

if($sql)
{
    $msg = 'Success';
}
echo json_encode($msg);
?>

After submitting and storing the information I get the $msg printed on my screen instead of returning it to the ajax as a response and also my users redirected to the URL of my PHP file instead of my register page URL.

Why did the code does not go inside the success function?

andrew
  • 9,313
  • 7
  • 30
  • 61
  • sounds like your form is submitting through browser default process. Are any errors thrown in console that would block your form code from runnning? – charlietfl Oct 11 '14 at 13:50
  • no it runs perfectly and submit information and store them also and no errors in the console . BUT instead of returning the message to the `sucess` function to alert it , it print it in the browser – Basel Shbeb Oct 11 '14 at 13:54
  • and that would happen if form submits through default and cause the page reload. Maybe selector is wrong? Or form doesn't exist when validation code runs? – charlietfl Oct 11 '14 at 13:57
  • I can't get the code running without setting `dataType: json` to `dataType: 'json'`. – Barry Oct 11 '14 at 14:12

0 Answers0