Html form (insert xquery) save in xml - html

I have an html form, when I press the submit button I want to take the information and use the functions of Xquery so that with an (insert) xquery will be saved in the xml.
--html--
<html>
<body>
<form action="insert.xq">
<span ="label">Name:</span>
<input type="text"/>
<span ="label">Telephone:</span>
<input type="text"/>
<span ="label">Website:</span>
<input type="text"/>
<span ="label">Date:</span>
<input type="text"/>
<!-- Buttons submit and reset-->
<input type="submit" value="Submit"/>
<input type="reset" value="Delete"/>
</form>
</body>
</html>
--exist-db XQUERY--
xquery version "3.1";
declare namespace exist="http://exist.sourceforge.net/NS/exist";
declare namespace xmldb="http://exist-db.org/xquery/xmldb";
declare namespace request = "http://exist-db.org/xquery/request";
update insert
<supplier id=" ">
<name> </name>
<telephone> </telephone>
<website> </website>
<date> </date>
</supplier>
into doc("suppliers.xml")//supplier
--xml--
<?xml version="1.0" encoding="UTF-8"?>
<suppliers xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="validate.xsd">
<supplier id="1">
<name>Microsoft</name>
<telephone>943715587</telephone>
<website>www.microsoft.com</website>
<date>10/08/2016</date>
</supplier>
</suppliers>
Sorry for my english.

Parameters in the HTTP REST interface are accessed into variables in an XQuery using the request function set as in:
let $name := request:get-parameter("name","")
let $telephone := request:get-parameter("telephone","")
let $website := request:get-parameter("website","")
let $date := request:get-parameter("date","")
Then used as you would in the rest of the code as a XQuery variable:
update insert
<supplier id=" ">
<name>{$name}</name>
...
Now, that is a start. Your HTML needs name attributes on the input elements to pass them as parameters into the query. You need a return after the let's to return and the http request should be set to GET. And not knowing your system, the query and the documents would have to be open to all unless you are in some session-based environment and the HTML GET can find the XQuery based on some pathing in your controller or something.
This is essentially the same as http://path/to/myquery.xm?name=John%20Doe&telephone=123456789 ... and you want to grab the name and telephone.

Related

How do I POST an array to the server?

I am trying to post an array of multiple geolocation addresses (generated by Google maps) to my server. The array displays correctly as an array while on the client side, however, the array arrives at the server as a string and not as an array. I am not entirely sure why this happens, also I can't figure out how to convert this string back into an array on the server.
Find below the code of the array while on the client side via the browser:
console.log("Geolocations: " ,selecedAddresses);
This codeabove yields an array of 3 items as illustrated below:
Geolocations: (3) ['Kileleshwa, Nairobi, Kenya', 'Ntinda, Kampala, Uganda', 'Uhuru Highway, Nairobi, Kenya']
When the same array is expanded for better readability:
0: "Kileleshwa, Nairobi, Kenya"
1: "Ntinda, Kampala, Uganda"
2: "Uhuru Highway, Nairobi, Kenya"
length: 3
For me to be able to POST the array to the server, I assign the array value to an HTML input field in a form.
document.getElementById('locationArray').value = selecedAddresses;
Find below the code to my HTML form where input field resides:
<form action="/advertiser" method="POST" enctype="multipart/form-data">
<div class="mb-3">
<input type="text" name="locationArray" value="" hidden/>
</div>
<button type="submit" class="btn btn-secondary"> Add location </button>
</form>
When the user clicks on the Add location button, the array is submitted to the server.
And now for the server side code...
app.route('/Geolocations')
.post((req, res)=>{
let locationArray = req.body.locationArray;
console.log("locationArray: " +locationArray);
})
The code above yeilds:
locationArray: "Kileleshwa, Nairobi, Kenya, Ntinda, Kampala, Uganda, Uhuru, Highway, Nairobi, Kenya"
Instead of the desired out put:
['Kileleshwa, Nairobi, Kenya', 'Ntinda, Kampala, Uganda', 'Uhuru Highway, Nairobi, Kenya']
Looking forward to your help.

MeteorJS: How to get id to load from collection

