Extract submit button value using jsoup - html

I have used HTTPClient to connect to a website and I can successfully access required data from the website using jsoup.
I have the following code from which I need to extract the submit button info.
<form method="POST" action="test.jsp" >
<font size="2">
<input type="hidden" name="num" id="num" value=123 >
<input type="hidden" name="iec" id="iec" value=456 >
<input type="submit" onclick=" return check();" value="Print" name="B1">
</font>
</form>
How can I access the value and name of the submit button?

You can access those values using the attr(String attribute) method of Element. For example:
String html = "<form method=\"POST\" action=\"test.jsp\" >"
+ "<font size=\"2\">"
+ "<input type=\"hidden\" name=\"num\" id=\"num\" value=123 >"
+ "<input type=\"hidden\" name=\"iec\" id=\"iec\" value=456 > "
+ "<input type=\"submit\" onclick=\" return check();\" value=\"Print\" name=\"B1\">"
+ "</font>"
+ "</form>";
Document doc = Jsoup.parse(html);
Element bttn = doc.select("input[type=submit]").first();
String value = bttn.attr("value"); // will be Print
String name = bttn.attr("name"); // will be B1

Related

How to create a sub function with html code?

I'm trying to create a sub function in classic asp that shows Password: the text space ? Any suggestions?
Sub GetPassword()
response.write " Password:"
<input type="text" name="txtEmPwd" size="20" value="<%=strEmPwd%>"> 1st 3 chars of
last name <input type="text" name="txtFirst3" size="5" maxlength=3 value="<%=strFirst3%>">
end sub
I get an error where the greater than character
Microsoft VBScript compilation error '800a0400'
Expected statement
That's because you are writing html directly in server side code.
You have to add the html as a string, something like this:
Sub GetPassword()
dim html
html = "Password:"
html = html & "<input type='text' name='txtEmPwd' size='20' value='" & strEmPwd & "'> 1st 3 chars of last name "
html = html & "<input type='text' name='txtFirst3' size='5' maxlength='3' value='" & strFirst3 & "'>"
Response.Write html
end sub

Output results on same page based on user selection

I am creating an HTML page which will accept user inputs and queries a database at the backend. To start off I am just trying to take user's output and display how the query will look like on the same page.
It has two radio buttons.
I am able to display the user selections, but right now its shows text for all buttons instead of the radio button selection. How can I restrict the display of text based only on the radio button which is selected? Here is my code -
<form id="data" method="post">
<input type="radio" id="Name" > Name
<input type = "text" id="value1"> <br>
<input type="radio" id="Last"> Lastname
<input type = "text" id="value2"> <br>
<input type="submit" onclick="return query()" value="Get Total">
<p style="font-size:80%;"> <span id='boldStuff1'> </span> <br> <span id='boldStuff2' > </span> </p>
</form>
<div id="display" style="height: 50px; width: 100%;"></div>
function query(){
var a = document.forms["data"]["value1"].value;
var b = document.forms["data"]["value2"].value;
var display=document.getElementById("display")
document.getElementById('boldStuff1').innerHTML= " SELECT * FROM table WHERE Name = " + a ;
document.getElementById('boldStuff2').innerHTML= " SELECT * FROM table WHERE Last = " + b ;
return false;
}
Here is how query looks like right now -
http://jsfiddle.net/mw6FB/104/
Thanks!
This will work,
function query(){
//var a = document.forms["data"]["value1"].value;
//var b = document.forms["data"]["value2"].value;
//var display=document.getElementById("display")
//document.getElementById('boldStuff1').innerHTML= " SELECT * FROM table WHERE Name = " + a ;
//document.getElementById('boldStuff2').innerHTML= " SELECT * FROM table WHERE Last = " + b ;
var nameDisplay = document.getElementById('boldStuff1');
var lastNameDisplay = document.getElementById('boldStuff2');
if(document.getElementById('Name').checked) {
var a = document.forms["data"]["value1"].value;
nameDisplay.innerHTML= " SELECT * FROM table WHERE Name = '" + a + "'";
lastNameDisplay.innerHTML= "";
} else if(document.getElementById('Last').checked) {
var b = document.forms["data"]["value2"].value;
lastNameDisplay.innerHTML= " SELECT * FROM table WHERE Last = '" + b + "'";
nameDisplay.innerHTML= "";
}
return false;
}
<form id="data" method="post">
<input type="radio" id="Name" name="radioQuery" checked > Name
<input type = "text" id="value1"> <br>
<input type="radio" id="Last" name="radioQuery"> Lastname
<input type = "text" id="value2"> <br>
<input type="submit" onclick="return query()" value="Get Total">
<p style="font-size:80%;"> <span id='boldStuff1'> </span> <br> <span id='boldStuff2' > </span> </p>
</form>
<div id="display" style="height: 50px; width: 100%;"></div>
You're not doing anything with the radio buttons. Find out which one is selected (if any), and only assign the innerHtml of the selected one. You may want to clear the other one as well. You can do this with a simple if().
See for example How to get value of selected radio button?.
Also, if you want to be able to actually submit this form and read its values, you'll want to name your input elements.

