WEBVTT 00:00:07.230 --> 00:00:12.580 In this video we're going to write code which will enable the user to change their email address. 00:00:12.870 --> 00:00:19.050 So first of all let's change this value of the email to the actual e-mail of how we use it. 00:00:19.380 --> 00:00:19.950 OK. 00:00:20.220 --> 00:00:27.690 So very simply we're going to go to the PSP file and use the e-mail variable that we created at the 00:00:27.690 --> 00:00:28.320 top. 00:00:28.490 --> 00:00:30.020 OK. 00:00:31.590 --> 00:00:32.040 So 00:00:44.990 --> 00:00:46.720 that's the first one. 00:00:46.740 --> 00:00:52.380 So you get to have some PSP code in the echo this variable 00:00:54.470 --> 00:00:56.290 . 00:00:56.910 --> 00:01:01.080 Let's do the same thing inside the email form 00:01:05.210 --> 00:01:07.810 OK. 00:01:07.870 --> 00:01:11.520 And here we go. 00:01:12.690 --> 00:01:17.050 That's the one we're looking for. 00:01:17.050 --> 00:01:18.730 All right. 00:01:19.210 --> 00:01:26.040 Now let's let's change the logging message to updates e-mail message 00:01:29.870 --> 00:01:32.550 and also the ID of that message to 00:01:36.010 --> 00:01:40.620 update e-mail message. 00:01:41.710 --> 00:01:49.780 Now the label goes to e-mail not logging e-mail. 00:01:49.780 --> 00:01:56.030 All right so it's save as refresh. 00:01:57.400 --> 00:02:02.020 OK this works perfectly. 00:02:02.160 --> 00:02:04.690 Now we're going to start with the AJAX call. 00:02:05.160 --> 00:02:08.490 So we get a copy another AJAX call 00:02:11.580 --> 00:02:17.360 and then amend it. 00:02:17.360 --> 00:02:28.560 All right so the ID of our address e-mail form is a data e-mail form. 00:02:28.810 --> 00:02:33.500 All right. 00:02:37.200 --> 00:02:41.990 And the file We're going to send the AJAX call to is update. 00:02:42.370 --> 00:02:44.070 Email those BHB 00:02:44.070 --> 00:02:52.160 . 00:02:52.570 --> 00:02:57.810 All right. 00:02:59.370 --> 00:03:04.350 The idea of the error message Dave is updates e-mail 00:03:07.260 --> 00:03:10.980 message. 00:03:14.310 --> 00:03:15.150 OK. 00:03:15.150 --> 00:03:20.510 So let's just call works the same way as the one we used for updating the password. 00:03:20.580 --> 00:03:27.570 If anything anything goes wrong the page file is going to return something which we all saw inside the 00:03:27.570 --> 00:03:34.320 data very well and then going to display that error message inside the abbe's e-mail message. 00:03:34.320 --> 00:03:34.980 Dave. 00:03:35.370 --> 00:03:39.280 OK so let's save our age school. 00:03:39.290 --> 00:03:41.330 Now it go to the APHC file. 00:03:41.940 --> 00:03:47.700 So first of all we're going to start a session and connect 00:03:51.220 --> 00:03:51.490 OK 00:03:55.410 --> 00:04:00.460 and then we're going to guess the user ID. 00:04:01.280 --> 00:04:01.870 OK. 00:04:02.100 --> 00:04:07.800 As well as the new e-mail which is supposed to be sent through the AJAX call. 00:04:08.010 --> 00:04:09.600 OK. 00:04:10.560 --> 00:04:18.690 So it gets the user id and new e-mail. 00:04:21.300 --> 00:04:26.090 Then we go in to check if the new e-mail exists. 00:04:26.310 --> 00:04:26.960 OK. 00:04:27.450 --> 00:04:33.850 So if the new e-mail exists then we're not going to allow the user to use does e-mail address. 00:04:33.890 --> 00:04:36.780 OK because it's already taken by another user. 00:04:36.780 --> 00:04:42.250 So check if new e-mail exists. 00:04:43.930 --> 00:04:50.660 Then we are going to get the current e-mail of our user. 00:04:50.940 --> 00:04:51.980 OK. 00:04:52.480 --> 00:04:59.530 Because we're going to send them an e-mail address to make sure they own that e-mail address they are 00:04:59.520 --> 00:05:00.260 using. 00:05:00.610 --> 00:05:01.360 OK. 00:05:01.550 --> 00:05:06.900 So get the current e-mail of our user. 00:05:08.130 --> 00:05:18.880 And then we're going to do before sending the e-mail address we're going to create a unique activation 00:05:18.880 --> 00:05:19.740 code. 00:05:19.810 --> 00:05:23.090 So how are we going to use that activation code. 00:05:23.130 --> 00:05:25.050 Basically we use an activation code. 00:05:25.080 --> 00:05:27.100 When we did the sign up process. 00:05:27.100 --> 00:05:32.310 Now we going to add another column to our users table. 00:05:32.310 --> 00:05:37.250 We're going to call it excavation 2 and that's where we're going to store the new activation code. 00:05:37.310 --> 00:05:45.520 We're going to use the activation code when we send the e-mail to our user to check and make sure that 00:05:45.510 --> 00:05:50.200 they own the new e-mail address they want to use. 00:05:50.190 --> 00:05:51.120 OK. 00:05:51.730 --> 00:05:56.140 So let's create 00:05:58.770 --> 00:06:01.890 a unique activation 00:06:07.080 --> 00:06:07.680 code. 00:06:07.820 --> 00:06:09.530 OK. 00:06:10.320 --> 00:06:16.090 And then we're going to insert the new activation code 00:06:20.800 --> 00:06:23.520 in our users table. 00:06:24.500 --> 00:06:26.370 All right. 00:06:26.380 --> 00:06:35.100 So before we continue let's create this new column it's going to have the same structure as the activation 00:06:35.110 --> 00:06:36.910 column. 00:06:38.400 --> 00:06:43.030 OK. 00:06:44.400 --> 00:06:48.990 Which means we're going to give it's 32 characters for the length. 00:06:49.400 --> 00:06:50.390 OK. 00:06:50.940 --> 00:06:51.410 And that's it 00:06:51.490 --> 00:06:58.350 . 00:06:58.950 --> 00:07:02.030 So we're going to add one column after activation 00:07:11.450 --> 00:07:13.300 in use char. 00:07:13.720 --> 00:07:14.510 OK. 00:07:14.700 --> 00:07:21.910 And let's Nimi excavation to find it safe. 00:07:23.250 --> 00:07:23.490 OK 00:07:24.480 --> 00:07:37.580 . 00:07:38.390 --> 00:07:40.580 And I cause an extra column there. 00:07:40.650 --> 00:07:41.090 OK. 00:07:41.250 --> 00:07:45.730 Where we can where we can store our new activation. 00:07:45.730 --> 00:07:46.020 All right. 00:07:46.020 --> 00:07:48.780 So next 00:07:51.730 --> 00:07:56.250 we're going to send an e-mail to our user. 00:07:57.370 --> 00:08:03.280 OK so that's e-mail is going to contain a link exactly the same way we did in the sign up process. 00:08:03.390 --> 00:08:06.730 But this time the link is going to go to another file. 00:08:07.000 --> 00:08:10.270 We're going to call activate new e-mail. 00:08:11.130 --> 00:08:11.940 OK. 00:08:11.940 --> 00:08:16.050 So that's file is going to process the data. 00:08:16.440 --> 00:08:18.360 But we're going to store inside that link. 00:08:18.450 --> 00:08:19.120 OK. 00:08:19.120 --> 00:08:25.610 So the link is going to contain the current e-mail. 00:08:26.230 --> 00:08:26.680 OK. 00:08:26.680 --> 00:08:31.510 Then you e-mail and the excavation code. 00:08:32.250 --> 00:08:33.190 OK. 00:08:33.370 --> 00:08:39.430 The whole purpose of this is to make sure as the user owns the new e-mail address that they'd like to 00:08:39.420 --> 00:08:40.040 use. 00:08:40.290 --> 00:08:41.690 OK. 00:08:42.610 --> 00:08:49.080 So these are the steps so let's go ahead and start writing code so to start the session and connect 00:08:49.090 --> 00:08:49.420 . 00:08:49.680 --> 00:08:52.400 We're going to do exactly the same thing we did before. 00:08:52.810 --> 00:08:56.390 So sessions starts on include the connection file. 00:08:56.670 --> 00:08:57.890 OK here we go. 00:08:57.930 --> 00:09:04.670 Now we going to guess the user id and the new e-mail which is sent you through the AJAX call. 00:09:04.680 --> 00:09:16.340 So let's create a variable for each user ID we can give the user id from the session. 00:09:18.700 --> 00:09:19.400 OK. 00:09:19.410 --> 00:09:20.010 Like this 00:09:26.730 --> 00:09:35.970 and then the new email is quality new e-mailing us. 00:09:37.770 --> 00:09:40.620 We're going to get it from the post array 00:09:43.390 --> 00:09:47.930 as we use the post method in our AJAX call. 00:09:48.690 --> 00:09:50.910 OK. 00:09:53.130 --> 00:09:54.740 OK so if you look at the 00:09:57.670 --> 00:10:08.340 IPs e-mail form you will see that's the name corresponding to the email field is e-mail. 00:10:08.610 --> 00:10:10.120 OK. 00:10:11.340 --> 00:10:21.240 All right so all right now we're going to check if the new e-mail provided by our user already exists 00:10:21.290 --> 00:10:23.030 in our users table. 00:10:23.220 --> 00:10:23.890 OK. 00:10:24.440 --> 00:10:24.710 So 00:10:28.010 --> 00:10:37.060 first of all we're going to write a query which will check if that e-mail exists in our table. 00:10:37.830 --> 00:10:39.020 So we're going to select 00:10:44.750 --> 00:10:48.740 records from our users table. 00:10:49.000 --> 00:10:49.270 OK. 00:10:49.340 --> 00:10:53.440 You can do select e-mail if you want the same thing. 00:10:53.490 --> 00:10:56.780 The only thing is does this is going to return the whole record. 00:10:56.780 --> 00:10:59.660 Basically the whole line for each record. 00:10:59.690 --> 00:11:02.510 OK so selects from users where 00:11:06.870 --> 00:11:16.040 we have the same e-mail where our e-mail is the same as that e-mail. 00:11:17.020 --> 00:11:17.970 OK. 00:11:18.600 --> 00:11:25.520 Now let's run the query. 00:11:29.880 --> 00:11:39.860 It's going to take out a link variable and query and we saw the results inside the results object. 00:11:39.920 --> 00:11:45.470 Now we're going to check how many records we've got inside the results objects. 00:11:45.570 --> 00:11:46.220 OK. 00:11:46.500 --> 00:11:57.030 So we're going to define a variable is called count as we did in the update password. 00:11:57.160 --> 00:11:58.780 OK. 00:11:59.780 --> 00:12:09.770 So this variable is going to tell us how many rows we found in the results objects or right now if a 00:12:09.770 --> 00:12:15.500 similar email exists already in our database then county is going to be greater than zero. 00:12:15.720 --> 00:12:18.770 So if gown's is greater than zero 00:12:21.430 --> 00:12:25.300 is means just the email already exists. 00:12:25.320 --> 00:12:26.960 Another user is using us. 00:12:27.200 --> 00:12:30.840 So we're going to echo an error message. 00:12:30.890 --> 00:12:33.050 All right. 00:12:33.500 --> 00:12:38.850 I can see the code straight away else. 00:12:40.470 --> 00:12:42.500 OK we don't need an else statement here. 00:12:42.500 --> 00:12:44.580 If you use X it's OK. 00:12:44.610 --> 00:12:48.800 So the error message can be something like 00:12:55.670 --> 00:12:56.550 there is 00:13:00.000 --> 00:13:09.500 already a user registered with that email. 00:13:10.230 --> 00:13:14.290 Please choose another one. 00:13:16.230 --> 00:13:16.650 OK. 00:13:16.890 --> 00:13:22.070 Soileau Dave give me a couple of glasses. 00:13:23.510 --> 00:13:28.230 Let's let's change on it 00:13:31.910 --> 00:13:32.150 now. 00:13:32.150 --> 00:13:38.690 If the email already exists then we're going to exceed the code right there after echoing the error 00:13:38.690 --> 00:13:40.040 message. 00:13:40.050 --> 00:13:45.710 Now we're going to guess the current e-mail of our user because he wants to send them an e-mail. 00:13:45.880 --> 00:13:52.430 Given that everything is fine and we get here so if we get here that means that's the e-mail chosen 00:13:52.430 --> 00:13:56.620 by our user is not already taken by somebody else. 00:13:57.000 --> 00:13:58.440 OK. 00:13:58.430 --> 00:14:07.520 So to get the current e-mail we can do the same thing we did in our profile file right at the top when 00:14:07.520 --> 00:14:14.530 we managed to get the e-mail address as well as the user name y user. 00:14:14.930 --> 00:14:18.510 You can do this using sessions. 00:14:18.680 --> 00:14:19.560 OK. 00:14:19.560 --> 00:14:25.640 It's something that you can do but in our case we're dealing with a scenario where the user is changing 00:14:25.640 --> 00:14:28.060 the email and changing their user name. 00:14:28.350 --> 00:14:30.500 So if you store something inside a session 00:14:33.300 --> 00:14:38.310 and then you refer to that variable after changing something in the database then you're going to get 00:14:38.850 --> 00:14:40.320 completely confused. 00:14:40.480 --> 00:14:41.190 OK. 00:14:41.460 --> 00:14:49.620 There is a way of using sessions given that whenever you update the user name or email you make sure 00:14:49.740 --> 00:14:53.250 that you update the variable or the value inside the session as well. 00:14:53.550 --> 00:14:54.150 OK. 00:14:54.510 --> 00:15:00.510 To make things simple I'm going to use the same way we used in the profile file. 00:15:01.050 --> 00:15:03.300 Ok so what is going to copy this code. 00:15:03.470 --> 00:15:07.760 We cause the user name and email. 00:15:07.910 --> 00:15:10.350 I mean I keep only the email for now 00:15:12.480 --> 00:15:20.760 . 00:15:25.220 --> 00:15:26.120 OK. 00:15:26.700 --> 00:15:32.420 And then if I echo that I'm going to actually the code right. 00:15:33.970 --> 00:15:41.700 And it's just change the error message to there was an error retrieving the email only from the database 00:15:41.700 --> 00:15:42.510 . 00:15:42.540 --> 00:15:43.170 OK. 00:15:43.200 --> 00:15:47.930 A comments about using exit's can use exits wherever you want the code to stop. 00:15:48.120 --> 00:15:54.540 OK so if we don't exits here it's still fine but it's going to continue. 00:15:54.560 --> 00:15:55.080 OK. 00:15:55.290 --> 00:16:02.010 So when you look at previous code sometimes I did not use exits in that case the use code will still 00:16:02.750 --> 00:16:08.250 just continue until the end which is which is fine in some cases but sometimes you want the code to 00:16:08.250 --> 00:16:11.130 just stop right there and that's where you use exits. 00:16:11.390 --> 00:16:11.870 OK. 00:16:12.330 --> 00:16:20.610 So it's save and let's continue so now we're going to create a unique activation code which we will 00:16:20.670 --> 00:16:30.190 use to update the new field inside our table. 00:16:30.210 --> 00:16:31.170 All right. 00:16:31.800 --> 00:16:39.140 So to create an activation code we can use the same code we used earlier in the Sign-Up file. 00:16:39.180 --> 00:16:39.940 OK. 00:16:40.560 --> 00:16:49.800 So it goes to the sign of file and look at the piece of code we use to create a unique activation code 00:16:49.820 --> 00:16:50.300 . 00:16:50.790 --> 00:16:51.510 So here we go 00:16:55.550 --> 00:16:59.390 is using the same code. 00:16:59.580 --> 00:17:08.710 Ok so now we've called a random number generator and stored inside the variable acceleration key. 00:17:09.000 --> 00:17:09.920 OK. 00:17:10.050 --> 00:17:15.720 Now we're going to insert this new activation code in our users table. 00:17:15.720 --> 00:17:16.370 All right. 00:17:16.560 --> 00:17:20.180 So let's write the query. 00:17:22.230 --> 00:17:30.330 So basically we're going to do just going to update our user or uses recode and inserts that number 00:17:30.330 --> 00:17:32.010 in here. 00:17:32.010 --> 00:17:37.050 All right so the syntax is update our table users. 00:17:37.400 --> 00:17:46.700 How are we going to set the column field or the acceleration to field elevation 00:17:49.320 --> 00:17:50.290 to. 00:17:50.580 --> 00:17:57.180 We're going to say that to the value of acceleration key like that. 00:17:57.870 --> 00:18:03.000 And the condition is where a user ID 00:18:11.910 --> 00:18:17.730 is the same as that one. 00:18:19.620 --> 00:18:20.950 OK. 00:18:21.630 --> 00:18:25.570 Now let's run a query. 00:18:26.280 --> 00:18:27.230 OK. 00:18:27.570 --> 00:18:32.490 And let's check if anything went wrong 00:18:38.250 --> 00:18:43.640 in that case we're going to echo the error message. 00:18:45.140 --> 00:18:46.330 OK. 00:18:46.630 --> 00:18:48.600 Else we are going to 00:18:51.390 --> 00:18:54.860 send an e-mail to our user. 00:18:55.580 --> 00:19:04.940 OK guys so let's writes something in the error message. 00:19:06.420 --> 00:19:08.770 By the way we need a Dave there 00:19:08.940 --> 00:19:16.850 . 00:19:17.490 --> 00:19:17.930 That's 00:19:24.540 --> 00:19:27.290 OK so let's write something in site or error message. 00:19:27.630 --> 00:19:37.400 So there was an error in setting the user details in the database. 00:19:38.920 --> 00:19:40.690 OK. 00:19:41.730 --> 00:19:47.140 Now let's look at our e-mail. 00:19:47.160 --> 00:19:52.160 We'll do this in the next video.