T J
T J

Reputation: 749

Submit form field values to a Javascript function

I am using Buddy.com's API to build a mobile app with a web Interface for Administrators and User Creation. I am still in the initial stages of this project.

The first page I am trying to build is the User Registration page for the website. This page will have a form asking the users for a username, password, email and some other fields.

I am using the javascript form the page http://buddy.com/developers/#UserAccount_Profile_Create

The function is: function createUserWithName(); And it has some inputs. The function looks like this:

function fixedEncodeURIComponent (str) {  
    return encodeURIComponent(str).replace(/[!'()*]/g, escape);  
}
function createUserWithName(aName, anEmail) 
{
    var finalURLStr = "https://webservice.buddyplatform.com/Service/v1/BuddyService.ashx";

    finalURLStr += "?UserAccount_Profile_Create";    // API Call
    finalURLStr += "&BuddyApplicationName=" + fixedEncodeURIComponent("<#Buddy App Name#>");
    finalURLStr += "&BuddyApplicationPassword=" + fixedEncodeURIComponent("<#Buddy App Password#>");
    finalURLStr += "&NewUserName=" + fixedEncodeURIComponent(aName);                    // The user name
    finalURLStr += "&UserSuppliedPassword=" + fixedEncodeURIComponent("<#password#>");  // User password
    finalURLStr += "&NewUserGender=" + "male";                                          // "male" or "female"
    finalURLStr += "&UserAge=" + 29;                                                    // user age (int)
    finalURLStr += "&NewUserEmail=" + fixedEncodeURIComponent(anEmail);                 // user email
    finalURLStr += "&StatusID=" + -1;                                                   // see status table (1-7 or -1)
    finalURLStr += "&FuzzLocationEnabled=" + 1;                                         // true / false: location isn't/is reported accurately
    finalURLStr += "&CelebModeEnabled=" + 0;                                            // true if user is hidden from non-friends
    finalURLStr += "&ApplicationTag=";                                                  // app-related info
    finalURLStr += "&RESERVED=";                                                       // leave empty

    $.ajax({ url: finalURLStr })
        .done( function(data) {
            // Result is a simple string
            alert("Result: " + data);
        })
        .fail(function() {
            alert("Error while contacting Buddy!");
       });
}

I have created a form which asks for the user to input this information.

How can I pass the field data from my form to this function?

This is the code for my form:

<form id="register">

<table width="200" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td><label for="username">Username*:</label>
        <input type="text" id="username" name="username" />
    </td>
  </tr>
  <tr>
    <td><label for="password">Password*:</label>
        <input type="text" id="password" name="password" />
    </td>
  </tr>
  <tr>
    <td><label for="email">Email*:</label>
        <input type="text" id="email" name="email" />
    </td>
  </tr>
  <tr>
    <td><label for="gender">Gender*:</label>
        <select id="gender">
            <option value="null">Please Choose One...</option>
            <option value="Male">Male</option>
            <option value="Female">Female</option>
        </select>
    </td>
  </tr>
</table>
<input class="button" name="submit" type="button" value="submit" onclick="createUserWithName(username.value)" />
</form>

Im sure that once someone shows me how to send the username and email to the javascript function I will be able to figure out the rest.

Thank you

Upvotes: 0

Views: 34990

Answers (4)

Girish Thimmegowda
Girish Thimmegowda

Reputation: 2179

first create a function in which you collect all the form data

function getFormData(){ 
var name=document.getElementById('username').value;
var email=document.getElementById('email').value;
/* some other fields */
/* now call ur function by passing the above values */
createUserWithName(name, email);
}

In your form call getFormData() method

<input class="button" name="submit" type="submit" value="submit" onclick="getFormData()" />

Upvotes: 2

Anup Singh
Anup Singh

Reputation: 1573

var username = $("#username").val();
var email= $("#email").val();
createUserWithName(username , email);

Upvotes: 0

Aashray
Aashray

Reputation: 2763

You can just use:

document.getElementById('username').value

in your javascript function. Alert the value to check if you are getting it.

Upvotes: 2

kangoroo
kangoroo

Reputation: 357

there are many ways, but you can use jquery stuff

username = $("username").val();

Upvotes: 0

Related Questions