Advertisement
WPDeveloper

Untitled

May 22nd, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.99 KB | None | 0 0
  1. <?php
  2.  
  3. $action = isset($_GET['action']) ? $_GET['action'] : null;
  4.  
  5. function registerUser($user)
  6. {
  7.     // inserir usuario no banco, e retornar se foi realmente criado (boleano).
  8. }
  9.  
  10. function checkUser($email)
  11. {
  12.     // verifica se usuário existe e retorna um boleano.
  13. }
  14.  
  15. if ($action) {
  16.     $resultado = [
  17.         'userExists'    => false,
  18.         'userCreated'   => false
  19.     ];
  20.  
  21.     switch ($action) {
  22.         case 'userExists':
  23.             $body = $_POST['user'];
  24.             $resposta['userCreated'] = registerUser($body);
  25.             break;
  26.  
  27.         case 'registerUser':
  28.             $email = isset($_GET['email']) ? $_GET['email'] : null;
  29.             $resultado['userExists'] = checkUser($email);
  30.             break;
  31.     }
  32.     exit($resposta);
  33. }
  34. ?>
  35. <!DOCTYPE html>
  36. <html lang="en">
  37.  
  38. <head>
  39.     <meta charset="UTF-8">
  40.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  41.     <meta http-equiv="X-UA-Compatible" content="ie=edge">
  42.     <title>Document</title>
  43. </head>
  44.  
  45. <body>
  46.  
  47.     <script>
  48.         $.ajax({
  49.             url: 'index.php?action=userExists&email=user@email.com',
  50.             type: 'GET',
  51.             success: function(response) {
  52.                 if (response.userExists) {
  53.                     console.log('Usuario já existe');
  54.                 } else {
  55.                     $.ajax({
  56.                         url: 'index.php?action=registerUser',
  57.                         type: 'POST',
  58.                         data: {
  59.                             email: 'user@email.com',
  60.                             name: 'UserName',
  61.                             phone: '(xx)xxxxx-xxxx'
  62.                         }
  63.                         success: function(response) {
  64.                             if (response.userCreated) {
  65.                                 console.log('Usuário criado com sucesso');
  66.                             }
  67.                         }
  68.                     })
  69.                 }
  70.             }
  71.         });
  72.     </script>
  73. </body>
  74.  
  75. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement