Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $usuarios = array(
- array('login' => 'fulano', 'email' => 'fulano@exemplo.com'),
- array('login' => 'beltrano', 'email' => 'beltrano@exemplo.com'),
- array('login' => 'siclano', 'email' => 'siclano@exemplo.com')
- );
- if($_POST) {
- $login = $_POST['login'];
- $email = $_POST['email'];
- $encontrado = array();
- foreach($usuarios as $usuario) {
- if($usuario['login'] === $login)
- $encontrado['login'] = true;
- if($usuario['email'] === $email)
- $encontrado['email'] = true;
- }
- header('Content-type: application/json');
- $json = array();
- if($encontrado) {
- $json['status'] = false;
- $json['message'] = "Login ou email em uso.";
- $json['data'] = $encontrado;
- } else {
- $json['status'] = true;
- $json['message'] = "Cadastro efetuado com sucesso.";
- }
- exit(json_encode($json));
- }
- ?>
- <html>
- <head>
- <title>Teste</title>
- </head>
- <body>
- <form id="myForm" method="post">
- <label>
- Login
- <input name="login">
- </label>
- <label>
- Email
- <input name="email" type="email">
- </label>
- <input type="submit" value="Enviar">
- </form>
- <script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
- <script>
- jQuery(function($) {
- var $myForm = $('#myForm');
- $myForm.submit(function() {
- $.post(window.location.href, $myForm.serialize(), function(response) {
- if(response.status) {
- $('input').css('border-color', '');
- } else {
- for(field in response.data) {
- $('[name=' + field + ']').css('border-color', 'red');
- }
- }
- alert(response.message);
- });
- return false;
- });
- });
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement