2 submit buttons - different queries, - java

I have a simple question and I can't find the answer by myself, so here it is.
Let's assume I have one simple index.html in which I have a form with 2 submit buttons. The action to those buttons is mapped to action.jsp. What I want to do is, if I click on the first submit button I want a query of type "SELECT * FROM USER WHERE ID = 1" and when I click on the second submit button I want a query of type "SELECT * FROM USER WHERE ID = 2". Can i do that in a single JSP file? How to do the check whether the first or the second submit button is pressed in the action.jsp - is that possible? Thanks!

Yes you can do both the operations on same JSP page.
Lets say you have two buttons like :
<input type="submit" name="bt1"/>
<input type="submit" name="bt2"/>
Now you can check their button click like :
if(request.getParamater("bt1")!=null)
{
//your first query
}
if(request.getParameter("bt2")!=null)
{
//your second query
}
Also make sure that form action should be the same JSP page on which it is declared

try using 2 forms, 1 form for each button.
or try using button only and create a function to handles it onclick()

Yes , you can use two submit buttons in singleJSP page.
Try this
<INPUT type="submit" name="bsubmit" value="Submit 1">
<INPUT type="submit" name="bsubmit" value="Submit 2">
Now when you create the script, these unique values will help us to figure out which of the submit buttons was pressed.
For example
Following is a sample Active Server Pages script to check for the submit button.
if Request.Form("bsubmit") = "Submit 1" then
btnID = "1"
elseif Request.Form("bsubmit") = "Submit 2" then
btnID = "2"
At server page you can write something like this

Related

Dropdown based on user input

Guys could someone help me to find solution for following case:
I have to type several letters for username (actually typed whole username) and click on it to choose. It looks as on screenshot which I added below.
Here is how it looks like on a page:
Added part
<div class="show-temp">
<div class="medium-12 column">
<div id="suggestionBox" class="search-input">
<label for="viewUser">Search user names</label>
<input type="text" autocomplete="off" id="viewUser" onkeydown="javascript: if(event.keyCode == 13 ){ userStats.HideContent();userStats.ApplyFilters(this); return false;}" placeholder="Search user names..">
I tried with this code:
Select dropdown2 = new Select(driver.findElement(By.xpath("//*[#id=\"viewUser\"]")));
dropdown2.selectByVisibleText("Zoran21");
Thread.sleep(2000);
And I got this error:
Element should have been "select" but was "input"
Please assist and thank you in advance
The Select class only works with <select> tags ,it's does not work for other Tags LIke (Input,div).if you want to work with DropDown first check the DoM ,how it's build .if its contain <select> Tag then Use Select Class other wise following the Below Approach.
The Error Indicates to you , DropDown Build With <Input> Tag so Your unable to Select Options By using Select Class
use below approach may be it's help you.
String searchUserXpath ="....";
String optionXpath ="----"
driver.findElement(By.xpath(searchUserXpath )).sendKeys(entersearchOptionValue totally /partially );
//for example option value is HYderabad ,you HYd Then takes xpath of
// HyderabaOPtion...
driver.findElement(By.xpath(optionXpath )).click();

jsp submit multiple select

I am working in JSP, which I am very new to, and trying to submit values from a select box that can select multiple values to an action class in Java.
<select multiple="multiple" name="ProjectIDs" listKey="projectID" listValue="name" id='lstBox2' style="min-width:150px">
</select>
At the moment I am able to select multiple values and pass the keys to a List variable called 'ProjectIDs' in my action class, which is what I want to do. But I would like to load all of the values in that select box without having to highlight them. This is because the values in the select box are loaded from another select box using javascript and a button that transfers the selected values from that select box to this one. So I shouldn't have to select the values in this select box because I always want to load ALL of them.
What is the easiest way to pre-select all of the values in the select box when I hit the submit button so that I don't have to highlight them myself?
Thank you! And sorry if my explanation was not very clear. I will further explain if this is the case.
Edit: I have made some changes in response to the answer provided. I am also using the Struts framework. So my select box starts out as an empty struts select box, which is then populated from the other struts select box.
<s:select list="#{}" multiple="true" name="ProjectIDs" listKey="projectID" listValue="name" id='lstBox2' style="min-width:150px"/>
Then in my form tag I added this onsubmit function:
<s:form id="formMain" action="performancereport" theme="simple" method="get" onsubmit="selectAllOptions('lstbox2');">
And then finally this javascript function:
function selectAllOptions(selStr)
{
var selObj = document.getElementById(selStr);
for (var i=0; i<selObj.options.length; i++) {
selObj.options[i].selected = true;
}
}
However, I am still having the same problem. If I highlight all the options myself, it works fine, but it is not automatically selecting them for me when I hit the submit button.
Any suggestions?
Edit 2: RESOLVED.
You can programatically select all the options using javascript:
var select = document.getElementById("lstBox2");
for(var i = 0; i < select.options.length; i++)
{
select.options[i].selected = true;
}
You can do this in your onsubmit function...right before you submit the form.
RESOLVED:
I added this jQuery code to occur before the form is submitted.
$(function() {
$('#formMain').submit(function() {
var select = document.getElementById("lstBox2");
for(var i = 0; i < select.options.length; i++)
{
select.options[i].selected = true;
}
return true;
});
});

