Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

108
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!