-3

So I am trying to make sure in my registration that an email has not already been used and store in my database. How do i do this? So far I have go to the point of using an SQL statement to check the database for any results and if there are any then it should do... but I don't know what it should do.

rohan.1
  • 13
  • 5
  • 1
    It should report an error, just like any other problem, and require the user to submit a new registration. – Barmar Nov 07 '18 at 19:50
  • user types in email, you search db for that email, if no results, email is good and let them register, else show error on screen saying that email has already been used. – Isaac Vidrine Nov 07 '18 at 19:52
  • 1
    It should show similar error like "Email address already exists". – Ashish Yadav Nov 07 '18 at 19:52
  • For a better understanding of code check this https://stackoverflow.com/questions/42460896/check-username-and-email-already-exists-in-php-mysql – Saurabh Gujarani Nov 07 '18 at 19:59
  • You should make an AJAX call to some provider ( your own I assume ) that will check to see if the email address is already in your database. You may want to *consider* making sure it is a valid email address too. – Neo Nov 07 '18 at 20:02
  • Possible duplicate of [check username and email already exists in php mysql](https://stackoverflow.com/questions/42460896/check-username-and-email-already-exists-in-php-mysql) – Stephen P Nov 07 '18 at 20:07

2 Answers2

1

This should get you started.

$.ajax({
    type: "POST",
    cache: false,
    "global": false,
    data: yourEmailAddressVariable,
    url: yourProviderForValidation,
    error: function (request, status, error) {
        alert('Error: ' + error);
    },
    success: function (result) {
        //Do your thing
    }
});
user3783243
  • 5,098
  • 5
  • 16
  • 37
Neo
  • 3,149
  • 6
  • 31
  • 43
0

I agree with Mister Positive's comment - I'd make an AJAX call to check if the email exists, and handle the response appropriately in the front end. Essentially:

  • user submits form, make an AJAX 'POST' request
  • you take email param and perform SQL query
  • SQL query echos a response, could be a string (e.g. 'taken')
  • Then in the front end, your AJAX success function will take the (response) and if response === 'taken', do whatever you want on the front end to notify the user that the email is already taken

I'd also recommend a client-side validation at the very least to ensure the email address is in a valid email format before submitting it to the server. And make sure to use prepared statements for your SQL query (i.e. using parameters in a template query) that uses bound variables to avoid SQL injection.

Question also asked here: How to check Email already exist in db using ajax?

aryakeio
  • 11
  • 3