user125264
user125264

Reputation: 1827

Parsley for validation and submit data via ajax

Im testing out the Parsley framework and i must say I really do like how simple it is to implement.

My only issue, is that if i try to attach an ajax submit if the form is validated correctly, it just doesnt seem to catch on.

below is my code.

<form data-validate="parsley" action="reply.html" method="post" id="main-form">
   <input type="text" name="email" data-type="email" data-required="true" /><br />
  <input type="submit" name="submit" />
</form>


<script>
$("#main-form").submit(function(event){
event.preventDefault();
    $.ajax({
           type: "post",
           url: $(this).attr('action'),
           data: $("#main-form").serialize(), // serializes the form's elements.
            dataType : 'json',
           success: function(data)
           {                
            if(data[0].status == 'success')
                {
                alert('its working ok');
                }
           }
         });
});
/* end simple ajax form post */
</script>

all it seems to do, is allow me to validate the form, then post it normally, however im looking for an easy way to simply validate the form and if all is good, submit the data via ajax.

I cant seem to find an easy way of doing this via their help docs... any advice greatly appreciated

Upvotes: 2

Views: 6984

Answers (2)

Asier
Asier

Reputation: 11

Resolved. If exists answers with 200, so its invalid, if not exists, works with 404, so:

In the external file, you should put if not exists header("HTTP/1.0 404 Not Found");

Upvotes: 0

Sajith
Sajith

Reputation: 2852

You can validate the email using parsely's ajax validation. use data-remote="http://yoururl.com" in your input field, which is to be validated. for example

<input type="text" name="email" data-type="email" data-required="true" data-remote="http://localhost/verify_email.php" />

Note that I've added data-remote="http://localhost/verify_email.php". This will fire an ajax request to the given url.

verify_email.php

// Do some code for verifying email id
if($emailIdExist){ 
    echo json_encode(array("error"=>"Sorry, Email exists. Please try with another one"));
}
else{
    echo true;
}

Upvotes: 5

Related Questions