html form submits directly to postgres

I would like to enable html form submits directly to postgres on the same server that is running my Servoy solutions. Is installing php a good way to achieve this? If so, how can php be installed so it works with Servoy 6.0.x Tomcat?

Dean Westover
Choices Software, Inc.

Hi Dean,

why don’t you use a webservice hosted by Servoy for this?
RESTful webservice are widely used nowadays, so a lot of information can be found.

Here you can find more info on the webservice plugin:
https://wiki.servoy.com/display/public/DOCS/RESTful+Web+Services

Marc,

Thank you for the info and the link!

Dean

FYI: If you need web services, you should have a look at Velocity, which has more extended options and is easier to use and customize…
https://www.servoyforge.net/projects/ve … /WebClient

Install PHP just for a simple input form into the database would be overkill. You could do it via a simple HTML and JavaScript page in the Servoy application_server/server/webapps/ROOT folder and then use jQuery to hit a RESTful webservice (or Velocity) that you create in Servoy.

Alternatively, you could also do a simple JSP page, as that is natively supported in Servoy.

I have the sample “restfulws_testpage.html” page working with the sample Servoy solution. However, I am struggling with the easy part.

The sample sends the contents of a single form field that contains firstName and lastName surrounded with XML tags. I am trying to do the same with a form that has multiple fields, each with data that is not surrounded with XML tags. I found the below code, which is similar to what I want:

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("button").click(function(){
        var x = $("form").serializeArray();
        $.each(x, function(i, field){
            $("#results").append("<" + field.name + ">" + field.value + "</" + field.name + ">");
        });
    });
});
</script>
</head>
<body>

<form action="">
  First name: <input type="text" name="FirstName" value="Mickey1">

  Last name: <input type="text" name="LastName" value="Mouse1">

</form>

<button>Serialize form values</button>

<div id="results"></div>

</body>
</html>

However, I have not been able to modify the Servoy sample HTML page to work with multiple form fields with values that do not include XML tags. Attached is a page I created to help me understand the sample code and the servoy method required for a REST service POST (maybe it will help others like me who are new to XMLHttpRequests). How can I modify Servoy’s sample HTML page to work with multiple form fields with values that do not include XML tags?

Dean

Hi Dean,

I just quickly look at your code and I noticed you are sending things as XML. As far as I know the REST plugin only supports JSON. Do you require your data to be in XML format?

Hi Rob,

I tried the Servoy sample REST solution that comes with the default installation. The solution name is “servoy_sample_rest_ws”. A sample “restfulws_testpage.html” file is in the ROOT/examples folder. That is the file that has the XML. It works fine if the HTML form only has one field. However, my HTML forms will have multiple fields. I have not been able to modify the HTML code to allow posting of data for multiple fields. XML or JSON does not matter to me as long as I can post my data to a Servoy solution.

Dean
Servoy 6.0.x

Hi Dean,

Seems that the web service plugin does support XML (and JSON/JSONP). I did a quick test with the demo solution in Servoy 6 using the following code that does accept more than 1 value:

curl -X "POST" "http://localhost:8080/servoy-service/rest_ws/servoy_sample_rest_ws/ws_employees" \
	-H "Content-Type: application/json" \
	-d "{\"firstName\":\"Robert\",\"lastName\":\"Testing\"}"

Here is a simple webform with AngularJS that does the same as the code above here.

<!DOCTYPE html>
<html>
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js"></script>
    <script>
     angular.module('myApp', [])
                .controller('myCtrl', function ($scope, $http) {
                    $scope.success = '';
                    $scope.sendPost = function () {
                        $http.post("http://localhost:8080/servoy-service/rest_ws/servoy_sample_rest_ws/ws_employees", $scope.model)
                                .success(function (data, status, headers, config) {
                                    // reset the fields
                                    $scope.model.firstName = "";
                                    $scope.model.lastName = "";
                                    // show that it worked
                                    $scope.success = "Send successfully!";
                                })
                                .error(function (data) {
                                     $scope.success = "An error occured: " + data;
                                })
                    }
                });
    </script>
</head>
<body ng-app="myApp" ng-controller="myCtrl">
    <form>
        First name: <input ng-model="model.firstName">

        Last name: <input ng-model="model.lastName">

    </form>
    <button ng-click="sendPost()">Send Data</button>
    <p>{{success}}</p>
</body>
</html>

Hope this gets you further.

Hi Rob,

Wow. That Angular JS is elegant. It works perfectly.
You have also encouraged me to learn more about curl.

Thank you very much!

Dean

You’re totally welcome :)

HTML with AngularJS is very readable, and because you code the event(s) on the HTML tags all your code also keeps working when you change the structure of your HTML.
Something that is always a bit of a headache with libraries like JQuery where you have to code against the DOM structure using ID’s, classes or a path (and that path can change when you change the HTML structure).

As for curl, it’s one of the tools you can use to talk directly to web services. For instance there is also PostMan for Chrome and as Mac/Windows/Linux app. Or my favourite; Paw (Mac only). In fact that curl code was generated by Paw so I could show you a working syntax (without having to use Paw).