0

Alright so I have this function

<?php

/**
 * @author Mitchell A. Murphy
 * @copyright 2011
 */
include ('func_lib.php');
connect();
echo (check($_POST['input']) ? 'true' : 'false');
function check($args)
{
    $args = strtolower($args);
    $checkemail = "/^[a-z0-9]+([_\\.-][a-z0-9]+)*@([a-z0-9]+([\.-][a-z0-9]+)*)+\\.[a-z]{2,}$/i";
    if (preg_match($checkemail, $args))
    {
        //logic for email argument
        $sql = "SELECT * FROM `users` WHERE `email`='" . $args . "'";
        $res = mysql_query($sql) or die(mysql_error());
        echo "type=email:";
        if (mysql_num_rows($res) > 0)
        {
            return true;
        } else
        {
            return false;
        }
    } else
    {
        //logic for username argument
        $sql = "SELECT * FROM `users` WHERE `username`='" . $args . "'";
        $res = mysql_query($sql) or die(mysql_error());
        echo "type=username:";
        if (mysql_num_rows($res) > 0)
        {
            return true;
        } else
        {
            return false;
        }
    }

}

?>

The function should be accessed by this jquery script:

$('form.register .submit').click(validateRegister);

function validateRegister() {

    //Variables
    var emailExists = false;
    var userExists = false;
    var $error = "";

    //Executes functions
    email();


    function email() {
        var $error = $('#email .error');
        var input = $('#email input').val();
        var emailRE = /^.*@.+\..{2,5}$/;
        if (input.match(emailRE)) {
            $error
                .html('<div>Proper Email Format: <span>[email protected]</span></div>')
                .animate({
                'left': '-130px',
                'opacity': '0'
            });

            //Checks for Existing Email

            function checkExisting_email() {
                $.ajax({
                    type: 'POST',
                    url: 'includes/checkExist.php',
                    data: input,
                    statusCode: {
                        404: function () {
                            alert('page not found');
                        }
                    },

                    success: function (data) {
                        alert(data);
                    },
                    error: function () {
                        alert("error bro");
                    }
                });

            }
            emailExists = checkExisting_email();

            //If it exists
            if (emailExists) {
                alert("This email already exists!");
            } else if (emailExists == false) {
                alert("Email doesnt exist!");
            }

        } else {
            //Email doesn't match
            $error
                .html('<div>Proper Email Format: <span>[email protected]</span></div>')
                .animate({
                'left': '-150px',
                'opacity': '1'
            });
        }
    }
    return false;
}

But for some reason the script (js) isn't sending any data? if so, how do i reference it. I am the backend developer but the designer who did the javascript left me to fix this. I know the php works because I made a test form to send the data with this html markup:

<form action="includes/checkExist.php" method="post">
<input type="text" name="input" />
<input type="submit" name="submit" />
</form>

And that works...so why is the input from jquery returning as NULL?

2 Answers 2

1

See that checkExisting_email() don't return anything, so emailExists = checkExisting_email(); will not set emailExists. This data will only be provided on the callback function, which today only display the result on an alert().

To make things easier, use jQuery ajax validation field remote. Check the documentation and sample.

Sign up to request clarification or add additional context in comments.

Comments

1

You need to pass in a key/value pair for the "data", not just the value.

As is, your form is going to be posted with a querystring looking like this:

[email protected]

it should be:

data: { input: input },

This will set the querystring to look like:

[email protected]

Also, since you are getting the value out of an element by ID, you dont need to specify the input tag.

 var input = $('#email').val();

1 Comment

Thanks :) I got it a little before but this would have helped me had I not :P

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.