dongzhi6905 2014-12-28 21:20
浏览 83
已采纳

Ajax代码在jquery表单验证中不起作用

I want to write (register section) code that can check if email have been used in past or the login is empty.

The code is working fine, but my ajax code dont run at all.

I checked everything, path to php file is good, variables are good etc. Don't how to solve it.

Code:

$('.login').submit(function(e) {

    e.preventDefault();
    var error = 0;
    var self = $(this);

    var $name = self.find('[type=name]');
    var $email = self.find('[type=email]');
    var $pass = self.find('[type=password]');

    var emailRegex = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;

    if (!emailRegex.test($email.val())) {
        createErrTult("Błąd! Email ma zły format!", $email)
        error++;
    }

    //MY AJAX CODE
    var email = $email.val();

    $.ajax({
        url: '../inc/rejestracja.php?action=emailcheck',
        data: {
            'email': email
        },
        type: 'POST',
        success: function(odp) {
            if (odp == 1) {
                createErrTult("Błąd! taki email już istnieje w bazie!", $email)
                error++;
            }
        }
    });

    if ($name.val().length > 1 && $name.val() != $name.attr('placeholder')) {
        $name.removeClass('invalid_field');
    } else {
        createErrTult('Error! Wrong name!', $name)
        error++;
    }

    if ($pass.val().length > 1 && $pass.val() != $pass.attr('placeholder')) {
        $pass.removeClass('invalid_field');
    } else {
        createErrTult('Error! Wrong password!', $pass)
        error++;
    }

    if (error != 0) return;
    self.find('[type=submit]').attr('disabled', 'disabled');

    self.children().fadeOut(300, function() {
        $(this).remove()
    })
    $('<p class="login__title">sign in <br><span class="login-edition">welcome to A.Movie</span></p><p class="success">You have successfully<br> signed in!</p>').appendTo(self)
        .hide().delay(300).fadeIn();

    // var formInput = self.serialize();
    // $.post(self.attr('action'),formInput, function(data){}); // end post
});

php:

<?php

include ("config.php");

  if($action == "emailcheck"){

      //sprawdzamy czy był już dodany plus
      $test = mysql_num_rows(mysql_query("select * from uzytkownicy where email='$email'"));
            if ($test > 0) {
                $dodano = 1;
                echo json_encode($dodano); 


      }
?>
  • 写回答

2条回答 默认 最新

  • dongxixian7803 2014-12-28 23:23
    关注

    First, you should try adding error callback:

    $.ajax({
        url: '../inc/rejestracja.php?action=emailcheck',
        data: {
            'email': email
        },
        type: 'POST',
        success: function(odp) {
            if (odp == 1) {
                createErrTult("Błąd! taki email już istnieje w bazie!", $email)
                error++;
            }
        },
        error: function(xhr, textStatus, error) // THOSE ROWS
        {
           alert(error);
        }
    });
    

    This may alert you about some occured error.

    Second, you can try to use json instead of plain text:

    Client-side:

    $.ajax({
        url: '../inc/rejestracja.php?action=emailcheck',
        data: {
            'email': email
        },
        dataType: 'json', // THIS ROW
        type: 'POST',
        success: function(odp) {
            if (odp['result'] == 1) {
                createErrTult("Błąd! taki email już istnieje w bazie!", $email)
                error++;
            }
        },
        error: function(xhr, textStatus, error)
        {
           alert(error);
        }
    });
    

    Server-side:

    <?php
    
    include ("config.php");
    if (isset($_GET['action'])){
        $action = $_GET['action'];
        if($action == "emailcheck") {
          if(isset($_GET['email'])) {
            $email = $_GET['email'];
            //sprawdzamy czy był już dodany plus
            $test = mysql_num_rows(mysql_query("select * from uzytkownicy where email='$email'"));
            if ($test > 0) {
                $dodano = 1;
                echo (json_encode(array("result" => $dodano))); // THIS ROW
            }
          }
        }
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器