473,671 Members | 2,373 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 1615
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
10911
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 empty strings in the resulting string array.
1
3817
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 Site Header table to Sample Details table relation ship is one-to-many, with two key fields SiteID and SiteVisitDate The Form/SubForm is linked by SiteID, SiteVisitDate The form adds one record to the Site Header Table.
2
1928
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 count how many records in that filed is empty and the records which have written any solution in that filed. I would be grateful for any solution.
1
1601
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
2621
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> <head> <script language="JavaScript"> function isEmpty(inputStr,fieldname){ if (inputStr==null || inputStr==""){
4
7815
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 args.Text.trim.length() = 0 then
5
2502
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 kind of things (ie age without anything): Surname : Sawers First name : Tom Age: Nationality : American Thanks a million!
15
9890
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
9632
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
3237
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, item, quantity, price, shippedby, shipdate, shipcost), etc.. I wish to "normalize" this without loosing data. MainTable.ID -> NamesTable (NameID, NameType, LName, FName, etc....) -> AddressTable (AddressID, Type, Line1, Line2,...
0
8478
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8397
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8821
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8599
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8670
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7439
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6230
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4225
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4409
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.