在Ajax中将mysql echo返回为错误 [英] return mysql echo as error in ajax

查看:110
本文介绍了在Ajax中将mysql echo返回为错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ajax创建登录页面.当用户名和密码错误时,它应该显示错误,当密码正确时,它应该重定向到另一个页面.但是在这两种情况下,都会调用成功函数.

i am creating login page with ajax.when user name and pass is wrong it should display error and when pass is right it should redirect to another page.but here in both case success function is called..

<script>
    $(document).ready(function()
    {


    $("#simple-post").click(function()
    {
        $("#ajaxform").submit(function(e)
        {
            $("#simple-msg").html("<img src='loading.gif'/>");
            var postData = $(this).serializeArray();
            var formURL = $(this).attr("action");

            $.ajax(
            {
                url : formURL,
                type: "POST",
                data : postData,
                success:function(data, textStatus, jqXHR) 
                {
                alert(data.status);
                $("#simple-msg").html('<pre><code class="prettyprint">'+data+'</code></pre>');


                },
                error: function(jqXHR, textStatus, errorThrown) 
                {
                    $("#simple-msg").html('<pre><code class="prettyprint">AJAX Request Failed<br/> textStatus='+textStatus+', errorThrown='+errorThrown+'</code></pre>');
                }
            });
            e.preventDefault(); //STOP default action
        });

        $("#ajaxform").submit(); //SUBMIT FORM
    });

    });
    </script>

这是php

<?php
ob_start();

include 'CUserDB.php';

session_start(); 
include 'config.php';
$myusername=$_POST['txtusername']; 
$mypassword=$_POST['txtpassword']; 
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword=mysql_real_escape_string($mypassword);      

$qry = "SELECT UserName,Type_user FROM login WHERE UserName = '".$myusername."' AND password = '".$mypassword."' ";

$result = mysql_query($qry) or die ("Query failed");

$UserData = mysql_fetch_array($result);

    if($UserData['UserName'] != '')

        {
            session_start(); 

             $_SESSION['UserId'] = $myusername;

        $typ = $UserData['Type_user'];



    if   ( $typ == "Dealer")
    { 
 header('location:Dealer/EditLoginDetails.php');  


    }
    else if     ($typ == "Individual")
    {
         header('location:/Dealer/EditLoginDetails.php');  

    }
    else 
    {
        header('location:/Builder/managep.php'); 
    }




        }
   else 

    { 
    echo " wrong username or password";


    }

?>

如果不成功,我想显示错误的用户名通行证..如何执行此操作,请问我是ajax的新手

i want to display wrong username pass if not success..how to do this please i am new in ajax

推荐答案

请清楚了解jquery.Ajax的成功回调.该文档说:如果请求成功,将调用该函数.

Please have a clear understanding of success callback of jquery.Ajax. The documentation says: A function to be called if the request succeeds.

在您的情况下,如果输入的用户名和密码错误,则请求成功.

In your case, request is successful if the username and password entered are wrong.

要使其正常工作,请将此部分的php代码更改为:

To Make it work properly, change your php code of this part to:

$redirect = '';
if($UserData['UserName'] != '')
{
    session_start();
    $error = 0;
    $message = 'Valid';
    $_SESSION['UserId'] = $myusername;

    $typ = $UserData['Type_user'];

    if($typ == "Dealer")
    {
        $redirect = 'Dealer/EditLoginDetails.php';
    }
    else if($typ == "Individual")
    {
        $redirect = '/Dealer/EditLoginDetails.php';
    }
    else
    {
        $redirect = '/Builder/managep.php';
    }
}
else
{
    $error = 1;
    $message = 'Invalid username or password';
}

echo json_encode(array('error' => $error, 'message' => $message, 'redirect' => $redirect));


然后将jquery代码添加到


And jquery code to,

$.ajax({
    url : formURL,
    type: "POST",
    data : postData,
    dataType:'json',
    success:function(data, textStatus, jqXHR) {
        if(data.error == 1) {
            $("#simple-msg").html('<pre><code class="prettyprint">'+data.message+'</code>< /pre>');
        } else {
            window.location = data.redirect;
        }
    }
});

这篇关于在Ajax中将mysql echo返回为错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