Advanced Search

Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: Displaying php validation results through javascript

  1. #1
    Join Date
    Jul 2012
    Posts
    197
    Thanks
    55
    Thanked 3 Times in 3 Posts

    Default Displaying php validation results through javascript

    My Question is little complicated,
    I have the following code in Jquery which submits a form and display the validation result from the destination page.

    Code:
    submitHandler : function(form) {
    if($('#login').submit(function(){return false;}))
    {
    $.ajax
    ({
    type: 'POST',
    url: $('#login').attr('action'),
    data: $('#login').serialize(),
    success: function(data) 
    {
    $('#results').html(data);
    }
    });
    }
    return false;
    },
    The destination page is in php. The content displayed in #results is php processed validation results

    My Issue is - Iam getting some alpha numeric strings with the validation result as follows

    {"output":"","status":-1,"error_messages":{"error":["please enter your name."],"success":[]}}

    This is because i have a php file called message.php this validation results should be come through message.php. it is exactly processing and stripping the unwanted characters and displays the error message properly.

    Is there any way to do this in the above JavaScript(Jquery)that the the validation results should come through message.php instead of direct displaying to avoid putting unwanted characters in validation results (only validation result should be displayed)..

    Or any other suggestions you have ?

    Looking forward for a favorable action..
    I don't think i will get a accurate support about this issue from this forum... but may be ...

    Regards TOM

  2. #2
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,623
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    Quote Originally Posted by letom View Post
    Code:
    {"output":"","status":-1,"error_messages":{"error":["please enter your name."],"success":[]}}
    This is what you're getting in PHP, or this is what you're getting as your response from PHP?

    This is a JSON string. If this is on the PHP side, you can turn it into an associative array like so:
    PHP Code:
    $myArray json_decode'{"output":"","status":-1,"error_messages":{"error":["please enter your name."],"success":[]}}',true ); 
    similarly, you can use JSON.parse (or jQuery's $.parseJSON for better cross-browser compatibility) to turn the string into a JS object on the client side.
    We Only Torture the Folks We Don't Like (You're Probably Gonna Be Okay)
    It's a Party in the CIA

  3. The Following User Says Thank You to traq For This Useful Post:

    letom (05-06-2013)

  4. #3
    Join Date
    Jul 2012
    Posts
    197
    Thanks
    55
    Thanked 3 Times in 3 Posts

    Default

    This is what you're getting in PHP, or this is what you're getting as your response from PHP?
    getting as response from php..
    Adrain
    Thanks.. i had spend my one day time by working over JSON, not getting the correct result.. but i will try again after checking your post..
    I conquered the matter by using slice() by stripping out characters up to [" from left to right and right to left, then it will display only the error message "please enter your name" , Is it professional and ethical ?

  5. #4
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,623
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    It's not a matter of ethics at all - your "solution" may work in this one case, but it is likely to break if the structure of the response ever changes. It's better to treat the response like what it actually is, JSON.

    For example, since you're already using jQuery:
    Code:
    /* here's the json string you get in your ajax response */
    var ajax_response_json = '{"output":"","status":-1,"error_messages":{"error":["please enter your name."],"success":[]}}';
    
    /* parse it; you'll get a regular javascript object in return */
    var ajax_response_object = $.parseJSON( ajax_response_json );
    
    /* this way, you don't ruin the response, and you can access its properties normally */
    
    /* for example, this alerts "please enter your name." */
    alert( ajax_response_object.error_messages.error[0] );
    We Only Torture the Folks We Don't Like (You're Probably Gonna Be Okay)
    It's a Party in the CIA

  6. The Following User Says Thank You to traq For This Useful Post:

    letom (05-06-2013)

  7. #5
    Join Date
    Jul 2012
    Posts
    197
    Thanks
    55
    Thanked 3 Times in 3 Posts

    Default

    Thanks..Your coding is working fine, but in my case i have to display more that one..

    But it would be nice if u have a look at this, it will not change if we slice it '{"output":"","status":-1,"error_messages":{"error":["please enter your name."],"success":[]}}'; only error message "please enter your name" is changing., remaining characters are at same position with same length...

    approximately
    slice(50, -19);

    Ethics i mean .. There is a ethics in every thing..In programming also.. If it is ethical all working environment will feasible to afford that, should be fine.
    Last edited by letom; 05-06-2013 at 04:12 AM.

  8. #6
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,623
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    Quote Originally Posted by letom View Post
    it will not change if we slice it '{"output":"","status":-1,"error_messages":{"error":["please enter your name."],"success":[]}}'; only error message "please enter your name" is changing., remaining characters are at same position with same length...
    If you need to display the message several times, you can assign it its own var:
    Code:
    var ajax_response_json = '{"output":"","status":-1,"error_messages":{"error":["please enter your name."],"success":[]}}';
    var ajax_response_object = $.parseJSON( ajax_response_json );
    
    /* instead of alert()ing the value, assign a var */
    var error_message = ajax_response_object.error_messages.error[0];
    
    /* use (as often) as desired */
    alert( error_message );
    alert( error_message );
    alert( error_message );
    But if you're happy with slicing it instead, that's fine, of course.


    Quote Originally Posted by letom View Post
    Ethics i mean .. There is a ethics in every thing..In programming also.. If it is ethical all working environment will feasible to afford that, should be fine.
    Of course - I understand that there are ethics in programming. What I was saying is that, in this case, choosing one method over the other does not have any "ethical" impact on anything.
    We Only Torture the Folks We Don't Like (You're Probably Gonna Be Okay)
    It's a Party in the CIA

  9. #7
    Join Date
    Jul 2012
    Posts
    197
    Thanks
    55
    Thanked 3 Times in 3 Posts

    Default

    Noted the contents

  10. #8
    Join Date
    Jul 2012
    Posts
    197
    Thanks
    55
    Thanked 3 Times in 3 Posts

    Default

    Code:
    var error_message = ajax_response_object.error_messages.error[0];
    Adrian
    can u explain the red colored part... when iam using $.parseJSON iam getting a null result.
    You will know the truth, and the truth will set you free (John 8:32)(The truth is about the only begotten son of GOD.....When you feel you are alone for facing your problems, God will never leave you by your own You are important for God

  11. #9
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,623
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    before jQuery 1.9, $.parseJSON returns null if the JSON string is malformed (after 1.9, it throws an error).

    Can you show the complete code you are using? and is the actual string you're using the same as the example you posted?
    We Only Torture the Folks We Don't Like (You're Probably Gonna Be Okay)
    It's a Party in the CIA

  12. #10
    Join Date
    Jul 2012
    Posts
    197
    Thanks
    55
    Thanked 3 Times in 3 Posts

    Default

    Thanks..

    following is the ajax coding to get the result

    Code:
    submitHandler : function(form) {
    if($('#login').submit(function(){return false;}))
    {
    $.ajax
    ({
    type: 'POST',
    url: $('#login').attr('action'),
    data: $('#login').serialize(),
    success: function(data) 
    {
    $('#results').html(data);
    }
    });
    }
    return false;
    },
    following is the php code, while this validation messages passes through this php code, it produce the correct error message "please enter your name"from the following
    {"output":"","status":-1,"error_messages":{"error":["please enter your name."],"success":[]}}

    But when we submitting it through ajax it is not going to the below coding , that is the problem iam getting some more results with the error message

    Code:
    if (isset($vars['object']) && is_array($vars['object']) && sizeof($vars['object']) > 0) {
    	foreach ($vars['object'] as $type => $list ) {
    		foreach ($list as $message) {
    			echo '<div id="error-msg">'.$message.'</div>';
    		}
    	}
    }
    Code:
    <?php echo $messages; ?>
    //displays the error message
    Last edited by letom; 05-19-2013 at 05:53 AM. Reason: adding more information
    You will know the truth, and the truth will set you free (John 8:32)(The truth is about the only begotten son of GOD.....When you feel you are alone for facing your problems, God will never leave you by your own You are important for God

Similar Threads

  1. Javascript results in text boxes
    By Johny Appleseed in forum JavaScript
    Replies: 1
    Last Post: 08-30-2012, 09:04 AM
  2. How to get my javascript results to display
    By forum_amnesiac in forum HTML
    Replies: 0
    Last Post: 05-23-2009, 09:17 AM
  3. Mixed results with javascript.
    By max_power in forum JavaScript
    Replies: 1
    Last Post: 04-24-2009, 11:58 AM
  4. Save javascript results
    By ptera in forum Looking for such a script or service
    Replies: 0
    Last Post: 03-26-2009, 04:35 PM
  5. Displaying serach results in a different way
    By stefaneh in forum Looking for such a script or service
    Replies: 1
    Last Post: 07-30-2007, 10:01 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •