jQuery validation of email al bestaat.
Mijn email formulier:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
<form action="register_form.php" name="registratieformulier" method="post" id="register_form">
<div class="regform" >
<label for="email">Email:*</label>
<input type="text" name="email" id="email" value="" />
</div>
<div class="regform" >
<input type="submit" name="register" class="b_verzenden" value="verzenden"/>
</div>
</form>
<div class="regform" >
<label for="email">Email:*</label>
<input type="text" name="email" id="email" value="" />
</div>
<div class="regform" >
<input type="submit" name="register" class="b_verzenden" value="verzenden"/>
</div>
</form>
Het script:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
$().ready(function() {
$.validator.methods.email_adres = function( value, element ) {
return this.optional( element ) || /[a-z]+@[a-z]+\.[a-z]+/.test( value );
};
$.validator.methods.lettercijf = function( value, element ) {
return this.optional( element ) || /^[a-z0-9]+$/i.test(value);
};
$("#register_form").validate({
onkeyup: false,
ignore: ".ignore",
rules: {
firstname: {
required: true,
lettersonly: true,
nowhitespace: true,
minlength: 2,
maxlength: 15,
},
lastname: {
required: true,
minlength: 2,
},
email: {
email_adres: true,
required: true,
remote: {
url: "register.php",
type: "post",
data: {
email: function() {
return $("#email").val();
}
}
}
},
$.validator.methods.email_adres = function( value, element ) {
return this.optional( element ) || /[a-z]+@[a-z]+\.[a-z]+/.test( value );
};
$.validator.methods.lettercijf = function( value, element ) {
return this.optional( element ) || /^[a-z0-9]+$/i.test(value);
};
$("#register_form").validate({
onkeyup: false,
ignore: ".ignore",
rules: {
firstname: {
required: true,
lettersonly: true,
nowhitespace: true,
minlength: 2,
maxlength: 15,
},
lastname: {
required: true,
minlength: 2,
},
email: {
email_adres: true,
required: true,
remote: {
url: "register.php",
type: "post",
data: {
email: function() {
return $("#email").val();
}
}
}
},
register.php
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
// Check if user with that email already exists
$result = $connect->query("SELECT * FROM users WHERE email='$email'") or die($connect->error());
// We know user email exists if the rows returned are more than 0 als het email adres niet bestaat gaat de procedure verder.
if ( $result->num_rows > 0 ) {
echo ('true'); //good to register
}
else
{
echo ('false'); //already registered
}
$result = $connect->query("SELECT * FROM users WHERE email='$email'") or die($connect->error());
// We know user email exists if the rows returned are more than 0 als het email adres niet bestaat gaat de procedure verder.
if ( $result->num_rows > 0 ) {
echo ('true'); //good to register
}
else
{
echo ('false'); //already registered
}
Ik heb het ook nog met een apart bestandje geprobeerd ipv register.php:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
if (!empty($_POST['email']))
{
$email = $connect->real_escape_string($_POST['email']);
$query = "SELECT ID FROM users WHERE user_email = '{$email}' LIMIT 1;";
$results = $connect->query($query);
if($results->num_rows == 0)
{
echo 'true'; //good to register
}
else
{
echo 'false'; //already registered
}
}
{
$email = $connect->real_escape_string($_POST['email']);
$query = "SELECT ID FROM users WHERE user_email = '{$email}' LIMIT 1;";
$results = $connect->query($query);
if($results->num_rows == 0)
{
echo 'true'; //good to register
}
else
{
echo 'false'; //already registered
}
}
Ik weet dat de register.php goed moet zijn die geeft aan of er wel of geen email bestaat zonder jQuery.
Ook heb ik alles van deze pagina geprobeerd.
Maar bij alles geen succes.
Gewijzigd op 17/09/2017 12:35:30 door - SanThe -
$connect komt van de .
En db_connect.php:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?php
ini_set('display_errors', 'On');
error_reporting(E_ALL);
/* Database connection settings */
$db['host'] = 'localhost';
$db['gebruikersnaam'] = 'root';
$db['wachtwoord'] = '';
$db['database'] = 'accounts';
/* Create mysql connection */
$connect = new mysqli($db['host'], $db['gebruikersnaam'], $db['wachtwoord'], $db['database']);
if(mysqli_connect_errno())
{
echo 'Verbinding maken met de database is mislukt.'.$connect->error;
}
?>
ini_set('display_errors', 'On');
error_reporting(E_ALL);
/* Database connection settings */
$db['host'] = 'localhost';
$db['gebruikersnaam'] = 'root';
$db['wachtwoord'] = '';
$db['database'] = 'accounts';
/* Create mysql connection */
$connect = new mysqli($db['host'], $db['gebruikersnaam'], $db['wachtwoord'], $db['database']);
if(mysqli_connect_errno())
{
echo 'Verbinding maken met de database is mislukt.'.$connect->error;
}
?>
$email komt van:
Heb je al in de Network-tab van je browser gekeken of de register.php (niet echt een duidelijke naam voor eejn e-mail controle) ingeladen wordt?
$email = $connect->escape_string($_POST['email']);
En zitten deze regels ook in register.php?
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<?php
/* Registration process, inserts user info into the database
and sends account confirmation email message
*/
// Set session variables to be used on profile.php page
$_SESSION['email'] = $_POST['email'];
$_SESSION['first_name'] = $_POST['firstname'];
$_SESSION['last_name'] = $_POST['lastname'];
// Escape all $_POST variables to protect against SQL injections
$first_name = $connect->escape_string($_POST['firstname']);
$last_name = $connect->escape_string($_POST['lastname']);
$email = $connect->escape_string($_POST['email']);
$password = $connect->escape_string(password_hash($_POST['password'], PASSWORD_BCRYPT));
$hash = $connect->escape_string( md5( rand(0,1000) ) );
$password_herh = $connect->escape_string($_POST['password_herh']);
// Check if user with that email already exists
$result = $connect->query("SELECT * FROM users WHERE email='$email'") or die($connect->error());
// We know user email exists if the rows returned are more than 0 als het email adres niet bestaat gaat de procedure verder.
if ( $result->num_rows > 0 ) {
echo ('true'); //good to register
}
else
{
echo ('false'); //already registered
}
// active is 0 by DEFAULT (no need to include it here)
$sql = "INSERT INTO users (first_name, last_name, email, password, hash) "
. "VALUES ('$first_name','$last_name','$email','$password', '$hash')";
// Add user to the database
if ( $connect->query($sql) ){
$_SESSION['active'] = 0; //0 until user activates their account with verify.php
$_SESSION['logged_in'] = true; // So we know the user has logged in
$_SESSION['message'] =
"Confirmation link has been sent to $email, please verify
your account by clicking on the link in the message!";
// Send registration confirmation link (verify.php)
$to = $email;
$subject = 'Account Verification';
$message_body = '
Hello '.$first_name.',
Thank you for signing up!
Please click this link to activate your account:
http://localhost/verify.php?email='.$email.'&hash='.$hash;
mail( $to, $subject, $message_body );
header("location: profile.php");
}
else {
$_SESSION['message'] = 'Registration failed!';
header("location: error.php");
}
//Google reCaptcha
$email = $_POST['email'];
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'https://www.google.com/recaptcha/api/siteverify',
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => [
'secret' => 'x',
'response' => $_POST['g-recaptcha-response'],
],
]);
$response = json_decode(curl_exec($curl));
?>
/* Registration process, inserts user info into the database
and sends account confirmation email message
*/
// Set session variables to be used on profile.php page
$_SESSION['email'] = $_POST['email'];
$_SESSION['first_name'] = $_POST['firstname'];
$_SESSION['last_name'] = $_POST['lastname'];
// Escape all $_POST variables to protect against SQL injections
$first_name = $connect->escape_string($_POST['firstname']);
$last_name = $connect->escape_string($_POST['lastname']);
$email = $connect->escape_string($_POST['email']);
$password = $connect->escape_string(password_hash($_POST['password'], PASSWORD_BCRYPT));
$hash = $connect->escape_string( md5( rand(0,1000) ) );
$password_herh = $connect->escape_string($_POST['password_herh']);
// Check if user with that email already exists
$result = $connect->query("SELECT * FROM users WHERE email='$email'") or die($connect->error());
// We know user email exists if the rows returned are more than 0 als het email adres niet bestaat gaat de procedure verder.
if ( $result->num_rows > 0 ) {
echo ('true'); //good to register
}
else
{
echo ('false'); //already registered
}
// active is 0 by DEFAULT (no need to include it here)
$sql = "INSERT INTO users (first_name, last_name, email, password, hash) "
. "VALUES ('$first_name','$last_name','$email','$password', '$hash')";
// Add user to the database
if ( $connect->query($sql) ){
$_SESSION['active'] = 0; //0 until user activates their account with verify.php
$_SESSION['logged_in'] = true; // So we know the user has logged in
$_SESSION['message'] =
"Confirmation link has been sent to $email, please verify
your account by clicking on the link in the message!";
// Send registration confirmation link (verify.php)
$to = $email;
$subject = 'Account Verification';
$message_body = '
Hello '.$first_name.',
Thank you for signing up!
Please click this link to activate your account:
http://localhost/verify.php?email='.$email.'&hash='.$hash;
mail( $to, $subject, $message_body );
header("location: profile.php");
}
else {
$_SESSION['message'] = 'Registration failed!';
header("location: error.php");
}
//Google reCaptcha
$email = $_POST['email'];
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'https://www.google.com/recaptcha/api/siteverify',
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => [
'secret' => 'x',
'response' => $_POST['g-recaptcha-response'],
],
]);
$response = json_decode(curl_exec($curl));
?>
register.php is niet alleen voor de email maar voor het hele formulier.
Ik heb niet in de Network-tab van de browser gekeken maar register.php werkt verder goed alles komt in de database terecht.
Gewijzigd op 17/09/2017 23:31:14 door - Ariën -
Ik zou dus liever een extra scriptje maken die je ajax_validatemail.php noemt, en daar controleer aan de hand van je POST-waarde of de mailadres wel of niet is goedgekeurd. Dan geef je bijvoorbeeld een string true of false door. De registratie zelf gebeurt na je het posten van je formulier, is en een uitgebreider proces dan enkel het valideren.
Gewijzigd op 17/09/2017 23:37:24 door - Ariën -
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
if (!empty($_POST['email']))
{
$email = $connect->real_escape_string($_POST['email']);
$query = "SELECT ID FROM users WHERE user_email = '{$email}' LIMIT 1;";
$results = $connect->query($query);
if($results->num_rows == 0)
{
echo 'true'; //good to register
}
else
{
echo 'false'; //already registered
}
}
{
$email = $connect->real_escape_string($_POST['email']);
$query = "SELECT ID FROM users WHERE user_email = '{$email}' LIMIT 1;";
$results = $connect->query($query);
if($results->num_rows == 0)
{
echo 'true'; //good to register
}
else
{
echo 'false'; //already registered
}
}
Met helemaal boven aan dan nog de
Maar wat zegt de Network tab over de request?
Zegt jou dit iets?
Ik dacht dat het de captcha was maar nu met de tab naar een ander veld komt die 304 er weer ik zie alleen zo snel nog niet wat de oplossing ervoor is.
Gewijzigd op 18/09/2017 00:05:33 door Erwin b
Je moet naar de request kijken van je PHP-script die via AJAX opgevraagd wordt.
Code (php)
1
2
3
2
3
<br />
<b>Notice</b>: Trying to get property of non-object in <b>C:\xampp\htdocs\validateEmail.php</b> on line <b>15</b><br />
true
<b>Notice</b>: Trying to get property of non-object in <b>C:\xampp\htdocs\validateEmail.php</b> on line <b>15</b><br />
true
En dit is er aangevraagd:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Host
localhost
User-Agent
Mozilla/5.0 (Windows NT 10.0; ) Gecko/20100101 Firefox/55.0
Accept
application/json, text/javascript, */*; q=0.01
Accept-Language
nl,en-US;q=0.7,en;q=0.3
Accept-Encoding
gzip, deflate
Referer
http://localhost/register_form.php
Content-Type
application/x-www-form-urlencoded; charset=UTF-8
X-Requested-With
XMLHttpRequest
Content-Length
15
Cookie
PHPSESSID=eje0ncvfdqcm86ksk3sj9htp46
Connection
keep-alive
localhost
User-Agent
Mozilla/5.0 (Windows NT 10.0; ) Gecko/20100101 Firefox/55.0
Accept
application/json, text/javascript, */*; q=0.01
Accept-Language
nl,en-US;q=0.7,en;q=0.3
Accept-Encoding
gzip, deflate
Referer
http://localhost/register_form.php
Content-Type
application/x-www-form-urlencoded; charset=UTF-8
X-Requested-With
XMLHttpRequest
Content-Length
15
Cookie
PHPSESSID=eje0ncvfdqcm86ksk3sj9htp46
Connection
keep-alive
Gewijzigd op 18/09/2017 00:43:12 door Erwin b
Gewijzigd op 18/09/2017 06:01:46 door - Ariën -
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?php
// Maak verbinding met de database
require 'db_connect.php';
// Toon alle foutmeldingen
ini_set( 'display_errors', true );
error_reporting( E_ALL | E_STRICT );
if (!empty($_POST['email']))
{
$email = $connect->real_escape_string($_POST['email']);
$query = "SELECT ID FROM users WHERE user_email = '{$email}' LIMIT 1;";
$results = $connect->query($query);
if($results->num_rows == 0)
{
echo 'true'; //good to register
}
else
{
echo 'fals'; //already registered
}
}
?>
// Maak verbinding met de database
require 'db_connect.php';
// Toon alle foutmeldingen
ini_set( 'display_errors', true );
error_reporting( E_ALL | E_STRICT );
if (!empty($_POST['email']))
{
$email = $connect->real_escape_string($_POST['email']);
$query = "SELECT ID FROM users WHERE user_email = '{$email}' LIMIT 1;";
$results = $connect->query($query);
if($results->num_rows == 0)
{
echo 'true'; //good to register
}
else
{
echo 'fals'; //already registered
}
}
?>
haal die ; eens weg na je LIMIT 1
Ook geen succes. Ook werkt het hele registreren niet meer zodra er remote in de validatie.js staat. Dan werkt de submit niet meer.
Ik zou eens zeggen: Kijk eens naar de request in je network-tab. Misschien worden er nog meer parameters gevraagd, dan dat je afhandelt?
Is er eventueel een testcase die ik we kunnen bekijken?
Alles is lokaal met xampp. Ik zal alles eens verwijderen en alleen het geen bewaren wat nodig is voor dit.
Word je ook niet wijzer uit de JavaScript Debug Console van je browser?
register_form.php
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?php
// Maak verbinding met de database
require 'db_connect.php';
// Begin the session
session_start();
// Toon alle foutmeldingen
ini_set( 'display_errors', true );
error_reporting( E_ALL | E_STRICT );
?>
<!DOCTYPE html>
<html lang="nl-NL">
<head>
</head>
<body>
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.2.1.min.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.13.0/jquery.validate.min.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.13.0/additional-methods.min.js"></script>
<script src='https://www.google.com/recaptcha/api.js'></script>
<script src="/js/validation.js"></script>
<h1>Registreer</h1><br /><br />
<form action="register_form.php" name="registratieformulier" method="post" id="register_form">
<!-- <fieldset> -->
<div class="regform" >
<label for="email">Email:*</label>
<input type="text" name="email" id="email" value="" />
</div>
<div class="regform" >
<label for="password">Wachtwoord:*</label>
<input type="password" name="password" id="password" value="" />
</div>
<div class="regform" >
<input type="submit" name="register" class="b_verzenden" value="verzenden"/>
</div>
<!-- </fieldset> -->
</form>
</body>
</html>
// Maak verbinding met de database
require 'db_connect.php';
// Begin the session
session_start();
// Toon alle foutmeldingen
ini_set( 'display_errors', true );
error_reporting( E_ALL | E_STRICT );
?>
<!DOCTYPE html>
<html lang="nl-NL">
<head>
</head>
<body>
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.2.1.min.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.13.0/jquery.validate.min.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.13.0/additional-methods.min.js"></script>
<script src='https://www.google.com/recaptcha/api.js'></script>
<script src="/js/validation.js"></script>
<h1>Registreer</h1><br /><br />
<form action="register_form.php" name="registratieformulier" method="post" id="register_form">
<!-- <fieldset> -->
<div class="regform" >
<label for="email">Email:*</label>
<input type="text" name="email" id="email" value="" />
</div>
<div class="regform" >
<label for="password">Wachtwoord:*</label>
<input type="password" name="password" id="password" value="" />
</div>
<div class="regform" >
<input type="submit" name="register" class="b_verzenden" value="verzenden"/>
</div>
<!-- </fieldset> -->
</form>
</body>
</html>
Het validatie script validation.js
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
$().ready(function() {
$.validator.methods.email_adres = function( value, element ) {
return this.optional( element ) || /[a-z]+@[a-z]+\.[a-z]+/.test( value );
};
$("#register_form").validate({
rules: {
email: {
email_adres: true,
required: true,
remote: {
url: "ajax_validatemail.php",
type: "post",
data: {
email: function() {
return $("#email").val();
}
}
}
},
},
messages: {
email: {
required: 'Vul een email adres in.',
email_adres: 'Vul een geldig email adres in.',
remote: 'Email adres bestaat al.',
},
},
submitHandler: function(form)
{
form.submit();
},
});
});
$.validator.methods.email_adres = function( value, element ) {
return this.optional( element ) || /[a-z]+@[a-z]+\.[a-z]+/.test( value );
};
$("#register_form").validate({
rules: {
email: {
email_adres: true,
required: true,
remote: {
url: "ajax_validatemail.php",
type: "post",
data: {
email: function() {
return $("#email").val();
}
}
}
},
},
messages: {
email: {
required: 'Vul een email adres in.',
email_adres: 'Vul een geldig email adres in.',
remote: 'Email adres bestaat al.',
},
},
submitHandler: function(form)
{
form.submit();
},
});
});
en dan alleen nog ajax_validatemail.php
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?php
// Maak verbinding met de database
require 'db_connect.php';
// Toon alle foutmeldingen
ini_set( 'display_errors', true );
error_reporting( E_ALL | E_STRICT );
if (!empty($_POST['email']))
{
$email = $connect->real_escape_string($_POST['email']);
$query = "SELECT ID FROM users WHERE user_email = '{$email}' LIMIT 1";
$results = $connect->query($query);
if($results->num_rows > 0)
{
echo 'true'; //good to register
}
else
{
echo 'fals'; //already registered
}
}
?>
// Maak verbinding met de database
require 'db_connect.php';
// Toon alle foutmeldingen
ini_set( 'display_errors', true );
error_reporting( E_ALL | E_STRICT );
if (!empty($_POST['email']))
{
$email = $connect->real_escape_string($_POST['email']);
$query = "SELECT ID FROM users WHERE user_email = '{$email}' LIMIT 1";
$results = $connect->query($query);
if($results->num_rows > 0)
{
echo 'true'; //good to register
}
else
{
echo 'fals'; //already registered
}
}
?>
En dan in de database een email adres dat ik exact zo overneem bij het invullen. Bij de Debug Console geeft ie niks aan van een fout ofzo. Het moet toch niet uitmaken dat het lokaal is?
Ik heb het veld wachtwoord even toegevoegd voor de tab naar een ander veld. Al doet tab naar submit het zelfde waarschijnlijk.
Krijg je nou nog steeds die Notice-melding bij het uitvoeren te zien bij het bekijken van de request van ajax_validatemail.php? En welke antwoord- en verzoekheaders worden er uitgevoerd?