0

I am trying to make jquery validation on a form, everything works about checking the inputs, but when everything is right, it doesnt "go further" and return anything.

function validateFields() {

    var fname = $("input#fname").val();
    if (fname == "") {
        $( "#WrapSu" ).css({"border-radius":"0px 0px 3px 3px"});
        $( "#Fejl" ).html("<p class=\"c\">First name missing!</p>");
        $( "#Fejl" ).slideDown( "slow" );
        return false;
    }

    var lname = $("input#lname").val();
    if (lname == "") {
        $( "#WrapSu" ).css({"border-radius":"0px 0px 3px 3px"});
        $( "#Fejl" ).html("<p class=\"c\">Last name missing!</p>");
        $( "#Fejl" ).slideDown( "slow" );
        return false;
    }

    var femail = $("input#femail").val();
    if (femail == "") {
        $( "#WrapSu" ).css({"border-radius":"0px 0px 3px 3px"});
        $( "#Fejl" ).html("<p class=\"c\">Email missing!</p>");
        $( "#Fejl" ).slideDown( "slow" );
        return false;
    }

    var lemail = $("input#lemail").val();
    if (lemail == "") {
        $( "#WrapSu" ).css({"border-radius":"0px 0px 3px 3px"});

        $( "#Fejl" ).html("<p class=\"c\">Email missing!</p>");
        $( "#Fejl" ).slideDown( "slow" );
        return false;
    }

    var cemail = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
    if (femail !== "") {
        $( "#WrapSu" ).css({"border-radius":"0px 0px 3px 3px"});
        if(!cemail.test(femail)){
            $( "#Fejl" ).html("<p class=\"c\">Your email is invalid.</p>");
            $( "#Fejl" ).slideDown( "slow" );
            return false;
        }
    }

    var fpassword = $("input#fpassword").val();
    if (fpassword == "") {
        $( "#WrapSu" ).css({"border-radius":"0px 0px 3px 3px"});
        $( "#Fejl" ).html("<p class=\"c\">Password missing!</p>");
        $( "#Fejl" ).slideDown( "slow" );
        return false;
    }

    var lpassword = $("input#lpassword").val();
    if (lpassword == "") {
        $( "#WrapSu" ).css({"border-radius":"0px 0px 3px 3px"});
        $( "#Fejl" ).html("<p class=\"c\">Confirm password!</p>");
        $( "#Fejl" ).slideDown( "slow" );
        return false;
    }

     var datastring = 'fname='+ fname +'&lname='+ lname +'&email='+ femail +'&password='+ fpassword;

    $.ajax({
        type: "POST",
        url: "opret.php",
        data: dataString,
        success: function() {
            if(responseText == 1) {
                $( "#Fejl" ).html("<p class=\"c\">virker</p>");
            } else { // else blank response
                $( "#Fejl" ).html("<p class=\"c\">virker2</p>");
            }
        }
    });
    return false;
};
    $('form#opret').on('submit',function(e) {
    e.preventDefault();
    var success=validateFields();
});

And i do not know what needs to be changed, i have tried much, and my experience stops, i have no idea what it is..

i would love for anyone to kindly explain me whats the error, so that i will learn it :-) php code:

1<?php

include("config.php");

$fname=$_POST['fname'];
$lname=$_POST['lname'];
$email=$_POST['femail'];
$password=$_POST['fpassword'];

$memails = mysql_result(mysql_query("SELECT COUNT(*) FROM Users WHERE email = '$email'"),0); //check for email exist

if($memails < 1){
    echo "1";
} else {
    echo "2";
}

?>

user3259244
  • 15
  • 1
  • 7

1 Answers1

0

You're trying to send your arguments as some sort of data. You need to either send a query string like so...

$.ajax({
    type: "POST",
    url: 'opret.php' + '?' + dataString,
    success: function(responseText) {
        if(responseText == "1") {
            $( "#Fejl" ).html("<p class=\"c\">virker</p>");
        } else { // else blank response
            $( "#Fejl" ).html("<p class=\"c\">virker2</p>");
        }
    }
});

... and let your PHP's and iterate through your $_POST to get the variables, or send your data as JSON and then decode it to an object in your PHP script like so...

var data = {
    'fname': fname,
    'lname': lname, 
    'email': femail,
    'password': fpassword
};

$.ajax({
    type: "POST",
    dataType: 'json',
    data: data,       
    url: "opret.php",
    success: function(responseText) {
        if(responseText == "1") {
            $( "#Fejl" ).html("<p class=\"c\">virker</p>");
        } else { // else blank response
            $( "#Fejl" ).html("<p class=\"c\">virker2</p>");
        }
    }
});

EDIT: Fixed appending of query string.

EDIT 2: Updated for handling of actual PHP returns.

gfish3000
  • 1,557
  • 1
  • 11
  • 22
  • Yes aint that what i did? I just had a datastring? :) But well, i tried that code you posted, didnt seem to give me any of those results back, that would say "virker" or "virker2" – user3259244 Mar 19 '14 at 22:10
  • @user3259244, no, your data string was formatted as a query string and you were trying to pass it as some sort of data to be serialized. You need to append data to your query string to your url or send it as a JSON while explicitly specifying what you're sending. Also, whoops, I forgot the `?` denoting the query string and will correct my response. Give it another try and see what you get back. – gfish3000 Mar 19 '14 at 22:40
  • I tried. It doesnt change the #Fejl text, so it must mean it doesnt get a responseText or something? In my opret.php i have just put a simple 1 inside it. – user3259244 Mar 19 '14 at 22:51
  • Ok, wow, silly me. I didn't notice that your `success()` function isn't letting you work with the data. Let me know if the new edit works and if not, post your PHP script. You might not be returning the `responseText` using `json_encode`. – gfish3000 Mar 19 '14 at 23:03
  • Yeah it seems to work, what does that function responsetext do exacly? – user3259244 Mar 19 '14 at 23:11
  • Basically, the AJAX response sends back a JSON string. This function call treats this returning string as an argument to the function and [lets you bring it into your Javascript as an object you can manipulate](http://stackoverflow.com/questions/14754619/jquery-ajax-success-callback-function-definition). So are you all set now? – gfish3000 Mar 19 '14 at 23:17
  • oh okay, thanks :-) Im now trying to slip the information into opret.php, i have ` if($_POST){ $fname=$_POST['fname'];` But it doesnt seem to work, am i doing it right? – user3259244 Mar 20 '14 at 01:05
  • You have to post your PHP script. One line doesn't tell me much at all. – gfish3000 Mar 20 '14 at 01:31
  • its at the top, edited it :-) the 1 before php was just to get a reaction, but nothing happend either – user3259244 Mar 20 '14 at 01:36
  • Umm... where are you populating your `$email`? Does your query return anything? Also, in this case, I'll need to edit the Javascript as well. Check it in just a minute here... – gfish3000 Mar 20 '14 at 01:40
  • See you changed it, also tried it. Wont work either. Could we maybe chat somewhere faster, if you want to? I also appreciate ur help alot :-) – user3259244 Mar 20 '14 at 01:48
  • You don't have enough points to chat on here, so where you'd want to chat is up to you in this case. – gfish3000 Mar 20 '14 at 01:54