html form submits directly to postgres

Questions and answers regarding general SQL and backend databases

html form submits directly to postgres

Postby Westy » Thu Aug 25, 2016 9:12 pm

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.
Westy
 
Posts: 833
Joined: Fri Feb 13, 2004 5:27 am
Location: Lynnfield, Massachusetts USA

Re: html form submits directly to postgres

Postby mboegem » Fri Aug 26, 2016 12:54 am

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 Boegem
Solutiative / JBS Group, Partner
• Servoy Certified Developer
• Servoy Valued Professional
• Freelance SAN Developer

Image
User avatar
mboegem
 
Posts: 1386
Joined: Sun Oct 14, 2007 1:34 pm
Location: Hoofddorp, The Netherlands

Re: html form submits directly to postgres

Postby Westy » Fri Aug 26, 2016 6:29 pm

Marc,

Thank you for the info and the link!

Dean
Westy
 
Posts: 833
Joined: Fri Feb 13, 2004 5:27 am
Location: Lynnfield, Massachusetts USA

Re: html form submits directly to postgres

Postby ptalbot » Sat Aug 27, 2016 4:09 am

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
Patrick Talbot
Freelance - Open Source - Servoy Valued Professional
http://www.servoy-stuff.net
https://www.servoyforge.net
--------------------------------------------
Servoy 5.2.16 / 6.0.9 / 6.1.6 / 7.3.1
All OSes / Java 5 & 6 & 7
User avatar
ptalbot
 
Posts: 1612
Joined: Wed Mar 11, 2009 5:13 am
Location: Montreal, QC

Re: html form submits directly to postgres

Postby goldcougar » Mon Aug 29, 2016 3:27 pm

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.
Scott Butler
iTech Professionals, Inc.
SAN Partner

Servoy Consulting & Development
Servoy University- Free Training Videos
Servoy Guy- Free Plugins, Tips & Resources
ServoyForge- Open Source Servoy Components
User avatar
goldcougar
Servoy Expert
 
Posts: 658
Joined: Sun Jan 08, 2006 7:15 am
Location: Cincinnati, OH

Re: html form submits directly to postgres

Postby Westy » Mon Aug 29, 2016 6:53 pm

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:
Code: Select all
<!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("&lt;" + field.name + "&gt;" + field.value + "&lt;/" + field.name + "&gt;");
        });
    });
});
</script>
</head>
<body>

<form action="">
  First name: <input type="text" name="FirstName" value="Mickey1"><br>
  Last name: <input type="text" name="LastName" value="Mouse1"><br>
</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
Attachments
resfulws_testpage.html page code.png
resfulws_testpage.html page code.png (207.03 KiB) Viewed 1641 times
Last edited by Westy on Mon Aug 29, 2016 8:53 pm, edited 2 times in total.
Westy
 
Posts: 833
Joined: Fri Feb 13, 2004 5:27 am
Location: Lynnfield, Massachusetts USA

Re: html form submits directly to postgres

Postby ROCLASI » Mon Aug 29, 2016 7:59 pm

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?
Robert Ivens
ROCLASI Software Solutions / JBS Group, Partner
SAN Developer / Servoy Valued Professional / Servoy Certified Developer
Twitter: @roclasi / @servoyforge
--
ServoyForge - Building Open Source Software.
PostgreSQL - The world's most advanced open source database.
User avatar
ROCLASI
Servoy Expert
 
Posts: 5230
Joined: Thu Oct 02, 2003 9:49 am
Location: Netherlands/Belgium

Re: html form submits directly to postgres

Postby Westy » Mon Aug 29, 2016 8:39 pm

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
Westy
 
Posts: 833
Joined: Fri Feb 13, 2004 5:27 am
Location: Lynnfield, Massachusetts USA

Re: html form submits directly to postgres

Postby ROCLASI » Tue Aug 30, 2016 11:09 am

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:
Code: Select all
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.
Code: Select all
<!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"><br>
        Last name: <input ng-model="model.lastName"><br>
    </form>
    <button ng-click="sendPost()">Send Data</button>
    <p>{{success}}</p>
</body>
</html>


Hope this gets you further.
Robert Ivens
ROCLASI Software Solutions / JBS Group, Partner
SAN Developer / Servoy Valued Professional / Servoy Certified Developer
Twitter: @roclasi / @servoyforge
--
ServoyForge - Building Open Source Software.
PostgreSQL - The world's most advanced open source database.
User avatar
ROCLASI
Servoy Expert
 
Posts: 5230
Joined: Thu Oct 02, 2003 9:49 am
Location: Netherlands/Belgium

Re: html form submits directly to postgres

Postby Westy » Tue Aug 30, 2016 4:37 pm

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
Westy
 
Posts: 833
Joined: Fri Feb 13, 2004 5:27 am
Location: Lynnfield, Massachusetts USA

Re: html form submits directly to postgres

Postby ROCLASI » Tue Aug 30, 2016 6:30 pm

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).
Robert Ivens
ROCLASI Software Solutions / JBS Group, Partner
SAN Developer / Servoy Valued Professional / Servoy Certified Developer
Twitter: @roclasi / @servoyforge
--
ServoyForge - Building Open Source Software.
PostgreSQL - The world's most advanced open source database.
User avatar
ROCLASI
Servoy Expert
 
Posts: 5230
Joined: Thu Oct 02, 2003 9:49 am
Location: Netherlands/Belgium


Return to SQL Databases

Who is online

Users browsing this forum: No registered users and 6 guests