How to display an ajax error through a url?

Asked

Viewed 319 times

2

I have a login/password field, when some user does not fill in one of the inputs, I display a message using ajax like this:

$('#loginform').submit(function(event)
            {
                // Values
                var login = $.trim($('#username').val()),
                    pass = $.trim($('#password').val());

                // Check inputs
                if (login.length === 0)
                {
                    // Display message
                    displayError('Por Favor, insira seu nome de usuario!');
                    return false;
                }
                else if (pass.length === 0)
                {
                    // Remove empty login message if displayed
                    formWrapper.clearMessages('Por Favor, insira sua porta!');

                    // Display message
                    displayError('Por Favor, insira sua senha!');
                    return false;
                }

But, when user misses password or user, it goes back to login url in the following form:

http://example.com?erro=senha_incorreta

As I would, to capture this erro=senha_incorreta and display the same warning style that is displayed when the form is blank? Something like:

if (?erro === senha_incorreta)
                {
                    // Display message
                    displayError('Sua senha está incorreta!');
                    return false;
                }

Can anyone help me with an example of how I would display the message when such a url is requested?

  • In this link I believe I have everything you need: http://www.blogalizado.com.br/ajax-e-json-com-jquery-e-php/

  • All right, I’ll see!

3 answers

1


I’ve put together a very superficial example for your login case. I suppose you already have the form, just change the field names.

LOGIN.PHP

<?
// PDO - select user
// senha inválida
echo false;

// usuário encontrado
echo true;
?>


JS

<script>
$('form').submit( function()
{
    $.ajax({
        url      : 'login.php',
        type     : 'POST',
        data     : { login : $(this).attr('login') , password : $(this).attr('password') }
        dataType : 'json',
        success  : function( request )
        {
            if( request == false )
            {
                alert( 'usuário não encontrado :(' )
            }
            else if( request == true )
            {
                alert( 'usuário encontrado :)' )
            }
        }
    });
});
</script>


A basic example of a query returning only TRUE/FALSE.

0

Maybe you’re interested in the Parseuri library.

Example of use:

<!DOCTYPE html>
<html lang="en">
<head>
    <title>parseUri example</title>
    <script src="/application/html/js/franzenzenhofer/parseUri/parseUri.js"></script>
</head>
<body>
    <div id="results"></div>
    <script>
        var uri = "http://www.google.com/?a=foo&b=bar&c=baz";
        var r= document.getElementById('results');
        r.innerHTML = parseUri(uri).queryKey.b
    </script>
</body>
</html>

Page: http://www.javascriptoo.com/parseuri

Download: https://github.com/franzenzenhofer/parseUri/zipball/master

0

In fact you are displaying an error message using jquery and not ajax, as there is no ajax request to the server requesting such a message.

Then follows below an example function to take the parameters of the opened URL, using jquery:

$.urlParam = function(name){
    var results = new RegExp('[\?&]' + name + '=([^&#]*)').exec(window.location.href);
    if (results==null){
       return null;
    }
    else{
       return results[1] || 0;
    }
}

if ($.urlParam('erro') == 'senha_incorreta')
                {
                    // Display message
                    displayError('Sua senha está incorreta!');

                    return false;
                }

But I would do in php something like this:

<?php

if ($_GET['erro'] == 'senha_incorreta') echo '<script>
$(document).ready(function() {
    displayError(\'Sua senha está incorreta!\');
});
</script>';

?>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.