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 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办