Welcome Guest, Not a member yet? Register   Sign In
Help escaping at sign (@) from ajax to php
#1

[eluser]digitaleye[/eluser]
Hi
I seem to be having a problem with some code. I have a check for an email address, which checks validity whilst typing, plus checks a database for an already registered email. The code i have is as follows :

Javascript in the view
...
Code:
$('#email').keyup(function() {
        var email = $("#email").val();
        $.ajax({
            url: 'email_callback/' + $(this).val(),
            type: 'post',
            data: "email="+escape(email),
            success: function(result) {
                $('#email_check').attr('innerHTML', result);
            }
        });
    });
...

Controller info
...
Code:
function email_callback()
    {
        $mail = $this->input->post('email');
        print $mail; // this echos email for testing
        
        // check email not already registered
        if($this->MRegister->checkExistingEmail($mail) == TRUE) {
            print "<img src='".base_url()."images/cancel_32.png' alt='Email in use' style='border:none; padding-left: 5px;' /> <span style='color:red; padding-left: 5px;'>Email already registered.</span>";
        } else {
            if (!eregi("^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})$", $mail)){
            print "<img src='".base_url()."images/cancel_32.png' alt='Invalid Email Address' style='border:none; padding-left: 5px;' /> <span style='color:red; padding-left: 5px;'>Invalid Email Address.</span>";
            } else {
                print "<img src='".base_url()."images/accepted_32.png' alt='Username Available' style='border:none; padding-left: 5px;' />";
            }
        }
...

The problem i'm having seems to be with the at sign (@), as when i echo the output, nothing shows.
I have tried escaping the email input, then decoding the php, but everything I try doesn't output the correct info.

Any help appreciated.
Thanks
#2

[eluser]Scal[/eluser]
Hi;
are you meaning the issue is in the reg exp evaluation? If so, why not use the valid_email() CI Email Helper function?

If not and talking about the $mail variable not being set like if it was not sent with the ajax call, did you try changing the type to 'POST'?
#3

[eluser]digitaleye[/eluser]
Hi
The content of the email field does seem to be being sent via post; I am checking this by echoing the input. However, as soon as i type the @ nothing is sent from this character onwards.

For example
I enter [email protected] into the email field
The characters echo'd are test then nothing else, as though the @ is stopping the function.

Thanks
#4

[eluser]rogierb[/eluser]
Can you post a print_r on $_POST?
#5

[eluser]digitaleye[/eluser]
Hi

When I enter [email protected] in the email field I get

Code:
Array ( [email] => test )

Thanks
#6

[eluser]rogierb[/eluser]
can you change the ajax call
Code:
data: "email="+escape(email),

to
Code:
data: {email:$("#email").val(),email2:'[email protected]', email3:escape($("#email").val())},
async: false,

and do a print_r on $_POST again?
#7

[eluser]digitaleye[/eluser]
Hi

Output of $_POST

Code:
Array ( [email] => test [email2] => [email protected] [email3] => test )

Thanks
#8

[eluser]Chad Fulton[/eluser]
Your problem is that in your AJAX request, you're setting the URL to be 'email_callback/'+$(this).val().

Now recall that in the context of the $('#email').keyup event, $(this) is bound to the #email element, which is your e-mail address form input.

So, you're pointing the URL of the AJAX request to, for example, 'email_callback/[email protected]'. The problem here is that CodeIgniter doesn't allow the @ symbol in URI's, so whenever you get to the @ symbol, CodeIgniter prevents the script from running your email_callback() function.

Fortunately for you, you don't seem to be using that anyway, so you can just remove it, like so:
Code:
$('#email').keyup(function() {
        var email = $("#email").val();
        $.ajax({
            url: 'email_callback/', // I removed the + $(this).val()
            type: 'post',
            data: "email="+escape(email),
            success: function(result) {
                $('#email_check').attr('innerHTML', result);
            }
        });
    });

Good luck!
#9

[eluser]rogierb[/eluser]
There are a couple of possibilities as far as I can see.

1: your $_POST gets scrubbed. Do you xss cleaning on by default(or something else?
2: $("#email").val(); does not contain an email address, try alert($("#email").val()) before the ajax call
3: $("#email").val() does not get posted properly, try something like email:"'"+$("#email").val()+"'" to see if that makes any difference
#10

[eluser]digitaleye[/eluser]
[quote author="Chad Fulton" date="1255100339"]

So, you're pointing the URL of the AJAX request to, for example, 'email_callback/[email protected]'. The problem here is that CodeIgniter doesn't allow the @ symbol in URI's, so whenever you get to the @ symbol, CodeIgniter prevents the script from running your email_callback() function.

Fortunately for you, you don't seem to be using that anyway, so you can just remove it, like so:
Code:
$('#email').keyup(function() {
        var email = $("#email").val();
        $.ajax({
            url: 'email_callback/', // I removed the + $(this).val()
            type: 'post',
            data: "email="+escape(email),
            success: function(result) {
                $('#email_check').attr('innerHTML', result);
            }
        });
    });

Good luck![/quote]

This has worked great.

Thanks everyone for your help, very much appreciated.




Theme © iAndrew 2016 - Forum software by © MyBB