2

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

1

2 Answers 2

5

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;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the answer. Do you know how to use parsley's custom red messages when there's an error on the input?
By default, Parsley is using its styles. If you want to write custom styles, override parsley's styles in your style sheet with the help of !import
0

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");

1 Comment

This still works? I am returning a 200 if is valid and 404 if not, but neither Parsley keep saying that is invalid

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.