Cristian
Cristian

Reputation: 113

How to do a function to validate a value using $.post functions?

For example:

<script type="text/javascript" src="http://code.jquery.com/jquery-1.6.2.min.js"></script>    
<script type="text/javascript">
    $(document).ready( function() {
        function is_valid( value )
        {
           // This is what I want to do, but this doesn't works.
           return value == "Iamgood" && $.post( 'validatefield.php', 'fieldvalue=' + value, function (data) { 
                 return data == "true";
           });
        } 

        $('#formtovalidate').bind( 'submit', function() {
            if (is_valid( $('#field').val() ) return true;
            return false;
        });
    });
    </script>
<form id="formtovalidate" action="yes.php">
  <input value="Iamgood" id="field" name="field"/>
  <input type="submit" id="sendform" value="Send" />
</form>

Upvotes: 0

Views: 95

Answers (4)

Joseph at SwiftOtter
Joseph at SwiftOtter

Reputation: 4321

I think that you will have to possibly think about a different way to approach this problem. I don't think this is possible, and the jQuery examples for this (http://api.jquery.com/jQuery.post/) seem to agree. The function is asynchronous - it's a callback, and so it is executed whenever the result is returned (presuming it is). I haven't tried it, but I saw something that you could try:

Try putting this before your $.post call:

$.ajaxSetup({async:false});

Would you be open to another way to do this? I don't know your situation, so this may or may not work:

<script type="text/javascript">
$(document).ready( function() {
    var isValid = false;
    var postValid = false;
    function is_valid( value )
    {
        if (value == "Iamgood") isValid = true;
        $.post( 'validatefield.php', 'fieldvalue=' + value, function (data) { 
            postValid = true;
            $('#formtovalidate').submit();
        });
    } 

    $('#formtovalidate').bind( 'submit', function() {
        if (!isValid || !postValid) {
            is_valid( $('#field').val() );
            return false;
        } else return true;
    });
});
</script>
<form id="formtovalidate" action="yes.php">
  <input value="Iamgood" id="field" name="field"/>
  <input type="submit" id="sendform" value="Send" />
</form>

Upvotes: 0

Rafay
Rafay

Reputation: 31033

$(document).ready( function() {
        function is_valid( value )
        {
           var returnval;
           // use $.ajax instead and use async:false (not a good practice)
           $.post( 'validatefield.php', {fieldvalue:value}, function (data) { 
                 if(data)
                    returnval="true";
                 else
                    returnval = "false";
           });
          if(returnval)
          { 
           switch (returnval)
             {
              case "true":
               return true;
              break;
              case "false":
               return false;
               break;                

              }                
          }
         else
          return false;
        } 

        $('#formtovalidate').bind( 'submit', function(e) {
            e.preventDefault();
            if (is_valid( $('#field').val() ) return true;
            else
            return false;
        });
    });

in your php file do

$value = $_POST['fieldvalue'];
//do processing
if($value)
 echo "true";

else
 echo "false";

P.S

the question was not vary clear to me and the code is untested but still you can try

Upvotes: 0

S P
S P

Reputation: 4643

The first reason why it won't work at all is because you don't prevent the form from being posted to yes.php. I suggest you change the button's type to 'button', call isValid using an 'onclick' and if valid, post to yes.php.

Or perhaps a more elegant way:

  <input type="submit" id="sendform" value="Send" onclick="javascript: return isValid()" />

and remove the input parameter in isValid function and instead determine value of 'Iamgood' in the function itself.

Upvotes: 0

ChristopheCVB
ChristopheCVB

Reputation: 7315

You can't do this that way... Because the function you are passing to post method is a callback and it is executed when data is received.

You have to create a function checkValid(value) which do the post and the post callback calls valueIsOk(value) if data == 'true'

Upvotes: 1

Related Questions