Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

109
Vistas
jQuery AJAX not working when passing data to a PHP page

Validation part of my jQuery works, however, the AJAX part does not seem to be doing anything and I am wondering why? It comes up with no errors and the registration works perfect without the AJAX, however, I have to use it.

HTML form in register.php:

<form method="post" action="register.php" name="registration-form" id="register-form">
    <div class="form-group" id="email-group">
        <label for="email"> Email address </label>
        <input id="email" type="email" name="email" class="form-control" aria-describedby="emailHelp" required>
    </div>
    <div class="form-group" id="password-group">
        <label for="password"> Password </label>
        <input id="password" type="password" name="password" class="form-control" required>
    </div>
    <div class="form-group form-check">
        <input type="checkbox" name="terms" class="form-check-input" required> I accept the <a class="privacy-link"
            href="privacy-policy.php"> Privacy Policy </a>
    </div>
    <button type="submit" name="register_user" class="btn btn-primary" value="register_user">Register</button>
</form>

jQuery with AJAX:

$(document).ready(function () {
    $("#register-form").submit(function (event) {
        event.preventDefault();
    }).validate({
        rules: {
            email: {
                minlength: 6
            },
            password: {
                minlength: 8
            }
        },
        messages: {
            email: "Email should be at least 6 characters",
            password: "Password should be at least 8 characters"
        },
        submitHandler: function (form) {

            var email = $("#email").val();
            var password = $("#password").val();

            $.ajax({
                url: "register.php",
                contentType: "application/json",
                type: "POST",
                data: {
                    email: email,
                    password: password
                }
            }).done(function (response) {
                // some actions
            });
        }
    });
});

register.php PHP to handle it:

if (isset($_POST['register_user'])) {
    /* Others */
}

I tried various "isset", but to no avail.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

There are some things wrong that I see here.

  1. maybe you shouldn't catch the "submit" event of your form, I think the validation plugin takes care of that. according to the doc: https://jqueryvalidation.org/validate/#submithandler maybe I should remove this part:

    .submit(function(event) { event.preventDefault(); })

  2. in your ajax call, you send 2 post variables, email and password , but in your PHP file you try to retrieve the variable $_POST['user_registration'] which DOES NOT EXIST because you did not send your form to the server's register.php file, you sent only the 2 variables email and password

I suggest you use FormData ( https://developer.mozilla.org/en-US/docs/Web/API/FormData/FormData ) so you just have to update the following code to get it to work (in theory)

example

 data = new FormData(document.getElementById('#register-form')); $.ajax({ url: "register.php", type: "POST", data: data, processData: false, contentType: false, })

you can refer to this SO question https://stackoverflow.com/a/8244082/5856233

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda