Unable to pass data from HTML Form as JSON - html

I'm trying to achieve below result using simple HTML form:
{"name": "Abc","email": "abc#abc.org", "message":"msg"}
HTML code:
<form name = "myform" action="URL" method="POST" enctype="application/json">
<input type="hidden" name="name" value="Abc">
<input type="hidden" name="email" value="Abc#abc.org">
<input type="hidden" name="message" value="msg">
<input type ="submit" value="Submit">
</form>
But this doesn't work. Someone suggested to use below and it worked but not sure how it is encoding data into JSON format.
<form name = "myform" action="URL" method="POST" enctype="text/plain">
<input type="hidden" name='{"name":"Abc","email":"abc#abc.org","message":"'value='msg"}'>
<input type="submit" value="Submit">
</form>
Can someone please explain how the above works? Thanks in Advance.

The enctype attribute simply has no option to encode as JSON.
There was a proposal to add support, but it was abandoned.
text/plain encodes the data in a plain text format which is not reliably machine parsable (e.g. you can't distinguish between a new line as data and a new line as a record separator). You should never use it in production. It was designed as a debugging tool but, frankly, having software that can display parsed application/x-www-form-urlencoded data is more useful for that.
The code you have "works" because the plain text format just concatenates the names and values, one per line, and you have half a JSON text in the name and the other half in the value.
It's very fragile, and would be more so if the user was inputting any data themselves.

Related

Redirect to selected site with variable which i got

<form action="/subsite/" method="GET">
<input type="text" name="" placeholder="Your Nick">
<input class="button" type="submit" />
</form>
I want to redirect I mean it should looks like
www/subsite/text
What should I use ? POST ?
The method="get" means that the parameters (and values) will be sent in the query string (the stuff after the question mark in the URL). eg.
/subsite?input_field_name=input_field_value
In your case, the input field doesn't have a name, which will cause problems. You probably want something like this:
<input type="text" name="nickname" placeholder="Your Nick">
So if you submit this form:
<form action="/subsite" method="get">
<input type="text" name="nickname" placeholder="Your Nick">
</form>
Then after submitting, the browser will go to:
/subsite?nickname=value_of_nickname_variable
If you use a method="post", then the form data (variables) will be sent along in the request body, not the query string. There are other differences between get/post, but that's one of them :)
If you just want to do a simple redirect when clicking the button, you could use javascript instead, eg.: window.location.href='/my_url_path_here'

Fetch HTML Service Values by ID

In the sample code:
HTML (Index):
<form>
<br><br>
Name:
<br>
<input type="text" name="name">
<br><br>
Comments:
<br>
<input type="text" id="comments">
<br><br>
<input type="button" id="button" value="Submit Info" onclick="google.script.run
.testFunction(this.form)">
</form>
Code:
function doGet() {//Creates the webpage
return HtmlService.createTemplateFromFile('Index')//From the GUI file.
.evaluate()
.setSandboxMode(HtmlService.SandboxMode.IFRAME);
}
function testFunction(form){
Logger.log(form.name);//Works when using the 'name' of the element.
Logger.log(form.comments);//Does not work when using the 'ID'.
}
Why does a value get returned when adding the 'Name' of the HTML value, but not the 'ID'? If I want to get the value by ID, how would I do this instead? I've tried:
form.getElementById("comments");
But this just throws me an error, and to be honest I'm out of ideas after that.
Note: It's completely acceptable to me to use the 'name' field, as I have been doing, I'm just curious as I'm brushing up on my HTML.
I believe what Google is doing behind the scenes is extracting the form out as a HTML form and sending it back to the server. HTML forms use the name attribute to store all their name value pairs when sent back to the server. So if this was a get request for this form:
<form>
<input type="text" name="field1" value="value1"/>
<input type="text" name="field2" value="value2"/>
</form>
it would look something like ?field1=value1&field2=value2.
So on the server side in your Code.gs you are basically getting this back as an object (not a DOM object, which is why your form.getElementById("comments"); won't work).
Either way, the standard with <input> tags is to use the name attribute, so you should be sticking with that. The id attribute should really only be used if you need to look up a value in your client side javascript.

How to format a form GET request url

How could i make a search input for a website where rather than a regular "?search=" it's just text with something appended, in this case ".htm"?
So for example argos works like this and if i type in "weight lifting" the url it makes is -
www.argos.co.uk/static/Search/searchTerm/weight+lifting.htm
I was trying the following however it does not work correctly -
<form name="search" method="get" style="margin-bottom:0%;" action="http://www.argos.co.uk/static/Search/searchTerm/"><input type="text" name="" maxlength="300">
<input type="hidden" name="" value=".htm">
</form>
Is it possible to create a search input that could create the url i needed with a regular html form?

Specifying field serialization priority in an HTML multipart upload form

I have a file upload form which needs to have certain fields created in the multipart request before other fields. It works great if I order my form like this:
<form method="POST" enctype"multipart/form-data" action="/upload/">
<input type="text" name="newFileName">
<input type="file" name="file">
<input type="submit">
</form>
However, if I change the order of my form so that the newFieldName field comes after the file field, things break, as newFieldName needs to be parsed first by my upload handler. Is there a way to specify priority as for which fields get serialized in which order?

HTML form is not encoding the URL of the search in Velocity

I'm having an issue with the URL encoding of a search query, I have this in a Velocity template.
<form method="GET" action="$req.contextPath/plugins/peopledirectory/search.action">
<div class="greyboxfill" style="width: 420px">
<input type="hidden" name="pageId" value="$pageId"/>
<input type="text" name="search" id="search" size="30" value="$search"/>
<input type="submit" name="searchbtn" value="Pesquisar">
</div>
</form>
The problem is that when I click the submit button, the search string is not URL encoded and if I search for something like ME&A, it only searches for ME. Is there any definition needed in Velocity to make that work?
I seen around the web that the form HTML tag has a inner URL encoding, why is it not working in this case?
The best thing to do is explicitly escape the string using EscapeTool from VelocityTools. This is an additional library that you'll need to download and then include in your velocity context.
URL encoding is then as easy as:
$escape.url($search)