I'm trying to load an array (with simple text) and trying to load it up on the template whenever it is called. How do I get the ID from that specific item to get the array that I stored in it?
HTML Template:
<template name="commentMarker">
<div id="viewMarker">
<h3 id="markerTitle">{{markerName}}</h3>
<h6 id="markerCategory">{{markerCategory}}</h6>
<br>
<fieldset>
<legend>Description</legend>
<p>{{markerDescription}}</p>
</fieldset>
<form id="commentForm">
<fieldset>
<legend>Comments</legend>
<input type="text" id="markerId" name="idForComment" value={{markerId}}>
<textarea rows="3" cols="19" name="comment" id="commentArea" placeholder="Insert your comment here..."></textarea>
{{#each comments}}
<p id="oneComment">{{this}}</p>
{{/each}}
</fieldset>
<input type="submit" value="Comment" class="commentButton">
<input type="submit" value="Close" class="exitButton">
</form>
</div>
</template>
JS:
Template.commentMarker.helpers({
comments(){
alert(template.find("#markerId").value);
if(commentArray.length===0) return;
else return commentArray;
}});
This is where I insert the comment into the collection's item and it's working fine
Template.commentMarker.events({
'click .commentButton': function(e, template){
e.preventDefault();
var id = template.find("#markerId").value;
var comment = template.find("#commentArea").value;
Points.update(id, { $push: { comments: comment }});
commentArray = Points.findOne(id).comments;
template.find("#commentArea").value = ' ';
}
I tried with commentArray as a global variable which still is. But I'm at loss how I can get the Id from that specific item, I even put it's Id (with hidden display) in the form to actually be able to insert the comment. But it doesn't help me with showing the comments because I cannot seem to get to this field in the Template.helpers ...
Not entirely sure what you are trying to do. It's almost like as if you are displaying the comments right after you updated in to the collection. It looks like you are doing this entirely on local and not a online collection.
However, storing it as a session would work...or reactive var. Might not be the best solution tho. Basically replace commentArray = Points.findOne(id).comments; with:
Session.set('comments', Points.findOne(id).comments)
Then to get it out in helpers:
let commentArray = Session.get('comments')
It's not safe to use it all the time tho for sensitive data. Also try catch the findOne(id).comments because it does produce errors if it happen to not find it.
NOTE: If you are going to use Meteor.Methods, you cannot use Session. You have to return the id and find it in your helpers.

Botdetect Captcha HTML <taglib>

I want to add Botdetect Captcha to my html file.But in the website of the Botdetect captcha, there is example only for jsp. In this jsp file, <taglib> is used like that:
<%#taglib prefix="botDetect" uri="https://captcha.com/java/jsp"%>
....
<botDetect:captcha id="basicExample" userInputID="captchaCode" />
<div class="validationDiv">
<input name="captchaCode" type="text" id="captchaCode" value="${basicExample.captchaCode}" />
<input type="submit" name="validateCaptchaButton" value="Validate" id="validateCaptchaButton" />
<span class="correct">${basicExample.captchaCorrect}</span>
<span class="incorrect">${basicExample.captchaIncorrect}</span>
</div>
Is there any alternative for <%#taglib> in HTML files. How can I solve this problem?
I'm facing this problem because I use Thymeleaf instead of JSP so I can't use the taglib but I still have "dynamic HTML". I'm answering assuming this is your case.
I saw a possible solution on the help pages here: https://captcha.com/doc/java/jsp-captcha.html at section 2:
<%
// Adding BotDetect Captcha to the page
Captcha captcha = Captcha.load(request, "exampleCaptcha");
captcha.setUserInputID("captchaCode");
String captchaHtml = captcha.getHtml();
out.write(captchaHtml);
%>
This is JSP code but can be easily adapted to Thymeleaf. This in the #Controller that opens the page:
Captcha captcha = Captcha.load(request, "exampleCaptcha");
captcha.setUserInputID("captchaCode");
String captchaHtml = captcha.getHtml();
model.addAttribute("captchaHtml", captchaHtml);
and the html is like
<th:block th:utext="${captchaHtml}"></th:block>
The code for captcha checking is the same as for JSP, but placed in the #Controller that handles the form:
// validate the Captcha to check we're not dealing with a bot
boolean isHuman = captcha.validate(request.getParameter("captchaCode"));
if (isHuman) {
// TODO: Captcha validation passed, perform protected action
} else {
// TODO: Captcha validation failed, show error message
}
To finish you also need to edit web.xml (if you have it) and import the java libs as by the official docs. I'm using Gradle and importing 'com.captcha:botdetect-servlet:4.0.beta3.7'
Warning: your server should be on HTTPS or you might get this error when using version 4.0.beta3.7:
Captcha.UserInputID is not set. Your implementation of BotDetect is
not completely secure yet
<%#taglib prefix="botDetect" uri="https://captcha.com/java/jsp"%>
....
<botDetect:captcha id="basicExample" userInputID="captchaCode" />
<div class="validationDiv">
<input name="captchaCode" type="text" id="captchaCode" value="${basicExample.captchaCode}" />
<input type="submit" name="validateCaptchaButton" value="Validate" id="validateCaptchaButton" />
<span class="correct">${basicExample.captchaCorrect}</span>
<span class="incorrect">${basicExample.captchaIncorrect}</span>
</div>
That is wrong, with HTML (HyperText Markup Language) you have to list the version of HTML in this case it is Doctype HTML so the following one should be correct...
<!DOCTYPE html>
<%#taglib prefix="botDetect" uri="https://captcha.com/java/jsp"%>
....
<botDetect:captcha id="basicExample" userInputID="captchaCode" />
<div class="validationDiv">
<input name="captchaCode" type="text" id="captchaCode" value="${basicExample.captchaCode}" />
<input type="submit" name="validateCaptchaButton" value="Validate" id="validateCaptchaButton" />
<span class="correct">${basicExample.captchaCorrect}</span>
<span class="incorrect">${basicExample.captchaIncorrect}</span>
</div>

How to process a POST request in SWI-Prolog?

I have an HTML form like this one:
<form action="test" method="post">
<input name="first_name" type="text"/>
<input name="last_name" type="text" />
<input name="age" type="text" />
<input type="submit" value="Send"/>
</form>
How do I get the values of the input fields and print them on screen, just like in any other procedural programming language such as PHP, ASP or JSP?
I tried to solve the problem the following way:
:- use_module(library(http/thread_httpd)).
:- use_module(library(http/http_dispatch)).
:- http_handler(root(test), reply, []).
:- http_handler('test', reply, []).
server(Port) :-
http_server(http_dispatch, [port(Port)]).
reply(Request) :-
member(method(post), Request), !,
http_read_data(Request, Data, []),
format('application/x-www-form-urlencoded', []),
format(Data).
That brought me nothing more than an error with the 500 code (internal server error).
You should use the http/http_client library (:- use_module(library(http/http_client))).
Additionally, I'm not sure how having two handlers for test will work.
Finally, I think that format(Data) might not work, especially since it is expected to return an html document.
By the way, to retrieve the values of the fields you can do something like:
http_read_data(Request, [first_name=FN, last_name=LN, age=A|_], []).
I'm pretty new with the http prolog lib, I would suggest checking http://www.pathwayslms.com/swipltuts/html/
Essentially, you'll handle the request like normal, checking that the method(Method) term in the request is method(post).
http_read_data will read the request body.
the body will be encoded like an URI query string, so uri_query_components/2
will convert it to a list of Key=Value terms
?- uri_query_components('a=b&c=d%2Bw&n=VU%20Amsterdam', Q).
Q = [a=b, c='d+w', n='VU Amsterdam'].
For others looking for similar info - if your response is json, you can use read_json_dict to get the data as a dict.
I use library(http/http_parameters). With that, I can do
load_graph(Request) :-
http_parameters(Request,
[path(Path, [atom]),
aperture(Aperture, [integer])]),
where load_graph is the handler for the form
...
html(form([action(Ref)],
dl([dt('Root Path'), dd(input([name=path, type=text, value=Default])),
dt('Aperture'), dd(select([name=aperture], Aplist)),
dt('Go!'), dd(input([type=submit, value='Load!']))
]))).

Passing a url in a form from page to servlet drops part of query string

If I have a form on a JSP like this:
<form action = "/myApp/myServlet?rssFeedURL=${rssFeedURL}' />" method = "post">
<input type = "button" value = "See data for this RSS feed."/>
</form>
What I find is that if the variable ${rssFeedURL} has no query string, then the server receives it properly, e.g.:
http://feeds.bbci.co.uk/news/rss.xml
But if a query string exists, e.g.:
http://news.google.com/news?ned=us&topic=m&output=rss
I expect that it is to do with the encoding of the '&' character. Can anyone advise?
The server receives only:
http://news.google.com/news?ned=us
My pages are charset=UTF-8 encoded.
You need to URL-encode request parameters. Otherwise they will be interpreted as part of the initial request URL.
JSTL offers you the <c:url> for this.
<c:url var="formActionURL" value="/myApp/myServlet">
<c:param name="rssFeedURL" value="${rssFeedURL}" />
</c:url>
<form action= "${formActionURL}" method="post">
...
An alternative is to create an EL function which delegates to URLEncoder#encode().