How to display an ajax error through a url?

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 the user misses the password or user, it returns to the login url in the following form:

  

link

How would I do, 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 someone help me with an example of how I would display the message when such a url was requested?

    
asked by anonymous 06.08.2014 / 05:20

3 answers

1

I set 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 .

    
06.08.2014 / 05:53
0

You may be interested in the ParseURI library.

Example usage:

<!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: link

Download: link

    
06.08.2014 / 13:02
0

You are actually displaying an error message using jquery and not ajax, since there is no ajax request to the server requesting such a message.

Then here is an example function to get the parameters of the open 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 something like this in php:

<?php

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

?>
    
06.08.2014 / 15:05