最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - Submit form field values to a Javascript function - Stack Overflow

programmeradmin3浏览0评论

I am using Buddy'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

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 = ".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

I am using Buddy.'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./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./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

Share Improve this question asked Aug 26, 2013 at 9:55 T JT J 7491 gold badge8 silver badges18 bronze badges 3
  • Is the javascript included in the html page? – Aashray Commented Aug 26, 2013 at 10:00
  • Yes, i have put it as part of the HTML page. – T J Commented Aug 26, 2013 at 11:39
  • Check my answer. That should work. – Aashray Commented Aug 26, 2013 at 11:54
Add a ment  | 

4 Answers 4

Reset to default 2

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()" />

You can just use:

document.getElementById('username').value

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

there are many ways, but you can use jquery stuff

username = $("username").val();
var username = $("#username").val();
var email= $("#email").val();
createUserWithName(username , email);
发布评论

评论列表(0)

  1. 暂无评论