I am trying to search for the right method to submit HTML form with the data from excel cell and retrieve part of the result back.
HTML form URL is http://www2.stat.gov.lt:8777/imones/sektor.html:
<form action="sektor.chk_sekt" method="POST">
<br>
<b>Ūkio subjekto kodas: </b>
<input type="text" name="imone01" size="9" maxlength="9">
<br>
<br>
<input type="submit" value=" OK ">
</form>
Example data strings to submit are 303305024, 300983557, the value to be extracted from the response on the page http://www2.stat.gov.lt:8777/imones/sektor.chk_sekt is the line:
<BR>
<B>Veiklos rūšis pagal EVRK red. 2: </B>
479100 - Užsakomasis pardavimas paštu arba internetu
<BR>
The values from each cell in column A should be submitted within loop, and the retrieved results should be filled into corresponding cells in column B.
I have reviewed several similar questions but they seem to be using some different format of form and doesn't suit in this case.
Here is the example using XHR and RegEx to retrieve the data you need:
Option Explicit
Sub RetriveData()
Dim i As Long
For i = 1 To Cells.Rows.Count
If Cells(i, 1).Value = "" Then Exit For
Cells(i, 2).Value = GetData(Cells(i, 1).Value)
Next
End Sub
Function GetData(sCompany As String) As String
Dim sContent As String
With CreateObject("MSXML2.XMLHTTP")
.Open "POST", "http://www2.stat.gov.lt:8777/imones/sektor.chk_sekt", False
.Send "imone01=" & sCompany
sContent = .ResponseText
End With
With CreateObject("VBScript.RegExp")
.Global = True
.MultiLine = True
.IgnoreCase = True
.Pattern = "<head>[\s\S]*?</head>|(?!<br>)<[^>]*>|[\r\n\t]*"
sContent = .Replace(sContent, "")
.Pattern = "<BR>Veiklos r\u016B\u0161is pagal EVRK red. 2: (.*?)<BR>"
With .Execute(sContent)
If .Count = 1 Then GetData = .Item(0).SubMatches(0) Else GetData = "n/a"
End With
End With
End Function
The output is as follows for me:
Related
I have this program that I am coding, the main thing is mostly to get familiar with Jax-rs and rest API. I am sending from a form in JSP page one input and with POST I pass it to my java Response class(that I use annotation #FormParam), it does what calculation I need and then I get the result in another link, what can i do to take the result just below my button on the same page? For my button function I use javascript, and when I push it I don't want to be redirected and take the result like this.
So i use this form and a javascript function in my jsp to pass the value in my POST method in java throw #FormParam
<form name="Form1" method="post">
<p>
Name : <input type="text" name="malename" id="malename" autofocus
value="enter your name..." />
</p>
<input type="submit" name="action" id="malename" value="I'm feeling lucky"
onclick="OnButton1();"> </form>
function OnButton1() {
document.Form1.action = "webapi/perfectmatcher/mate"
document.Form1.target = "_self"; // Open in a new window
document.Form1.submit(); // Submit the page
//return;
}
Here after i have the value i use two other classes to do the calculations and return the result i need. In a few words i pass a value in my form and when i push the button i am redirected in another link(the path of POST) and i display the result. I want to display the result below my button. I hope the code is helpful and i hope i am clear with my explanation
#POST
#Path("/mate")
#Produces(MediaType.TEXT_PLAIN)
public Response postMaleName(#FormParam("malename") String malename) {
String femalename = NumMatcher.numMatcher(Numberizer.numberizer(malename));
// FemaleName female = new FemaleName(femalename);
female.setName(femalename);
System.out.println(female.getName());
return Response.status(200).entity("Perfect Match for " + malename + " is " + femalename).build();
}
P.S.:The object female that i am trying to create its for testing purposes, i was trying somehow to get the information that i store but i cannot find how!!I used even jsp expressions but i get null value of course ...
<%
FemaleName female = new FemaleName("john");
String string = female.getName();
out.print(string);
%>
I have a form with two inputs, which acts as a calculator of sorts. Think of something like a mortgage calculator. A user needs to be able to input a value into input a (int or decimal), and have it calculate some value for input b. Likewise, a user must be able to input a value into input b (int), and have it calculate a value for input a. The values should calculate as the user types.
I have the calculation working, but I'm running into problems when I try to either a: empty the field to start over; or b: attempt to input a decimal into input a.
In the first case, neither input will let me remove the final character. Even if I highlight the entire value and hit backspace, the input stays at whatever value was there.
In the second case, the decimal never takes. If I try to type in 2.2, I get 22 instead.
Per the React documentation, I'm using an onChange event handler, which sets the state of my component, and then the inputs display that state.
Code example is here:
handleIntervalChange: ->
iVal = parseFloat #refs.confidenceInterval.getDOMNode().value
iVal = iVal/100
lVal = parseFloat #refs.confidenceLevel.getDOMNode().value
return false if _.isNaN ival
docCount = #calcDocsByCLevel lVal, iVal
docCount = null if _.isNaN(docCount) or docCount is Infinity
#setState
targetCLevel: lVal
targetCInterval: iVal
docCount: docCount
handleDocChange: ->
docCount = parseInt #refs.docCount.getDOMNode().value
return false if _.isNaN docCount
lVal = parseFloat #refs.confidenceLevel.getDOMNode().value
#setState
targetCLevel: lVal
targetCInterval: #calcIntervalByDocCount lVal, docCount
docCount: docCount
render: ->
<fieldset>
<div className='controls'>
<label htmlFor='confidence-interval'>
Confidence interval (%)
</label>
{<ValidatorTooltip errors={#state.errors['confidence-interval']}/> if #state.errors}
<input type='text' id='confidence-interval'
className={cIntervalClasses} ref='confidenceInterval'
value={#state.targetCInterval * 100}
onKeyUp={#handleKeyUp} onChange={#handleIntervalChange} />
</div>
<div className='controls'>
<label htmlFor='doc-count'>Number of documents</label>
{<ValidatorTooltip errors={#state.errors['doc-count']}/> if #state.errors}
<input type='text' id='doc-count' className={docClasses}
ref='docCount' onChange={#handleDocChange} onKeyUp={#handleKeyUp}
value={#state.docCount} />
</div>
</fieldset>
Got it. The problem was in my calculation methods - I needed some logic to catch if the document or interval fields were blank (and would then render the other fields blank).
I'm trying to set up a simple select dropdown form with Flask. Based on the option chosen, I grab different data from my database, and display it back into a div on my html template. But I can't seem to get the Flask request to register any of the select options. When I print request.form or request.args, I always get empty Dicts. It's probably something simple I'm missing but I can't seem to find the problem. I've gotten this to work with several input and button forms, but I can't get it to work right for selects.
Here is a bit of my html template code, with the form and select. I've tried both GET and POST method in the form.
<div class="options" id="options">
<form class="form-horizontal container-fluid" role="form" method="GET" action="exploresn2.html">
<div class="form-group">
<label for="xaxis" class="col-sm-2 control-label">X-axis:</label>
<div class="col-sm-2">
<select name="xaxis" class="form-control" id="xaxis">
<option selected value="mjd" id="mjd">MJD</option>
<option value="seeing" id="seeing">Seeing</option>
<option value="airmass" id="airmass">Airmass</option>
<option value="hourangle" id="hourangle">Hour Angle</option>
</select>
</div>
</div>
</form>
</div>
In Flask, at first, I tried inside my app
import flask
from flask import request, render_template, send_from_directory, current_app
explore_page = flask.Blueprint("explore_page", __name__)
#explore_page.route('/exploresn2.html', methods=['GET','POST'])
def explore():
xaxis = str(request.args.get("xaxis", "any"))
.... [populate new xaxis variable based on request option selected]
exploreDict['xaxis'] = xaxis
return render_template("exploresn2.html", **exploreDict)
or
mjd = valueFromRequest(key='mjd', request=request, default=None)
if mjd:
mjds = [int(exp.platedbExposure.start_time/(24*3600)) for exp in exposures]
xaxis = mjds
exploreDict['xaxis'] = xaxis
to look for and grab a specific values, or in the first case, any value select. The valueFromRequest is function that grabs data from either GET or POST requests.
but this returns nothing, and then I tried just printing the entire request.args (or request.form) and it returns and empty Dict. Everything I try it still returns empty Dicts. So I'm missing some set up somewhere I think but the form looks right to me?
I'm not sure if this is the actual answer to this problem that I was looking for, but here is what I came up with. I couldn't actually get the Flask to accept a GET request into the original explore method defined, so I implemented a new method in Flask to return a JSON object
#explore_page.route('/getdata', methods=['GET','POST'])
def getData(name=None):
name = str(request.args.get("xaxis", "mjd"))
xaxis = 'populate new xaxis data based on value of name'
data = '(x,y) data array filled with values for plotting'
axisrange = range of x,y data for axes for plot
return jsonify(result=data, range=axisrange)
and then I just made a GET request via javascript to that method whenever the select button changes. So in my exploresn2.html template I have (using Flot for plotting)
$("#xaxis").change(function(){
var newname = $("#xaxis :selected").text();
var axes = plot.getAxes();
options = plot.getOptions();
var plotdata = plot.getData();
// make a GET request and return new data
$.getJSON($SCRIPT_ROOT + '/getdata', {'xaxis':$("#xaxis :selected").val()},
function(newdata){
// set new data
for (var i = 0; i < plotdata.length; ++i) {
plotdata[i].data = newdata.result[plotdata[i].label];
}
// set new axes
axes.xaxis.options.panRange = [newdata.range[0]-50,newdata.range[1]+50];
axes.xaxis.options.axisLabel = newname;
axes.xaxis.options.min = newdata.range[0]-1;
axes.xaxis.options.max = newdata.range[1]+1;
axes.yaxis.options.min = newdata.range[2];
axes.yaxis.options.max = newdata.range[3];
// redraw plot
plot.setData(plotdata);
plot.setupGrid();
plot.draw();
});
});
We need to create html dropdown list dynamically. Following is our current code. Option1 and Option2 are strings that include all the items. The reason we have two strings is we split all the items in two groups.
<td>
<div id="dvGroup" style="display:none;" runat="server"></div>
</td>
string Option1 = "<optgroup label='Group1'><option>1</option><option1>2</option>";
string Option2 = "<optgroup label='Group2'><option>a</option><option>b</option><option>c</option>;
…
dvGroup.InnerHtml = "<Select name='ddGroup' id='ddGroup'>" + Option1 + Option2 + "</Select>";
But by setting InnerHtml will potentially bring some security problem. So I changed it to:
HtmlSelect select = new HtmlSelect();
select.ID = "ddGroup";
select.Name = "ddGroup";
select.ClientIDMode = ClientIDMode.Static;
dvGroup.Controls.Add(select);
select.Controls.Add(new LiteralControl(Option1 + Option2));
However, it threw following error at the line select.Controls.Add…
'System.Web.UI.HtmlControls.HtmlSelect' does not allow child controls.
How do I fix this? Another problem is, setting ClientIDMode to Static only make sure the id is not changed, but name still being added extra stuff. How do I make sure name is not changed either?
You can do this way:
HtmlSelect mHtmlSelect = new HtmlSelect();
mHtmlSelect.ID = "ddGroup";
mHtmlSelect.Name = "ddGroup";
mHtmlSelect.ClientIDMode = ClientIDMode.Static;
String Option1 = "Hello";
String Option2 = "World";
mHtmlSelect.Items.Add(new ListItem(Option1 + Option2));
this.Controls.Add(mHtmlSelect);
Output:
<select name="ddGroup" id="ddGroup">
<option value="HelloWorld">HelloWorld</option>
</select>
In jQuery, there is a function to serialize a form element so for example I can submit it as an ajax request.
Let's say we have a form such as this:
<form id="form">
<select name="single">
<option>Single</option>
<option selected="selected">Single2</option>
</select>
<input type="checkbox" name="check" value="check1" id="ch1">
<input name="otherName" value="textValue" type="text">
</form>
If I do this with the help of jquery
var str = $( "form" ).serialize();
console.log(str);
the result would be
single=Single2&check=check1&otherName=textValue
Is there such functionality in dart's FormElement or I have to code it myself? Thanks.
I came up with my own simple solution that might not work in all cases (but for me it is workikng). The procedure is this:
First we need to extract all input or select element names and values from the form into Dart's Map, so the element name will be the key and value the value (e.g. {'single': 'Single2'}).
Then we will loop through this Map and manually create the resulting string.
The code might look something like this:
FormElement form = querySelector('#my-form'); // To select the form
Map data = {};
// Form elements to extract {name: value} from
final formElementSelectors = "select, input";
form.querySelectorAll(formElementSelectors).forEach((SelectElement el) {
data[el.name] = el.value;
});
var parameters = "";
for (var key in data.keys) {
if (parameters.isNotEmpty) {
parameters += "&";
}
parameters += '$key=${data[key]}';
}
Parameters should now contain all the {name: value} pairs from the specified form.
I haven't seen anything like that yet.
In this example Seth Ladd uses Polymers template to assign the form field values to a class which get's serialized.