By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,747 Members | 1,672 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,747 IT Pros & Developers. It's quick & easy.

validate empty fields

P: 5
I have a form that populates data from database upon clicking on a fetch button and fills the other fields.
It works fine fine with the exception that it should display an error message if it retrieves nothing. Can someone help me write the failure function.i.e if the form returns nothing when u click on the fetch button,it should display some kind of error message.

Here is my code for form:
Expand|Select|Wrap|Line Numbers
  1. <input type="text" name="username" id="username"> 
  2. <div id="formResponse"></div>
  3. <button id="fetchFields">fetch</button>
  4. <label for="posts">Posts: </label>
  5. <input type="text" size="20" name="posts" id="posts">
  6. <label for="joindate">Joindate: </label>
  7. <input type="text" size="20" name="joindate" id="joindate">
  8.  
  9.  
  10.  
  11. <p><input type="submit" value="Submit" name="submitBtn"></p>
  12.  
  13. </fieldset>
  14. </form>
  15.  
  16. <script type="text/javascript">
  17. $(document).ready(function() {
  18.     function myrequest(e) {
  19.         var name = $('#username').val();
  20.         $.ajax({
  21.              method: "GET",
  22.             url: "autofill.php",
  23.             dataType: 'json',
  24.             cache: false,
  25.             data: {
  26.                 username: name
  27.             },
  28.             success: function( responseObject ) {
  29.                 alert('success');
  30.                 $('#posts').val( responseObject.posts );
  31.                 $('#joindate').val(responseObject.joindate);
  32.                 /*
  33.                 once you've gotten your ajax to work, then go through and replace these dummy vals with responseObject.whatever
  34.                 */
  35.             },
  36.             failure: function() 
  37.             {
  38.                 alert('fail');
  39.             }
  40.         });
  41.     }
  42.  
  43.     $('#fetchFields').click(function(e) {
  44.         e.preventDefault();
  45.         myrequest();
  46.     });
  47. });
here is my autofill.php
Expand|Select|Wrap|Line Numbers
  1. <?
  2. $name = stripslashes($_GET['username']);
  3.  
  4.  
  5.    $return = mysql_query("SELECT posts,joindate FROM user WHERE username = '$name' LIMIT 1");
  6.    if(mysql_num_rows($return) > 0)
  7.    {
  8.       $rows = mysql_fetch_assoc($return);
  9.       $formattedData = json_encode($rows);
  10.       echo    $formattedData;  
  11.    }
  12.  
  13. ?>
  14.  
[/quote]
Jan 30 '12 #1
Share this Question
Share on Google+
1 Reply


Dormilich
Expert Mod 5K+
P: 8,639
rename failure to error.

but that won’t help you since no content is not a failure (it is, so-to-speak, only an empty string). to properly deal with that issue you could use HTTP headers. there is for instance "HTTP\1.1 204 No Content" which you can intercept with the statusCode property.
Jan 31 '12 #2

Post your reply

Sign in to post your reply or Sign up for a free account.