Insert a button into a div on a jsp using document ready

I have a layout.jsp that I am using to build a page using multiple other jsp's.
In my main content jsp pages I have a button or two. In the current version I am positioning the div with the buttons relatively.
I am switching the way the page is designed. I created a div in my layout page to place the buttons into. I gave it an appButtonDiv class.
The issue I am running into is I do not know how to create a button insert it into the div. I would like to do this on document ready.
Here is the div I want to put the button(s) into.
<div class="row-fluid verticalmenubottom">
<div class="span12 verticalMenuCell appButtonDiv"></div>
</div>
Below is a example button.
<button type="submit" id="searchButton" name="searchButton"
value='<spring:message code="Generic.Search"/>'/>
Search</button>
There are some cases where I will have multiple buttons.
I haven't JSP competence, however you are asking how to add content to an element on document ready.
Using JQuery, you can do this:
$(document).ready(init);
else without JQuery, you might use the onload event of your body element:
<body onload="init()">...</body>
Then declare this function in your script tag:
function init() {
//this executes on document ready
//create your elements
var externDiv = document.createElement('div');
var internDiv = document.createElement('div');
externDiv.className = 'row-fluid verticalmenubottom';
internDiv .className = 'span12 verticalMenuCell appButtonDiv';
externDiv.appendChild(internDiv);
//find your button and put the new elements in it (check that your jsp don't change the id client-side)
var btn = document.getElementById('searchButton');
btn.appendChild(externDiv);
}
That's it!
You mean u want to insert a button inside a button?
or create another button inside the div?
<div class="row-fluid verticalmenubottom">
<div id="button1" class="span12 verticalMenuCell appButtonDiv"></div>
<div id="button2" class="span12 verticalMenuCell appButtonDiv"></div>
</div>
or i miss something ## ?

Button tag with Image while submit issue

I am using a
<button name="btn" value="Save"><img src="/image/save.png"/>Save</button>;
in my JSP page. when user lands on page, User is seeing that button and image on left to text of Save. but when the Form is submitted, in request.getParameterValues(), it is showing the value of button name:
(name=value as btn = img src="/image/save.png"/ Save)
and I am restrcting special character like < sign due to security concern. So I am expecting the request.getParameterValues for btn should return me "Save" and not complete <img tag.
Has anybody came across this issue? I dont want to add image using css.
Thanks in advance.
Nilesh
I'm not sure what you're asking but I'm guessing you're trying to display an image as a button?
Maybe try this:
<input type="image" src="/image/save.png" alt="Save" name="save" value="save"/>
then in your JSP or Servlet:
String save = request.getParameter("save");
If you are using IE, that's the expected result at least when I tested it on IE8, it sent the text between button tag (with the HTML tag stripped off) as value to the server. FF and Chrome both return the correct value set as the value attribute. Go test here.

Call a same servlet by clicking two different buttons to fetch data from same database

I created a JSP page where I created a search form where the user can see some specific records of a student in the same page just below the search button. The search is done as follows:
"select col1, col2 from table1 where regn_no='"+regn_no+"'";
After displaying two columns(col1, col2), I have another button in the same jsp page to view the details to see details of the particular student based on the regn_no. The search button is working fine, but the view details button not. I do not know how to call the same servlet. Any help will be much appreciated.
The button's name=value pair get sent as HTTP request parameter as well. Assuming that you've
<input type="submit" name="search" value="Search">
and
<input type="submit" name="view" value="View">
then you can distinguish the button pressed in the servlet as follows:
if (request.getParameter("search") != null) {
// Search button pressed.
} else if (request.getParameter("view") != null) {
// View button pressed.
}
See also:
Hidden features of HTML
Unrelated to the problem, you've got a SQL injection risk there. Use PreparedStatement.
Assume that the two buttons in different forms. Both the forms can POST data to the same servlet. But you should make a marker to tell servlet which button is clicked. You can use a hidden input to do that. Say <input type="hidden" value="search"/> in search form, and <input type="hidden" value="viewdetail"/> in view form. Of cause, you can distinguish which button is clicked by the button name attribute which would be posted to servlet.

Categories