473,511 Members | 12,087 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

validate empty fields

5 New Member
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
1 1609
Dormilich
8,658 Recognized Expert Moderator Expert
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

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

Similar topics

19
10894
by: David Logan | last post by:
We need an additional function in the String class. We need the ability to suppress empty fields, so that we can more effectively parse. Right now, multiple whitespace characters create multiple...
1
3807
by: Shaggers | last post by:
I need to hide/not display a blank row on a subform. The Main form is based on a table, contains Site Header information The Subform is based on another table containing Sample Details records ...
2
1918
by: Viktor | last post by:
I have a problem with counting empty fields in table. For example: I have the field in which i coulld write several solutions. "done", "not done", "send", "not send" etc. In report i want to...
1
1598
by: naboumerhi | last post by:
Hi, I have a a custom report trying to hide empty fields and shift the next field up. It still shows and empty space. Example: company1 company2 company3
13
2608
by: elsheh | last post by:
Hi.. guys I’m trying to use JavaScript to validate my HTML for empty fields. My code is as below. The problem is that I can escape validation and submit empty fields. Any clue? <html> ...
4
7805
by: Eric Layman | last post by:
Hi everyone, I noticed that customevalidator won't work with empty fields. But I do not want to put in another requiredfieldvalidator Any workarounds? I've tried if...
5
2492
by: Phaelle | last post by:
In my database, some fields are empty for some recordings. And I wouldl like that the empty fields don´t appear on the answer page of my search engine or, in other worlds, that I don´t have that...
15
9876
by: JFKJr | last post by:
Hello everyone, I have imported an excel file into an Access table, but the table contains some empty fields, does anyone know how to delete those empty fields using Access VBA? Thanks in Advance.
5
9623
code green
by: code green | last post by:
How can empty fields be detected. SELECT * FROM products WHERE description IS EMPTYThe field may be NULL or just empty and I suppose even space characters.
3
3229
by: FKlusmann | last post by:
I have inherited a big, messy table with duplicated data and empty fields consisting of Names (business, client, supplier, etc.), Addresses ( physical, mailing, shipping, billing), Orders ( Date,...
0
7245
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
1
7085
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7512
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5671
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5069
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4741
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3227
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1577
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
785
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.