How to get value of HTML element with VB.net

I create html element from database :
Dim table As String = "<form method='POST'>"
table &= "<input type='textbox' value='123' id='txtText'/>"
table &= "<input type="sumbit" text='Submit' /></form>"
When i click button submit, I want get value textbox with VB.NET
You just need to use RUNAT = "SERVER"
<form method='POST'>
<input type='textbox' value='123' id='txtText'/>
<input runat="SERVER" type="hidden" name="txtHidden" value="123" />
</form>
You need to set the value to hidden text, use runat = "SERVER"and get the value on server-side.

Error on submitting Checkbox value in ASP

This is in file: DrinkOrder.asp
<form action="DrinkResult.asp" method="post">
Drink:
<select name="drink">
<option>Coffee</option>
<option>Tea</option>
<option>Hot Chocolate</option>
</select>
<p/>
Sugar:
<input type="radio" name="sugar" value="1"> 1
<input type="radio" name="sugar" value="2"> 2
<input type="radio" name="sugar" value="3"> 3
<p/>
Milk: <input type="checkbox" name="milk">
<p/>
<input type="submit" value="Submit Order"><input type="reset" value="Reset">
</form>
This is in file: DrinkResult.asp
<%#language="Javascript"%>
<%
function milkOn(form){
var with = "";
if(form == "1") { with = "With milk"; }
else { with = "No milk"; }
return with;
}
%>
<%=("<table border=\"1\">")%>
<%=("<tr><th><i>Drink:</i></th>" + "<td> " + (Request.Form("drink")) + "</td>")%>
<%=("<tr><th><i>Sugar:</i></th>" + "<td> " + (Request.Form("sugar")) + "</td>")%>
<%=("<tr><th><i>Milk:</i></th>" + "<td>" + (milkOn(Request.Form("milk"))) + "</td>")%>
<%=("</table>")%>
After submitting form from DrinkOrder.asp to DrinkResult.asp, I received an error which clearly I don't know how to fix it.
After a few modification I made to detect the error location, I'm pretty sure it is located at milkOn(Request.Form("milk")) in file DrinkResult.asp because when I remove the statement, the result shows up.
What's wrong with my code here?
change
if(form == "1")
to
if(form == "on")
otherwise the code seems to test fine for me.

form submission on same page

JavaScript:
function CreateMsg() {
var MsgDOM = document.getElementById("MSG");
MsgDOM.innerHTML = "Hello, " + document.forms[0].FNAME.value + " " + document.forms[0].LNAME.value + ". You're sex is " + document.forms[0].GENDER.value;
}
HTML:
<FORM NAME="DUH">
<INPUT TYPE=TEXT NAME=FNAME>
<INPUT TYPE=TEXT NAME=LNAME>
<SELECT NAME="GENDER">
<OPTION VALUE="Male">Male</OPTION>
<OPTION VALUE="Female">Female</OPTION>
</SELECT>
<BR><BR>
<input type="button" value="Search" onclick="javascript:CreateMsg();"/>
<BR><BR>
<SPAN ID="MSG"> </SPAN>
</FORM>
Please update the JavaScript method like this -
function CreateMsg() {
var MsgDOM = document.getElementById("MSG");
var duhForm = document.forms[0];
MsgDOM.innerHTML = "Hello, " + duhForm.FNAME.value + " " + duhForm.LNAME.value + ". You're sex is " + duhForm.GENDER.value;
duhForm.target="_self"; // or _top or _parent or _blank
duhForm.action="PleaseAskTheQuestion.do";
duhForm.submit();
}
mu is too short has already submitted the form on the same page, but server is too busy; no donuts for you. :-)