0

How do I POST all the values from a form to an sql adapter that inserts the form values into the database?

  1. I have a form with an onclick event

  2. I have an adapter setup with a procedure to addUserInfo:

    function addUserInfo(firstname, lastname, email, state, province, zippostal, phonenumber, streetnamenumber, city) {
        return WL.Server.invokeSQLStatement({
            preparedStatement : addStatement,
            parameters : [firstname, lastname, email, state, province, zippostal, phonenumber, streetnamenumber, city]
        });
    }
    

Can I get some advice/an example of how to connect the two?

Idan Adar
  • 44,156
  • 13
  • 50
  • 89
Spindoctor
  • 434
  • 3
  • 17

1 Answers1

0

The following answer provides an end-to-end scenario for inserting values taken from the HTML and into a database: https://stackoverflow.com/a/25164028/1530814


You need to get the input from the HTML and use it as the WL.client.invokeProcedure's parameters:

function loadFeeds1(){
    var invocationData = {
        adapter:"car2",
        procedure:"getuser",
        parameters:[$('#carnum').val(),$('#details').val()]
    };

    WL.Server.invokeProcedure(invocationData,{
        onSuccess :loadFeedsSuccess1,
        onFailure :loadFeedsFailure1,
    });
}

HTML:

<h1>Test Insert Into Database</h1>
<input type="text" id="value1" placeholder="value1"/><br/>
<input type="text" id="value2" placeholder="value2"/><br/>
<input type="button" value="Insert values to database" onclick="insertValuesToDB();"/>

main.js:

function insertValuesToDB() {
    var invocationData = {
        adapter: 'insertValuesAdapter',
        procedure: 'insertValuesProcedure',
        parameters: [$('#value1').val(), $('#value2').val()]
    };

    WL.Client.invokeProcedure(invocationData, {onSuccess: insertSuccess, onFailure: insertFailure});
}

function insertSuccess() {
    alert("success");
}

function insertFailure() {
    alert("failure");
}

Adapter XML:

...
...
<connectivity>
    <connectionPolicy xsi:type="sql:SQLConnectionPolicy">
        <dataSourceDefinition>
            <driverClass>com.mysql.jdbc.Driver</driverClass>
            <url>jdbc:mysql://localhost:3306/worklight_training</url>
            <user>Worklight</user>
            <password>Worklight</password> 
        </dataSourceDefinition> 
    </connectionPolicy>
    <loadConstraints maxConcurrentConnectionsPerNode="5" />
</connectivity>

<procedure name="insertValuesProcedure"/>
...
...

Adapter implementation:

var insertValuesProcedureStatement = WL.Server.createSQLStatement("INSERT INTO users(userId, firstName, lastName, password) VALUES (?,?, 'someLastName', 'somePassword')");

function insertValuesProcedure(value1,value2) {
    return WL.Server.invokeSQLStatement({
        preparedStatement : insertValuesProcedureStatement,
        parameters : [value1,value2]
    });
}
Community
  • 1
  • 1
Idan Adar
  • 44,156
  • 13
  • 50
  • 89
  • Thank you for this example, but I'm not sure I understand how the insertValuesToDB() will know to post these values to a remote server which has the DB. Can you please explain. – Spindoctor Jan 15 '15 at 16:11
  • Before you write code, go and read the tutorials IBM provides in the Developer Center. The function invokes an adapter request which sends the data to the backend, where it runs SQL code to insert the values. Go and read on adapters. – Idan Adar Jan 15 '15 at 16:14
  • Thanks. I have ticked as green. – Spindoctor Jan 15 '15 at 16:18