Skip to Main Content
20 Recipes for Programming PhoneGap
book

20 Recipes for Programming PhoneGap

by Jamie Munro
March 2012
Intermediate to advanced content levelIntermediate to advanced
78 pages
1h 35m
English
O'Reilly Media, Inc.
Content preview from 20 Recipes for Programming PhoneGap

1.13. Creating a New Contact in the Address Book

Problem

You want to allow your application to create and save a new contact or edit an existing contact in the device’s address book.

Solution

The PhoneGap API exposes a function called contacts.create that accepts a structure with the information about the contact. It creates a new Contact object that contains a save function to add the contact to the address book. When the Contact.id already exists, the information is updated instead of creating a new contact. By creating a new form that allows the user to enter information about the contact, when the user submits the data, it will be saved in the address book.

Discussion

The first thing that you need to do is create a new form. Inside of your assets/www directory, create a new file called form.html. This contains a basic form that will be used to collect the contact’s information.

<!DOCTYPE HTML> <html> <head> <title>PhoneGap</title> </head> <body> <div data-role="page" id="form-page"> <div data-role="header" data-position="inline"> <a href="contacts.html" data-icon="delete"> Cancel</a> <h1>Add Contact</h1> <a onClick="return saveContact()" href="#" data-icon="check" data-theme="b">Save</a> </div> <form action="form.html" method="post"> <input type="hidden" name="id" value="0" /> <table> <tr> <td>Display Name</td> <td><input type="text" name="displayName" value="" /></td> </tr> <tr> <td>First Name</td> <td><input type="text" name="firstName" value="" /></td> </tr> <tr> <td>Last Name</td> ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

HTML5 Mobile Websites

HTML5 Mobile Websites

Matthew David
PhoneGap Build

PhoneGap Build

Bintu Harwani

Publisher Resources

ISBN: 9781449334383Supplemental ContentErrata