phpjqueryjqueryform

PHP Function can't read jquery POST data?


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>Hello@Yoursite.com</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>Hello@Yoursite.com</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?


Solution

  • 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.