0

I'm trying to build a landing page with php & js validation - i create the html/php & etc, but the .JS script always hangs on the last "else"... and gives the "123" alert... i debugged it for syntax problem for hundred of time with any online debugging site i know - but it's seems to be something else....

$(document).ready(function(){

    $('form.landing-form').submit(function(){

        var name = $.trim($('input[type="text"].field-name').val());
        var email = $.trim($('input[type="text"].field-email').val());
        var phone = $.trim($('input[type="text"].field-phone').val());

        var nameReg = /^[a-zA-Z ]+$/;
        var emailReg = /^([\w-\.]+@([[\w-]+\.)+[\w-]{2,4})?$/;
        var phoneReg = /^[0-9-+]+$/;

        $('input[type="text"]').removeClass('error');

        if(name.length < 2 || !nameReg.test(name)) {
            $('input[type="text"].field-name').addClass('error');
            $('input[type="text"].field-name').val('');
            $('input[type="text"].field-name').attr('placeholder','*valid name is requierd!');
        } else if (email.length < 5 || !emailReg.test(email)) {
            $('input[type="text"].field-email').addClass('error');
            $('input[type="text"].field-email').val('');
            $('input[type="text"].field-email').attr('placeholder','*email is requierd!');
        } else if (phone.length < 9 || !phoneReg.test(phone)) {
            $('input[type="text"].field-phone').addClass('error');
            $('input[type="text"].field-phone').val('');
            $('input[type="text"].field-phone').attr('placeholder','*phone is requierd!');
        } else {
            $.ajax({
                url: "handler/form_handler.php",
                type : "POST",
                dataType: "html",  
                async: "false",  
                data : { name:name, email:email, phone:phone },
                beforeSend : function () {
                    var messege = '<img src="images/ajax-loader.gif" border="0">';
                    messege += '&nbsp;&nbsp;Sending...';
                    $('form.landing-form label').html(messege);
                },
                success: function(response) {
                    if(response == true){
                        alert('321');
                    } else {
                        alert('123');
                    }
                }
            });
        }
        return false;
    });
});

Any ideas?

Matteo B.
  • 3,906
  • 2
  • 29
  • 43
user3387719
  • 55
  • 1
  • 10
  • 2
    Are you returning `true` from the server? If so `if(response == true)` should be `if(response == 'true')` – caspian Jul 11 '14 at 17:05
  • Hi, yeah - i'm trying to INSERT the clients data to SQL by calling the "form_handler.php".... it's got all the querys....i've tried 'true'... didn't work so far... – user3387719 Jul 11 '14 at 17:09
  • 1
    Have you tried to log the response content [console.log(response)] ? – xecgr Jul 11 '14 at 17:10
  • Are you sure your your jquery selector is getting the right data? if you have ids use them. Instead of $('input[type="text"].field-name').val() use $('#field-name').val() – jtorrescr Jul 11 '14 at 17:12
  • XECGR - yes - i've tried - here is the logXHR Loaded (form_handler.php - 200 OK - 1.005500078201294s - 402B) VM383:3 http://localhost/roy/Landing%20Page/handler/form_handler.php?name=roy+barak&email=barakroy%40gmail.com&phone=09-5555553 VM385:3 Time over 1000ms VM386:3 Object {startedDateTime: "2014-07-11T17:12:53.863Z", time: 1005.500078201294, request: Object, response: Object, cache: Object…} VM387:3 – user3387719 Jul 11 '14 at 17:13
  • 2
    try console.log(response); to see what is returned to js – Philipp Werminghausen Jul 11 '14 at 17:16
  • just tried it - gives me a php error Notice: Undefined index: name in C:\xampp\htdocs\roy\Landing Page\handler\form_handler.php on line 3 Notice: Undefined index: email in C:\xampp\htdocs\roy\Landing Page\handler\form_handler.php on line 4 Notice: Undefined index: email in C:\xampp\htdocs\roy\Landing Page\handler\form_handler.php on line 5 Notice: Undefined index: phone in C:\xampp\htdocs\roy\Landing Page\handler\form_handler.php on line 6 Don't know why..... it's all defined – user3387719 Jul 11 '14 at 17:33
  • here are the php varibles $name = filter_var($_POST['name'],FILTER_SANITIZE_STRING); $email = filter_var($_POST['email'],FILTER_VALIDATE_EMAIL); $email = filter_var($email,FILTER_SANITIZE_EMAIL); $phone = filter_var($_POST['phone'],FILTER_SANITIZE_STRING); – user3387719 Jul 11 '14 at 17:34
  • 1
    can you post the php code by editing your question? it becomes apparent that the problem resides there and not in your jquery.. – Félix Adriyel Gagnon-Grenier Jul 11 '14 at 20:45
  • Félix Gagnon-Grenier - i'm strugling to post the code properly but it's not going so well - i'll send you a link by msg. – user3387719 Jul 11 '14 at 21:40
  • here are the files -https://www.dropbox.com/s/d5i6r32ag5tgewt/Landing%20Page.rar – user3387719 Jul 11 '14 at 21:44

1 Answers1

0

I think you're using success the wrong way.

The jquery manual states:

Function( PlainObject data, String textStatus, jqXHR jqXHR )

in other words, there's no boolean returned, but the success message.

Try the following and see what is returned:

success: function(response) {
        alert(response);  /* y no console.log()? */
},
error: function(e) {
        alert(e);
}

From the responses you can figure out what is actually being send, and what error is returned (if any). In case you're not using them, developer tools like firebug will help.

Also, you might want to use === instead of == in comparisons with javascript. (see: this SO-post)

Community
  • 1
  • 1
puredevotion
  • 1,135
  • 1
  • 11
  • 27
  • He uses the *setting* `success`, that is not deprecated. What is deprecated is `jqXHR.success`, which is something like `jQuery.ajax('/').success( ... )` because the return value of jQuery.ajax is `jqXHR`. – Matteo B. Jul 12 '14 at 11:28
  • sorry guys.... nothing seems to work.... here is the code again.... https://www.dropbox.com/s/d5i6r32ag5tgewt/Landing%20Page.rar – user3387719 Jul 12 '14 at 23:44
  • something is going wrong with the post-vars and the way PHP gets them. the `undefined index` errors means that PHP does not find the index `email` in the `$_POST` variable. using firebug or another tool, find out what is exactly being send to the PHP-server and `print_r($_POST)` reveals what the `$_POST` actually receives – puredevotion Jul 13 '14 at 00:00
  • Got it - forgot a letter in a function.. (php file - line 35 - function clean_input insted of function clean_inputs.... Thanks !!! wouldn't done it without you! – user3387719 Jul 13 '14 at 00:58