Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $action = isset($_GET['action']) ? $_GET['action'] : null;
- function registerUser($user)
- {
- // inserir usuario no banco, e retornar se foi realmente criado (boleano).
- }
- function checkUser($email)
- {
- // verifica se usuário existe e retorna um boleano.
- }
- if ($action) {
- $resultado = [
- 'userExists' => false,
- 'userCreated' => false
- ];
- switch ($action) {
- case 'userExists':
- $body = $_POST['user'];
- $resposta['userCreated'] = registerUser($body);
- break;
- case 'registerUser':
- $email = isset($_GET['email']) ? $_GET['email'] : null;
- $resultado['userExists'] = checkUser($email);
- break;
- }
- exit($resposta);
- }
- ?>
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <meta http-equiv="X-UA-Compatible" content="ie=edge">
- <title>Document</title>
- </head>
- <body>
- <script>
- $.ajax({
- url: 'index.php?action=userExists&email=user@email.com',
- type: 'GET',
- success: function(response) {
- if (response.userExists) {
- console.log('Usuario já existe');
- } else {
- $.ajax({
- url: 'index.php?action=registerUser',
- type: 'POST',
- data: {
- email: 'user@email.com',
- name: 'UserName',
- phone: '(xx)xxxxx-xxxx'
- }
- success: function(response) {
- if (response.userCreated) {
- console.log('Usuário criado com sucesso');
- }
- }
- })
- }
- }
- });
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement